From: Robert Varga Date: Tue, 23 Jan 2024 23:43:24 +0000 (+0100) Subject: Modernize UniqueValidator X-Git-Tag: v13.0.2~11 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=99f454a7e0e13a29a220fcbfc459d5233d47a3c4;p=yangtools.git Modernize UniqueValidator Use local variable type inference and instanceof patterns to reduce verbosity and explicit casts. Change-Id: I6f8a3a8fd96037d127c091aa16a705a31775d96f Signed-off-by: Robert Varga --- diff --git a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/UniqueValidator.java b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/UniqueValidator.java index b502a90d4d..20a89d07fd 100644 --- a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/UniqueValidator.java +++ b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/UniqueValidator.java @@ -7,17 +7,15 @@ */ package org.opendaylight.yangtools.yang.data.tree.impl; -import static com.google.common.base.Preconditions.checkState; -import static com.google.common.base.Verify.verify; import static java.util.Objects.requireNonNull; import com.google.common.base.MoreObjects; +import com.google.common.base.VerifyException; import com.google.common.collect.Collections2; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import java.util.Collections; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -25,7 +23,6 @@ import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant; @@ -71,10 +68,13 @@ abstract class UniqueValidator implements Immutable { @Override Map indexValues(final Object values) { - final Map index = Maps.newHashMapWithExpectedSize(descendants.size()); - final Iterator it = ((UniqueValues) values).iterator(); - for (Object obj : descendants) { - verify(index.put(decodeDescendant(obj), it.next()) == null); + final var index = Maps.newHashMapWithExpectedSize(descendants.size()); + final var it = ((UniqueValues) values).iterator(); + for (var obj : descendants) { + final var prev = index.put(decodeDescendant(obj), it.next()); + if (prev != null) { + throw new VerifyException("Unexpected collision with " + prev); + } } return index; } @@ -132,8 +132,7 @@ abstract class UniqueValidator implements Immutable { * @return Decoded path */ private static @NonNull ImmutableList decodePath(final Object obj) { - return obj instanceof NodeIdentifier ? ImmutableList.of((NodeIdentifier) obj) - : (ImmutableList) obj; + return obj instanceof NodeIdentifier nid ? ImmutableList.of(nid) : (ImmutableList) obj; } private static @NonNull Descendant decodeDescendant(final Object obj) { @@ -161,24 +160,28 @@ abstract class UniqueValidator implements Immutable { * @return Value for the descendant */ private static @Nullable Object extractValue(final DataContainerNode data, final List path) { - DataContainerNode current = data; - final Iterator it = path.iterator(); + var current = data; + final var it = path.iterator(); while (true) { - final NodeIdentifier step = it.next(); - final DataContainerChild next = current.childByArg(step); + final var step = it.next(); + final var next = current.childByArg(step); if (next == null) { return null; } if (!it.hasNext()) { - checkState(next instanceof LeafNode, "Unexpected node %s at %s", next, path); - final Object value = next.body(); - LOG.trace("Resolved {} to value {}", path, value); - return value; + if (next instanceof LeafNode leaf) { + final var value = leaf.body(); + LOG.trace("Resolved {} to value {}", path, value); + return value; + } + throw new IllegalStateException("Unexpected non-leaf node " + next + " at " + path); } - checkState(next instanceof DataContainerNode, "Unexpected node %s in %s", next, path); - current = (DataContainerNode) next; + if (!(next instanceof DataContainerNode nextContainer)) { + throw new IllegalStateException("Unexpected non-container node " + next + " at " + path); + } + current = nextContainer; } } }