(Multi)NamingContext: increment after computing max 31/9831/3
authorMarek Gradzki <[email protected]>
Wed, 13 Dec 2017 16:15:53 +0000 (17:15 +0100)
committerMarek Gradzki <[email protected]>
Wed, 13 Dec 2017 16:45:04 +0000 (16:45 +0000)
Instead of incrementing all elements of stream
and applying max, first compute max and then increment.

Change-Id: I4e45ffa74d9b8685abd7bafaddc77d1d9850abdb
Signed-off-by: Marek Gradzki <[email protected]>
vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java
vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java
vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java

index 6b10881..4d0e7a9 100644 (file)
@@ -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) {
index 60bd822..da7be81 100644 (file)
@@ -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
index 1fe4da5..df561c7 100644 (file)
@@ -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)