Add AugmentationIdentifier.create() methods 45/82645/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 30 May 2019 17:03:24 +0000 (19:03 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 21 Jun 2019 08:33:41 +0000 (10:33 +0200)
This adds two flavors of AugmentationIdentifier.create(), which
use a global weak cache for normalizing AugmentationIdentifiers.

The cache is carefully populated, i.e. it makes a point of using
immutable keys only.

JIRA: YANGTOOLS-998
Change-Id: I1e7d65b995e50773ec2260c9fb01bc1c6bbaafbf
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 700dcb02f55c7172c61f16a8318901b276238242)
(cherry picked from commit 0cc211c923a4f925a73fc598baf9d2fe4adf3216)

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.
          *