Add SchemaNodeIdentifier.Absolute.intern() 02/91502/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Jul 2020 12:02:19 +0000 (14:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 21 Jul 2020 12:05:49 +0000 (14:05 +0200)
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 <robert.varga@pantheon.tech>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/api/stmt/SchemaNodeIdentifier.java

index 1079eb24abac28722485b710f1c32f23a618fdd2..a80d2f62fbe0b631f5829ef3883ad1c30fed3c3c 100644 (file)
@@ -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<Absolute> 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<QName> 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<QName> qnames) {
         final ImmutableList<QName> 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;
     }