*/
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;
+import org.opendaylight.yangtools.util.HashCodeBuilder;
/**
*
* This would be the same as using a path like so, "/nodes/node/openflow:1" to refer to the openflow:1 node
*
*/
-public class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>, Immutable {
+public class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>, Immutable, Serializable {
+ 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);
* 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;
* 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;
@Override
public final String toString() {
- return addToStringAttributes(Objects.toStringHelper(this)).toString();
+ return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
}
/**
* @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));
}
/**
*/
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;
}
* 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();
* 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");
}
@SuppressWarnings("unchecked")
- public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifier<N> child(
+ public final <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> KeyedInstanceIdentifier<N, K> child(
final Class<N> listItem, final K listKey) {
final PathArgument arg = new IdentifiableItem<>(listItem, listKey);
- return (InstanceIdentifier<N>) childIdentifier(arg);
+ return (KeyedInstanceIdentifier<N, K>) childIdentifier(arg);
}
@SuppressWarnings("unchecked")
@Deprecated
public final List<PathArgument> getPath() {
if (legacyCache == null) {
- legacyCache = ImmutableList.<PathArgument>copyOf(getPathArguments());
+ legacyCache = ImmutableList.<PathArgument>copyOf(pathArguments);
}
return legacyCache;
/**
* Create a new InstanceIdentifierBuilder given a base InstanceIdentifier
*
- * @param basePath
+ * @param base
* @param <T>
* @return
*
}
Preconditions.checkArgument(a != null, "pathArguments may not be empty");
- return trustedCreate(a, pathArguments, hashBuilder.toInstance(), wildcard);
+ return trustedCreate(a, pathArguments, hashBuilder.build(), wildcard);
}
/**
*
* Example
* <pre>
- * List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
+ * List<PathArgument> path = Arrays.asList(new Item(Nodes.class))
* new InstanceIdentifier(path);
* </pre>
*
*
* @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;
Class<? extends DataObject> getType();
}
- private static abstract class AbstractPathArgument<T extends DataObject> implements PathArgument {
+ private static abstract class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
+ private static final long serialVersionUID = 1L;
private final Class<T> type;
protected AbstractPathArgument(final Class<T> type) {
}
@Override
- public int compareTo(PathArgument arg) {
+ public int compareTo(final PathArgument arg) {
return type.getCanonicalName().compareTo(arg.getType().getCanonicalName());
}
}
* @param <T>
*/
public static final class Item<T extends DataObject> extends AbstractPathArgument<T> {
+ private static final long serialVersionUID = 1L;
+
public Item(final Class<T> type) {
super(type);
}
* @param <T> The identifier of the object
*/
public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> extends AbstractPathArgument<I> {
+ private static final long serialVersionUID = 1L;
private final T key;
public IdentifiableItem(final Class<I> type, final T key) {
*
* @return
*/
+ @Override
InstanceIdentifier<T> build();
+
+ /*
+ * @deprecated use #build()
+ */
+ @Deprecated
+ InstanceIdentifier<T> toInstance();
+ }
+
+ private void writeObject(final java.io.ObjectOutputStream out) throws IOException {
+ 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 {
+ 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);
+ }
}
}