From 4840c9fc6ab956d6210ac33d8e9c5b1abbc4172e Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Wed, 13 Dec 2017 17:15:53 +0100 Subject: (Multi)NamingContext: increment after computing max Instead of incrementing all elements of stream and applying max, first compute max and then increment. Change-Id: I4e45ffa74d9b8685abd7bafaddc77d1d9850abdb Signed-off-by: Marek Gradzki --- .../common/translate/util/MultiNamingContext.java | 21 ++++++++++-------- .../common/translate/util/NamingContext.java | 16 ++++++-------- .../common/translate/util/NamingContextTest.java | 25 +++++++++++++++++----- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java index 6b10881d6..4d0e7a98f 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java @@ -23,6 +23,7 @@ import com.google.common.base.Optional; import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.util.RWUtils; import java.util.Collections; +import java.util.OptionalInt; import java.util.stream.Collectors; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.multi.naming.context.rev160411.MultiMappingCtxAugmentation; @@ -134,15 +135,17 @@ public class MultiNamingContext { return startIndex; } - return read.get().getMapping() - .stream() - .filter(mapping -> mapping.getName().equals(parentName)) - .flatMap(mapping -> mapping.getValue().stream()) - .mapToInt(Value::getIndex) - // do not use i++(need increase before, not after - .map(i -> ++i) - .max() - .orElse(startIndex); + final OptionalInt max = read.get().getMapping() + .stream() + .filter(mapping -> mapping.getName().equals(parentName)) + .flatMap(mapping -> mapping.getValue().stream()) + .mapToInt(Value::getIndex) + .max(); + if (max.isPresent()) { + return max.getAsInt() + 1; + } else { + return startIndex; + } } private KeyedInstanceIdentifier getMappingIid(final String name) { diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java index 60bd822fb..da7be8109 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java @@ -43,6 +43,7 @@ import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; public final class NamingContext implements AutoCloseable { private static final Collector SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector(); + private static final int START_INDEX = 0; private final String artificialNamePrefix; private final KeyedInstanceIdentifier namingContextIid; @@ -220,17 +221,14 @@ public final class NamingContext implements AutoCloseable { private int getNextAvailableIndex(final MappingContext mappingContext) { final Optional read = mappingContext.read(namingContextIid.child(Mappings.class)); - if (!read.isPresent()) { - return 0; + if (!read.isPresent() || read.get().getMapping().isEmpty()) { + return START_INDEX; } - return read.get().getMapping() - .stream() - .mapToInt(Mapping::getIndex) - // do not use i++(need increase before, not after - .map(i -> ++i) - .max() - .orElse(0); + return 1 + read.get().getMapping() + .stream() + .mapToInt(Mapping::getIndex) + .max().getAsInt(); } @Override diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java index 1fe4da50a..df561c737 100644 --- a/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java @@ -96,16 +96,31 @@ public class NamingContextTest implements InjectablesProcessor { } @Test - public void addNameNextIndex() throws Exception { + public void addName() throws Exception { namingContext.addName("name-3", mappingContext); verify(mappingContext, times(1)) - .put(instanceIdentifierArgumentCaptor.capture(), mappingArgumentCaptor.capture()); + .put(instanceIdentifierArgumentCaptor.capture(), mappingArgumentCaptor.capture()); assertEquals(instanceIdentifierArgumentCaptor.getValue(), parentKey("name-3")); assertEquals(mappingArgumentCaptor.getValue(), new MappingBuilder() - .setIndex(3) - .setName("name-3") - .build()); + .setIndex(3) + .setName("name-3") + .build()); + } + + @Test + public void addNameNoMapings() throws Exception { + when(mappingContext.read(namingContextIid.child(Mappings.class))).thenReturn(Optional.absent()); + + namingContext.addName("name-0", mappingContext); + verify(mappingContext, times(1)) + .put(instanceIdentifierArgumentCaptor.capture(), mappingArgumentCaptor.capture()); + + assertEquals(instanceIdentifierArgumentCaptor.getValue(), parentKey("name-0")); + assertEquals(mappingArgumentCaptor.getValue(), new MappingBuilder() + .setIndex(0) + .setName("name-0") + .build()); } @Test(expected = IllegalArgumentException.class) -- cgit 1.2.3-korg