diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2017-12-13 17:15:53 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-12-13 16:45:04 +0000 |
commit | 4840c9fc6ab956d6210ac33d8e9c5b1abbc4172e (patch) | |
tree | ed12e09ac706e3c172a3a948a1528e173718d153 /vpp-common/vpp-translate-utils | |
parent | 938e017b9a471c078a3bbc2213b4638315b2f8df (diff) |
(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 <mgradzki@cisco.com>
Diffstat (limited to 'vpp-common/vpp-translate-utils')
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<Mapping, MappingKey> 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<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector(); + private static final int START_INDEX = 0; private final String artificialNamePrefix; private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext, NamingContextKey> namingContextIid; @@ -220,17 +221,14 @@ public final class NamingContext implements AutoCloseable { private int getNextAvailableIndex(final MappingContext mappingContext) { final Optional<Mappings> 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) |