From 5dc406242f5199a87b8db19227f57e15c577e178 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 21 Jul 2020 14:02:19 +0200 Subject: [PATCH] Add SchemaNodeIdentifier.Absolute.intern() We will be using SchemaNodeIdentifiers in a lot of contexts, some of which involve dynamic wiring lookups. In these contexts we want the ability to squash an Absolute schema node identifier into a JVM-wide single instance -- hence we get benefits of cached hash code and instance-shortcuts on equals(). Change-Id: I2144ba659a783d585e59103dde3e6d292a63ec48 Signed-off-by: Robert Varga --- .../yang/model/api/stmt/SchemaNodeIdentifier.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java index 1079eb24ab..a80d2f62fb 100644 --- a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java +++ b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java @@ -12,6 +12,8 @@ import static java.util.Objects.requireNonNull; import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Interner; +import com.google.common.collect.Interners; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -32,6 +34,8 @@ public abstract class SchemaNodeIdentifier implements Immutable { * An absolute schema node identifier. */ public static final class Absolute extends SchemaNodeIdentifier { + private static final Interner INTERNER = Interners.newWeakInterner(); + Absolute(final QName qname) { super(qname); } @@ -49,7 +53,11 @@ public abstract class SchemaNodeIdentifier implements Immutable { } public static @NonNull Absolute of(final Collection nodeIdentifiers) { - return new Absolute(ImmutableList.copyOf(nodeIdentifiers)); + return new Absolute(nodeIdentifiers); + } + + public @NonNull Absolute intern() { + return INTERNER.intern(this); } @Override @@ -104,7 +112,7 @@ public abstract class SchemaNodeIdentifier implements Immutable { SchemaNodeIdentifier(final Collection qnames) { final ImmutableList tmp = ImmutableList.copyOf(qnames); - checkArgument(!tmp.isEmpty()); + checkArgument(!tmp.isEmpty(), "SchemaNodeIdentifier has to have at least one node identifier"); this.qnames = tmp.size() == 1 ? tmp.get(0) : tmp; } -- 2.36.6