Add a few useful utilities to InstanceIdentifier 79/2979/2
authorRobert Varga <rovarga@cisco.com>
Tue, 19 Nov 2013 16:37:41 +0000 (17:37 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 22 Nov 2013 08:16:14 +0000 (09:16 +0100)
Change-Id: Ia31f2a41d346b52e0426d20f0c790e404e319e21
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java

index 66c745e29a674e19cd129f7496800a085f1bd2e5..726eebe7fec4c07f5782f2ff1f9d3dac75f7eb22 100644 (file)
@@ -15,6 +15,8 @@ import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.Path;
 
+import com.google.common.collect.Iterables;
+
 /**
  * Uniquely identifies data location in the overall of data tree 
  * modeled by YANG.
@@ -25,7 +27,7 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
 
     private final List<PathArgument> path;
     private final Class<T> targetType;
-    
+
     public InstanceIdentifier(Class<T> type) {
         path = Collections.<PathArgument> singletonList(new Item<>(type));
         this.targetType = type;
@@ -53,6 +55,60 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
         return "InstanceIdentifier [path=" + path + "]";
     }
 
+    /**
+     * Return an instance identifier trimmed at the first occurrence of a
+     * specific component type.
+     * 
+     * @param type component type
+     * @return trimmed instance identifier, or null if the component type
+     *         is not present.
+     */
+    public <T extends DataObject> InstanceIdentifier<T> firstIdentifierOf(final Class<T> type) {
+        int i = 1;
+        for (final PathArgument a : path) {
+            if (type.equals(a.getType())) {
+                return new InstanceIdentifier<>(path.subList(0, i), type);
+            }
+
+            ++i;
+        }
+
+        return null;
+    }
+
+    /**
+     * Return the key associated with the first component of specified type in
+     * an identifier.
+     * 
+     * @param listItem component type
+     * @param listKey component key type
+     * @return key associated with the component, or null if the component type
+     *         is not present.
+     */
+    public <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
+        for (PathArgument i : path) {
+            if (listItem.equals(i.getType())) {
+                @SuppressWarnings("unchecked")
+                final K ret = ((IdentifiableItem<N, K>)i).getKey();
+                return ret;
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Return the key associated with the last component of the specified identifier.
+     * 
+     * @param id instance identifier
+     * @return key associated with the last component
+     */
+    public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
+        @SuppressWarnings("unchecked")
+        final K ret = ((IdentifiableItem<N, K>)Iterables.getLast(id.getPath())).getKey();
+        return ret;
+    }
+
     /**
      * Path argument of {@link InstanceIdentifier}.
      * <p>
@@ -101,7 +157,7 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
                 return false;
             return true;
         }
-        
+
         @Override
         public String toString() {
             return type.getName();
@@ -173,7 +229,7 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
                 Class<N> listItem, K listKey);
 
         <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
-        
+
         <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
                 Class<N> listItem, K listKey);
 
@@ -211,7 +267,7 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
         public BuilderImpl() {
             this.path = new ArrayList<>();
         }
-        
+
         public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
             this.path = new ArrayList<>(prefix);
             this.target = target;
@@ -254,7 +310,7 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
         public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
             return addNode(container);
         }
-        
+
         @Override
         public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
                 Class<N> listItem, K listKey) {
@@ -313,7 +369,7 @@ public final class InstanceIdentifier<T extends DataObject> implements Path<Inst
                 return false;
             }
         }
-        
+
         return true;
     }
 }