BUG-1590: introduce PathArgument.toRelativeString() and use it 07/10107/2
authorRobert Varga <rovarga@cisco.com>
Wed, 20 Aug 2014 15:58:15 +0000 (17:58 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 26 Aug 2014 13:00:45 +0000 (13:00 +0000)
This patch introduces a new method, toRelativeString(), which does the
same thing as toString(), but takes into account previous path argument
and does not emit namespace if it matches the previous namespace.

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

index dba2e726fa3d04bca68e2b806f20e1a74242bb9d..e9f940a4555bad2af63797018916240a4209d3a3 100644 (file)
@@ -31,6 +31,7 @@ import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.Path;
 import org.opendaylight.yangtools.util.HashCodeBuilder;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 
 /**
@@ -311,7 +312,7 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
     /**
      * Path argument / component of InstanceIdentifier
      *
-     * Path argument uniquelly identifies node in data tree on particular
+     * Path argument uniquely identifies node in data tree on particular
      * level.
      * <p>
      * This interface itself is used as common parent for actual
@@ -339,6 +340,17 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
          * @return Node type
          */
         QName getNodeType();
+
+        /**
+         * Return the string representation of this object for use in context
+         * provided by a previous object. This method can be implemented in
+         * terms of {@link #toString()}, but implementations are encourage to
+         * reuse any context already emitted by the previous object.
+         *
+         * @param previous Previous path argument
+         * @return String representation
+         */
+        String toRelativeString(PathArgument previous);
     }
 
     private static abstract class AbstractPathArgument implements PathArgument {
@@ -380,6 +392,18 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         public String toString() {
             return getNodeType().toString();
         }
+
+        @Override
+        public String toRelativeString(final PathArgument previous) {
+            if (previous instanceof AbstractPathArgument) {
+                final QNameModule mod = ((AbstractPathArgument)previous).getNodeType().getModule();
+                if (getNodeType().getModule().equals(mod)) {
+                    return getNodeType().getLocalName();
+                }
+            }
+
+            return getNodeType().toString();
+        }
     }
 
     /**
@@ -498,6 +522,11 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         public String toString() {
             return super.toString() + '[' + keyValues + ']';
         }
+
+        @Override
+        public String toRelativeString(final PathArgument previous) {
+            return super.toRelativeString(previous) + '[' + keyValues + ']';
+        }
     }
 
     /**
@@ -539,6 +568,11 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         public String toString() {
             return super.toString() + '[' + value + ']';
         }
+
+        @Override
+        public String toRelativeString(final PathArgument previous) {
+            return super.toRelativeString(previous) + '[' + value + ']';
+        }
     }
 
     /**
@@ -590,7 +624,6 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
         }
 
         /**
-         *
          * Returns set of all possible child nodes
          *
          * @return set of all possible child nodes.
@@ -606,6 +639,11 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
             return sb.toString();
         }
 
+        @Override
+        public String toRelativeString(final PathArgument previous) {
+            return toString();
+        }
+
         @Override
         public boolean equals(final Object o) {
             if (this == o) {
@@ -736,15 +774,14 @@ public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier
             synchronized (this) {
                 ret = toStringCache;
                 if (ret == null) {
-                    final StringBuilder builder = new StringBuilder('/');
-                    boolean first = true;
+                    final StringBuilder builder = new StringBuilder("/");
+                    PathArgument prev = null;
                     for (PathArgument argument : getPathArguments()) {
-                        if (first) {
-                            first = false;
-                        } else {
+                        if (prev != null) {
                             builder.append('/');
                         }
-                        builder.append(argument.toString());
+                        builder.append(argument.toRelativeString(prev));
+                        prev = argument;
                     }
 
                     ret = builder.toString();