Add SchemaNodeIdentifier.Absolute.intern()
[yangtools.git] / 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;
     }