diff options
author | 2017-12-13 17:15:53 +0100 | |
---|---|---|
committer | 2017-12-13 16:45:04 +0000 | |
commit | 4840c9fc6ab956d6210ac33d8e9c5b1abbc4172e (patch) | |
tree | ed12e09ac706e3c172a3a948a1528e173718d153 /vpp-common/vpp-translate-utils/src/main/java | |
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/src/main/java')
2 files changed, 19 insertions, 18 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 |