Introduce top-level pom file.
[mdsal.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / InstanceIdentifier.java
index 9a597152c3ce4f3aa1a4e04e8ff94c230378e0c4..f99f3a45dea4570f6dfb5723960808c89389a3b2 100644 (file)
@@ -7,19 +7,19 @@
  */
 package org.opendaylight.yangtools.yang.binding;
 
-import com.google.common.base.Objects;
-import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
-
 import java.io.IOException;
 import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
-
 import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.concepts.Path;
@@ -55,17 +55,29 @@ import org.opendaylight.yangtools.util.HashCodeBuilder;
  *
  */
 public class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>, Immutable, Serializable {
-    private static final long serialVersionUID = 1L;
+    private static final Field PATHARGUMENTS_FIELD;
+    private static final long serialVersionUID = 2L;
     /*
      * Protected to differentiate internal and external access. Internal
      * access is required never to modify the contents. References passed
      * to outside entities have to be wrapped in an unmodifiable view.
      */
-    protected final Iterable<PathArgument> pathArguments;
+    protected transient final Iterable<PathArgument> pathArguments;
     private final Class<T> targetType;
     private final boolean wildcarded;
     private final int hash;
 
+    static {
+        final Field f;
+        try {
+            f = InstanceIdentifier.class.getDeclaredField("pathArguments");
+        } catch (NoSuchFieldException | SecurityException e) {
+            throw new ExceptionInInitializerError(e);
+        }
+        f.setAccessible(true);
+        PATHARGUMENTS_FIELD = f;
+    }
+
     InstanceIdentifier(final Class<T> type, final Iterable<PathArgument> pathArguments, final boolean wildcarded, final int hash) {
         this.pathArguments = Preconditions.checkNotNull(pathArguments);
         this.targetType = Preconditions.checkNotNull(type);
@@ -95,7 +107,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * Check whether an instance identifier contains any wildcards. A wildcard
      * is an path argument which has a null key.
      *
-     * @return @true if any of the path arguments has a null key.
+     * @return true if any of the path arguments has a null key.
      */
     public final boolean isWildcarded() {
         return wildcarded;
@@ -152,7 +164,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * quick checks on their specific fields.
      *
      * @param other The other identifier, guaranteed to be the same class
-     * @return @true if the other identifier cannot be equal to this one.
+     * @return true if the other identifier cannot be equal to this one.
      */
     protected boolean fastNonEqual(final InstanceIdentifier<?> other) {
         return false;
@@ -160,7 +172,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
 
     @Override
     public final String toString() {
-        return addToStringAttributes(Objects.toStringHelper(this)).toString();
+        return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
     }
 
     /**
@@ -170,7 +182,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * @return ToStringHelper instance which was passed in
      */
     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return toStringHelper.add("targetType", targetType).add("path", Iterables.toString(getPathArguments()));
+        return toStringHelper.add("targetType", targetType).add("path", Iterables.toString(pathArguments));
     }
 
     /**
@@ -194,10 +206,10 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      */
     public final <I extends DataObject> InstanceIdentifier<I> firstIdentifierOf(final Class<I> type) {
         int i = 1;
-        for (final PathArgument a : getPathArguments()) {
+        for (final PathArgument a : pathArguments) {
             if (type.equals(a.getType())) {
                 @SuppressWarnings("unchecked")
-                final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(Iterables.limit(getPathArguments(), i));
+                final InstanceIdentifier<I> ret = (InstanceIdentifier<I>) internalCreate(Iterables.limit(pathArguments, i));
                 return ret;
             }
 
@@ -217,7 +229,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      *         is not present.
      */
     public final <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
-        for (final PathArgument i : getPathArguments()) {
+        for (final PathArgument i : pathArguments) {
             if (listItem.equals(i.getType())) {
                 @SuppressWarnings("unchecked")
                 final K ret = ((IdentifiableItem<N, K>)i).getKey();
@@ -274,7 +286,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      * non-wildcarded PathArgument counterpart.
      *
      * @param other Identifier which should be checked for inclusion.
-     * @return @true if this identifier contains the other object
+     * @return true if this identifier contains the other object
      */
     public final boolean containsWildcarded(final InstanceIdentifier<?> other) {
         Preconditions.checkNotNull(other, "other should not be null");
@@ -343,7 +355,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
     @Deprecated
     public final List<PathArgument> getPath() {
         if (legacyCache == null) {
-            legacyCache = ImmutableList.<PathArgument>copyOf(getPathArguments());
+            legacyCache = ImmutableList.<PathArgument>copyOf(pathArguments);
         }
 
         return legacyCache;
@@ -352,7 +364,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
     /**
      * Create a new InstanceIdentifierBuilder given a base InstanceIdentifier
      *
-     * @param basePath
+     * @param base
      * @param <T>
      * @return
      *
@@ -418,7 +430,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
         }
         Preconditions.checkArgument(a != null, "pathArguments may not be empty");
 
-        return trustedCreate(a, pathArguments, hashBuilder.toInstance(), wildcard);
+        return trustedCreate(a, pathArguments, hashBuilder.build(), wildcard);
     }
 
     /**
@@ -426,7 +438,7 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      *
      * Example
      * <pre>
-     *  List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
+     *  List&lt;PathArgument&gt; path = Arrays.asList(new Item(Nodes.class))
      *  new InstanceIdentifier(path);
      * </pre>
      *
@@ -467,8 +479,13 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
      *
      * @param id instance identifier
      * @return key associated with the last component
+     * @throws IllegalArgumentException if the supplied identifier type cannot have a key.
+     * @throws NullPointerException if id is null.
      */
     public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
+        Preconditions.checkNotNull(id);
+        Preconditions.checkArgument(id instanceof KeyedInstanceIdentifier, "%s does not have a key", id);
+
         @SuppressWarnings("unchecked")
         final K ret = ((KeyedInstanceIdentifier<N, K>)id).getKey();
         return ret;
@@ -649,20 +666,37 @@ public class InstanceIdentifier<T extends DataObject> implements Path<InstanceId
          *
          * @return
          */
+        @Override
         InstanceIdentifier<T> build();
+
+        /*
+         * @deprecated use #build()
+         */
+        @Deprecated
+        InstanceIdentifier<T> toInstance();
     }
 
     private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
-        out.writeObject(targetType);
-        out.writeBoolean(wildcarded);
-        out.writeInt(hash);
-        out.write(Iterables.size(pathArguments));
+        out.defaultWriteObject();
+        out.writeInt(Iterables.size(pathArguments));
         for (Object o : pathArguments) {
             out.writeObject(o);
         }
     }
 
     private void readObject(final java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
-        // TODO Auto-generated method stub
+        in.defaultReadObject();
+
+        final int size = in.readInt();
+        final List<PathArgument> args = new ArrayList<>(size);
+        for (int i = 0; i < size; ++i) {
+            args.add((PathArgument) in.readObject());
+        }
+
+        try {
+            PATHARGUMENTS_FIELD.set(this, ImmutableList.copyOf(args));
+        } catch (IllegalArgumentException | IllegalAccessException e) {
+            throw new IOException(e);
+        }
     }
 }