Add AugmentationIdentifier.create() methods
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / YangInstanceIdentifier.java
index 0f693a7ff5fbadd763bf94853ca5d62484e41811..35afc97afb70818a03c509122ff75ce60a593549 100644 (file)
@@ -708,6 +708,15 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
      */
     public static final class AugmentationIdentifier implements PathArgument {
         private static final long serialVersionUID = -8122335594681936939L;
+
+        private static final LoadingCache<ImmutableSet<QName>, AugmentationIdentifier> CACHE = CacheBuilder.newBuilder()
+                .weakValues().build(new CacheLoader<ImmutableSet<QName>, AugmentationIdentifier>() {
+                    @Override
+                    public AugmentationIdentifier load(final ImmutableSet<QName> key) {
+                        return new AugmentationIdentifier(key);
+                    }
+                });
+
         private final ImmutableSet<QName> childNames;
 
         @Override
@@ -738,6 +747,29 @@ public abstract class YangInstanceIdentifier implements Path<YangInstanceIdentif
             this.childNames = ImmutableSet.copyOf(childNames);
         }
 
+        /**
+         * Return an AugmentationIdentifier for a particular set of QNames. Unlike the constructor, this factory method
+         * uses a global instance cache, resulting in object reuse for equal inputs.
+         *
+         * @param childNames Set of possible child nodes
+         * @return An {@link AugmentationIdentifier}
+         */
+        public static AugmentationIdentifier create(final ImmutableSet<QName> childNames) {
+            return CACHE.getUnchecked(childNames);
+        }
+
+        /**
+         * Return an AugmentationIdentifier for a particular set of QNames. Unlike the constructor, this factory method
+         * uses a global instance cache, resulting in object reuse for equal inputs.
+         *
+         * @param childNames Set of possible child nodes
+         * @return An {@link AugmentationIdentifier}
+         */
+        public static AugmentationIdentifier create(final Set<QName> childNames) {
+            final AugmentationIdentifier existing = CACHE.getIfPresent(childNames);
+            return existing != null ? existing : create(ImmutableSet.copyOf(childNames));
+        }
+
         /**
          * Returns set of all possible child nodes.
          *