import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Array;
+import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
* </ul>
*
*
- * @see http://tools.ietf.org/html/rfc6020#section-9.13
+ * @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
*/
public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
+ /**
+ * An empty {@link YangInstanceIdentifier}. It corresponds to the path of the conceptual
+ * root of the YANG namespace.
+ */
+ public static final YangInstanceIdentifier EMPTY = trustedCreate(Collections.<PathArgument>emptyList());
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, ImmutableList> LEGACYPATH_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, ImmutableList.class, "legacyPath");
private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
- private static final YangInstanceIdentifier EMPTY = trustedCreate(Collections.<PathArgument>emptyList());
+ private static final Field PATHARGUMENTS_FIELD;
- private static final long serialVersionUID = 2L;
- private final Iterable<PathArgument> pathArguments;
+ private static final long serialVersionUID = 3L;
+ private final transient Iterable<PathArgument> pathArguments;
private final int hash;
private transient volatile ImmutableList<PathArgument> legacyPath = null;
private transient volatile String toStringCache = null;
- private final ImmutableList<PathArgument> getLegacyPath() {
+ static {
+ final Field f;
+ try {
+ f = YangInstanceIdentifier.class.getDeclaredField("pathArguments");
+ } catch (NoSuchFieldException | SecurityException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ f.setAccessible(true);
+
+ PATHARGUMENTS_FIELD = f;
+ }
+
+ private ImmutableList<PathArgument> getLegacyPath() {
// Temporary variable saves a volatile read
ImmutableList<PathArgument> ret = legacyPath;
if (ret == null) {
this.hash = hash;
}
- private static final YangInstanceIdentifier trustedCreate(final Iterable<PathArgument> path) {
+ private static YangInstanceIdentifier trustedCreate(final Iterable<PathArgument> path) {
final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
for (PathArgument a : path) {
hash.addArgument(a);
}
- return new YangInstanceIdentifier(path, hash.toInstance());
+ return new YangInstanceIdentifier(path, hash.build());
}
- public static final YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
+ public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
if (Iterables.isEmpty(path)) {
return EMPTY;
}
return trustedCreate(ImmutableList.copyOf(path));
}
- public static final YangInstanceIdentifier create(final PathArgument... path) {
+ public static YangInstanceIdentifier create(final PathArgument... path) {
// We are forcing a copy, since we cannot trust the user
return create(Arrays.asList(path));
}
*
* @return new builder for InstanceIdentifier with empty path arguments.
*/
- static public InstanceIdentifierBuilder builder() {
+ public static InstanceIdentifierBuilder builder() {
return new BuilderImpl();
}
* @param origin Instace Identifier from which path arguments are copied.
* @return new builder for InstanceIdentifier with path arguments copied from original instance identifier.
*/
- static public InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
+ public static InstanceIdentifierBuilder builder(final YangInstanceIdentifier origin) {
return new BuilderImpl(origin.getPathArguments(), origin.hashCode());
}
- /**
- * Returns new builder for InstanceIdentifier with first path argument set to {@link NodeIdentifier}.
- *
- * @param node QName of first {@link NodeIdentifier} path argument.
- * @return new builder for InstanceIdentifier with first path argument set to {@link NodeIdentifier}.
- *
- * @deprecated Either use {@link #node(QName)} or instantiate an intermediate builder.
- */
- @Deprecated
- public static InstanceIdentifierBuilder builder(final QName node) {
- return builder().node(node);
- }
-
/**
* Path argument / component of InstanceIdentifier
*
}
/**
- *
* Fluent Builder of Instance Identifier instances
- *
- * @
- *
*/
public interface InstanceIdentifierBuilder extends Builder<YangInstanceIdentifier> {
/**
*
* @return {@link YangInstanceIdentifier}
*/
+ @Override
YangInstanceIdentifier build();
+
+ /*
+ * @deprecated use #build()
+ */
+ @Deprecated
+ YangInstanceIdentifier toInstance();
}
/**
* / module to the target node.
*
*
- * @see http://tools.ietf.org/html/rfc6020#section-7.15
+ * @see <a href="http://tools.ietf.org/html/rfc6020#section-7.15">RFC6020</a>
*/
public static final class AugmentationIdentifier implements PathArgument {
private static final long serialVersionUID = -8122335594681936939L;
this.childNames = ImmutableSet.copyOf(childNames);
}
- /**
- * Augmentation node has no QName
- *
- * @deprecated Use
- * {@link AugmentationIdentifier#AugmentationIdentifier(Set)}
- * instead.
- */
- @Deprecated
- public AugmentationIdentifier(final QName nodeType, final Set<QName> childNames) {
- this(childNames);
- }
-
/**
* Returns set of all possible child nodes
*
@Override
public String toString() {
- final StringBuffer sb = new StringBuffer("AugmentationIdentifier{");
+ final StringBuilder sb = new StringBuilder("AugmentationIdentifier{");
sb.append("childNames=").append(childNames).append('}');
return sb.toString();
}
@Override
public YangInstanceIdentifier build() {
- return new YangInstanceIdentifier(ImmutableList.copyOf(path), hash.toInstance());
+ return new YangInstanceIdentifier(ImmutableList.copyOf(path), hash.build());
}
}
}
return ret;
}
+
+ private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
+ inputStream.defaultReadObject();
+ legacyPath = ImmutableList.copyOf((Collection<PathArgument>)inputStream.readObject());
+
+ try {
+ PATHARGUMENTS_FIELD.set(this, legacyPath);
+ } catch (IllegalArgumentException | IllegalAccessException e) {
+ throw new IOException(e);
+ }
+ }
+
+ private Object readResolve() throws ObjectStreamException {
+ return legacyPath.isEmpty() ? EMPTY : this;
+ }
+
+ private void writeObject(final ObjectOutputStream outputStream) throws IOException {
+ /*
+ * This may look strange, but what we are doing here is side-stepping the fact
+ * that pathArguments is not generally serializable. We are forcing instantiation
+ * of the legacy path, which is an ImmutableList (thus Serializable) and write
+ * it out. The read path does the opposite -- it reads the legacyPath and then
+ * uses invocation API to set the field.
+ */
+ ImmutableList<PathArgument> pathArguments = getLegacyPath();
+ outputStream.defaultWriteObject();
+ outputStream.writeObject(pathArguments);
+ }
}