Introduce YangInstanceIdentifier.toOptimized() 44/22544/2
authorRobert Varga <rovarga@cisco.com>
Sun, 14 Jun 2015 09:12:29 +0000 (11:12 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 15 Jun 2015 08:47:48 +0000 (08:47 +0000)
Users often need to retain a YangInstanceIdentifier for use in either
addressing the same element in the data tree, or to create child
instance identifiers. Create a new method, toOptimized(), which returns
an equivalent FixedYangInstanceIdentifier instance.

Change-Id: I057db1d539520195cd38c1bfc77a8be1384108e2
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java
yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/YangInstanceIdentifier.java

index bc60c5a5819c5a6e6126bb8e86d1e0da4cd48721..773fa11b36854e791e5efeca79549bfb04997cdc 100644 (file)
@@ -99,4 +99,9 @@ final class FixedYangInstanceIdentifier extends YangInstanceIdentifier {
             return super.pathArgumentsEqual(other);
         }
     }
+
+    @Override
+    public YangInstanceIdentifier toOptimized() {
+        return this;
+    }
 }
index 816a082a3dab9385e15e749e1873a3dc1a5a9cc1..9a513aaf62ef0da44a61644a53401ffc4656db8e 100644 (file)
@@ -138,4 +138,9 @@ final class StackedYangInstanceIdentifier extends YangInstanceIdentifier {
         }
         outputStream.writeObject(p);
     }
+
+    @Override
+    public YangInstanceIdentifier toOptimized() {
+        return FixedYangInstanceIdentifier.create(getPathArguments());
+    }
 }
index 46489d31f34847292b238ab8e21f270f7574f025..82d1ceb260731d0df04df83ecd25b7d71d48e408 100644 (file)
@@ -6,6 +6,7 @@
  */
 package org.opendaylight.yangtools.yang.data.api;
 
+import com.google.common.annotations.Beta;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableMap;
@@ -95,6 +96,15 @@ public abstract class YangInstanceIdentifier extends IterablePathArguments imple
      */
     public abstract boolean isEmpty();
 
+    /**
+     * Return an optimized version of this identifier, useful when the identifier
+     * will be used very frequently.
+     *
+     * @return A optimized equivalent instance.
+     */
+    @Beta
+    public abstract YangInstanceIdentifier toOptimized();
+
     /**
      * Return the conceptual parent {@link YangInstanceIdentifier}, which has
      * one item less in {@link #getPathArguments()}.
@@ -243,6 +253,55 @@ public abstract class YangInstanceIdentifier extends IterablePathArguments imple
         return Optional.of(createRelativeIdentifier(common));
     }
 
+    @Override
+    public final boolean contains(final YangInstanceIdentifier other) {
+        Preconditions.checkArgument(other != null, "other should not be null");
+
+        final Iterator<?> lit = getPathArguments().iterator();
+        final Iterator<?> oit = other.getPathArguments().iterator();
+
+        while (lit.hasNext()) {
+            if (!oit.hasNext()) {
+                return false;
+            }
+
+            if (!lit.next().equals(oit.next())) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public final String toString() {
+        /*
+         * The toStringCache is safe, since the object contract requires
+         * immutability of the object and all objects referenced from this
+         * object.
+         * Used lists, maps are immutable. Path Arguments (elements) are also
+         * immutable, since the PathArgument contract requires immutability.
+         * The cache is thread-safe - if multiple computations occurs at the
+         * same time, cache will be overwritten with same result.
+         */
+        String ret = toStringCache;
+        if (ret == null) {
+            final StringBuilder builder = new StringBuilder("/");
+            PathArgument prev = null;
+            for (PathArgument argument : getPathArguments()) {
+                if (prev != null) {
+                    builder.append('/');
+                }
+                builder.append(argument.toRelativeString(prev));
+                prev = argument;
+            }
+
+            ret = builder.toString();
+            TOSTRINGCACHE_UPDATER.lazySet(this, ret);
+        }
+        return ret;
+    }
+
     private static int hashCode(final Object value) {
         if (value == null) {
             return 0;
@@ -268,7 +327,6 @@ public abstract class YangInstanceIdentifier extends IterablePathArguments imple
     // Static factories & helpers
 
     /**
-     *
      * Returns a new InstanceIdentifier with only one path argument of type {@link NodeIdentifier} with supplied QName
      *
      * @param name QName of first node identifier
@@ -279,7 +337,6 @@ public abstract class YangInstanceIdentifier extends IterablePathArguments imple
     }
 
     /**
-     *
      * Returns new builder for InstanceIdentifier with empty path arguments.
      *
      * @return new builder for InstanceIdentifier with empty path arguments.
@@ -292,7 +349,7 @@ public abstract class YangInstanceIdentifier extends IterablePathArguments imple
      *
      * Returns new builder for InstanceIdentifier with path arguments copied from original instance identifier.
      *
-     * @param origin Instace Identifier from which path arguments are copied.
+     * @param origin InstanceIdentifier from which path arguments are copied.
      * @return new builder for InstanceIdentifier with path arguments copied from original instance identifier.
      */
     public static InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
@@ -692,53 +749,4 @@ public abstract class YangInstanceIdentifier extends IterablePathArguments imple
             }
         }
     }
-
-    @Override
-    public final boolean contains(final YangInstanceIdentifier other) {
-        Preconditions.checkArgument(other != null, "other should not be null");
-
-        final Iterator<?> lit = getPathArguments().iterator();
-        final Iterator<?> oit = other.getPathArguments().iterator();
-
-        while (lit.hasNext()) {
-            if (!oit.hasNext()) {
-                return false;
-            }
-
-            if (!lit.next().equals(oit.next())) {
-                return false;
-            }
-        }
-
-        return true;
-    }
-
-    @Override
-    public final String toString() {
-        /*
-         * The toStringCache is safe, since the object contract requires
-         * immutability of the object and all objects referenced from this
-         * object.
-         * Used lists, maps are immutable. Path Arguments (elements) are also
-         * immutable, since the PathArgument contract requires immutability.
-         * The cache is thread-safe - if multiple computations occurs at the
-         * same time, cache will be overwritten with same result.
-         */
-        String ret = toStringCache;
-        if (ret == null) {
-            final StringBuilder builder = new StringBuilder("/");
-            PathArgument prev = null;
-            for (PathArgument argument : getPathArguments()) {
-                if (prev != null) {
-                    builder.append('/');
-                }
-                builder.append(argument.toRelativeString(prev));
-                prev = argument;
-            }
-
-            ret = builder.toString();
-            TOSTRINGCACHE_UPDATER.lazySet(this, ret);
-        }
-        return ret;
-    }
 }