--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import java.io.ObjectStreamException;
+import java.util.List;
+import org.opendaylight.yangtools.util.HashCodeBuilder;
+
+final class FixedYangInstanceIdentifier extends YangInstanceIdentifier {
+ static final FixedYangInstanceIdentifier EMPTY_INSTANCE = new FixedYangInstanceIdentifier(ImmutableList.<PathArgument>of(), new HashCodeBuilder<>().build());
+ private static final long serialVersionUID = 1L;
+ private final ImmutableList<PathArgument> path;
+ private transient volatile YangInstanceIdentifier parent;
+
+ private FixedYangInstanceIdentifier(final ImmutableList<PathArgument> path, final int hash) {
+ super(hash);
+ this.path = Preconditions.checkNotNull(path, "path must not be null.");
+ }
+
+ static FixedYangInstanceIdentifier create(final Iterable<? extends PathArgument> path, final int hash) {
+ return new FixedYangInstanceIdentifier(ImmutableList.copyOf(path), hash);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return path.isEmpty();
+ }
+
+ @Override
+ public YangInstanceIdentifier getParent() {
+ if (path.isEmpty()) {
+ return null;
+ }
+
+ YangInstanceIdentifier ret = parent;
+ if (ret == null) {
+ ret = YangInstanceIdentifier.create(path.subList(0, path.size() - 1));
+ parent = ret;
+ }
+
+ return ret;
+ }
+
+ @Override
+ public List<PathArgument> getPath() {
+ return path;
+ }
+
+ @Override
+ public List<PathArgument> getPathArguments() {
+ return path;
+ }
+
+ @Override
+ public List<PathArgument> getReversePathArguments() {
+ return path.reverse();
+ }
+
+ @Override
+ List<PathArgument> tryPathArguments() {
+ return path;
+ }
+
+ @Override
+ public PathArgument getLastPathArgument() {
+ return path.isEmpty()? null : path.get(path.size() - 1);
+ }
+
+ @Override
+ YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
+ if (skipFromRoot == path.size()) {
+ return EMPTY_INSTANCE;
+ }
+
+ final ImmutableList<PathArgument> newPath = path.subList(skipFromRoot, path.size());
+ final HashCodeBuilder<PathArgument> hash = new HashCodeBuilder<>();
+ for (PathArgument a : newPath) {
+ hash.addArgument(a);
+ }
+
+ return new FixedYangInstanceIdentifier(newPath, hash.build());
+ }
+
+ private Object readResolve() throws ObjectStreamException {
+ return path.isEmpty() ? EMPTY_INSTANCE : this;
+ }
+
+ @Override
+ boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
+ if (other instanceof FixedYangInstanceIdentifier) {
+ return path.equals(((FixedYangInstanceIdentifier) other).path);
+ } else {
+ return super.pathArgumentsEqual(other);
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.UnmodifiableIterator;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
+final class StackedPathArguments implements Iterable<PathArgument> {
+ private final List<StackedYangInstanceIdentifier> stack;
+ private final YangInstanceIdentifier base;
+
+ public StackedPathArguments(final YangInstanceIdentifier base, final List<StackedYangInstanceIdentifier> stack) {
+ this.base = Preconditions.checkNotNull(base);
+ this.stack = Preconditions.checkNotNull(stack);
+ }
+
+ @Override
+ public Iterator<PathArgument> iterator() {
+ return new IteratorImpl(base, stack);
+ }
+
+ private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
+ private final Iterator<StackedYangInstanceIdentifier> stack;
+ private final Iterator<PathArgument> base;
+
+ IteratorImpl(final YangInstanceIdentifier base, final Collection<StackedYangInstanceIdentifier> stack) {
+ this.base = base.getPathArguments().iterator();
+ this.stack = stack.iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return stack.hasNext();
+ }
+
+ @Override
+ public PathArgument next() {
+ if (base.hasNext()) {
+ return base.next();
+ }
+ return stack.next().getLastPathArgument();
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.UnmodifiableIterator;
+import java.util.Iterator;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+
+final class StackedReversePathArguments implements Iterable<PathArgument> {
+ private final StackedYangInstanceIdentifier identifier;
+
+ StackedReversePathArguments(final StackedYangInstanceIdentifier identifier) {
+ this.identifier = Preconditions.checkNotNull(identifier);
+ }
+
+ @Override
+ public Iterator<PathArgument> iterator() {
+ return new IteratorImpl(identifier);
+ }
+
+ private static final class IteratorImpl extends UnmodifiableIterator<PathArgument> {
+ private StackedYangInstanceIdentifier identifier;
+ private Iterator<PathArgument> tail;
+
+ IteratorImpl(final StackedYangInstanceIdentifier identifier) {
+ this.identifier = Preconditions.checkNotNull(identifier);
+ }
+
+ @Override
+ public boolean hasNext() {
+ return tail == null || tail.hasNext();
+ }
+
+ @Override
+ public PathArgument next() {
+ if (tail != null) {
+ return tail.next();
+ }
+
+ final PathArgument ret = identifier.getLastPathArgument();
+ final YangInstanceIdentifier next = identifier.getParent();
+ if (!(next instanceof StackedYangInstanceIdentifier)) {
+ tail = next.getReversePathArguments().iterator();
+ identifier = null;
+ } else {
+ identifier = (StackedYangInstanceIdentifier) next;
+ }
+
+ return ret;
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.api;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
+import com.google.common.collect.ImmutableList;
+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.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+
+final class StackedYangInstanceIdentifier extends YangInstanceIdentifier {
+ private static final long serialVersionUID = 1L;
+ private static final Field PARENT_FIELD;
+
+ static {
+ final Field f;
+ try {
+ f = StackedYangInstanceIdentifier.class.getDeclaredField("parent");
+ } catch (NoSuchFieldException | SecurityException e) {
+ throw new ExceptionInInitializerError(e);
+ }
+ f.setAccessible(true);
+
+ PARENT_FIELD = f;
+ }
+
+ @SuppressWarnings("rawtypes")
+ private static final AtomicReferenceFieldUpdater<StackedYangInstanceIdentifier, ImmutableList> LEGACYPATH_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(StackedYangInstanceIdentifier.class, ImmutableList.class, "legacyPath");
+
+ private final YangInstanceIdentifier parent;
+ private final PathArgument pathArgument;
+
+ private transient volatile ImmutableList<PathArgument> legacyPath;
+ private transient volatile Iterable<PathArgument> pathArguments;
+ private transient volatile Iterable<PathArgument> reversePathArguments;
+
+ StackedYangInstanceIdentifier(final YangInstanceIdentifier parent, final PathArgument pathArgument, final int hash) {
+ super(hash);
+ this.parent = Preconditions.checkNotNull(parent);
+ this.pathArgument = Preconditions.checkNotNull(pathArgument);
+ }
+
+ @Override
+ public YangInstanceIdentifier getParent() {
+ return parent;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return false;
+ }
+
+ @Override
+ public List<PathArgument> getPath() {
+ // Temporary variable saves a volatile read
+ ImmutableList<PathArgument> ret = legacyPath;
+ if (ret == null) {
+ // We could have used a synchronized block, but the window is quite
+ // small and worst that can happen is duplicate object construction.
+ ret = ImmutableList.copyOf(getPathArguments());
+ LEGACYPATH_UPDATER.lazySet(this, ret);
+ }
+
+ return ret;
+ }
+
+ @Override
+ public Iterable<PathArgument> getPathArguments() {
+ Iterable<PathArgument> ret = tryPathArguments();
+ if (ret == null) {
+ List<StackedYangInstanceIdentifier> stack = new ArrayList<>();
+ YangInstanceIdentifier current = this;
+ while (current.tryPathArguments() == null) {
+ Verify.verify(current instanceof StackedYangInstanceIdentifier);
+
+ final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) current;
+ stack.add(stacked);
+ current = stacked.getParent();
+ }
+
+ ret = new StackedPathArguments(current, Lists.reverse(stack));
+ pathArguments = ret;
+ }
+
+ return ret;
+ }
+
+ @Override
+ public Iterable<PathArgument> getReversePathArguments() {
+ Iterable<PathArgument> ret = reversePathArguments;
+ if (ret == null) {
+ ret = new StackedReversePathArguments(this);
+ reversePathArguments = ret;
+ }
+ return ret;
+ }
+
+ @Override
+ public PathArgument getLastPathArgument() {
+ return pathArgument;
+ }
+
+ @Override
+ Iterable<PathArgument> tryPathArguments() {
+ return pathArguments;
+ }
+
+ @Override
+ YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
+ // TODO: can we optimize this one?
+ return YangInstanceIdentifier.create(Iterables.skip(getPathArguments(), skipFromRoot));
+ }
+
+ @Override
+ boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
+ if (other instanceof StackedYangInstanceIdentifier) {
+ final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) other;
+ return pathArgument.equals(stacked.pathArgument) && parent.equals(stacked.parent);
+ } else {
+ return super.pathArgumentsEqual(other);
+ }
+ }
+
+ private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
+ inputStream.defaultReadObject();
+
+ final FixedYangInstanceIdentifier p = (FixedYangInstanceIdentifier) inputStream.readObject();
+ try {
+ PARENT_FIELD.set(this, p);
+ } catch (IllegalArgumentException | IllegalAccessException e) {
+ throw new IOException("Failed to set parent", e);
+ }
+ }
+
+ private void writeObject(final ObjectOutputStream outputStream) throws IOException {
+ outputStream.defaultWriteObject();
+
+ final FixedYangInstanceIdentifier p;
+ if (parent instanceof FixedYangInstanceIdentifier) {
+ p = (FixedYangInstanceIdentifier) parent;
+ } else {
+ p = FixedYangInstanceIdentifier.create(parent.getPathArguments(), parent.hashCode());
+ }
+ outputStream.writeObject(p);
+ }
+}
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
-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.Arrays;
-import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.concepts.Path;
*
* @see <a href="http://tools.ietf.org/html/rfc6020#section-9.13">RFC6020</a>
*/
-public final class YangInstanceIdentifier implements Path<YangInstanceIdentifier>, Immutable, Serializable {
+public abstract 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");
+ public static final YangInstanceIdentifier EMPTY = FixedYangInstanceIdentifier.EMPTY_INSTANCE;
+
private static final AtomicReferenceFieldUpdater<YangInstanceIdentifier, String> TOSTRINGCACHE_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(YangInstanceIdentifier.class, String.class, "toStringCache");
- private static final Field PATHARGUMENTS_FIELD;
+ private static final long serialVersionUID = 4L;
- 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;
- static {
- final Field f;
- try {
- f = YangInstanceIdentifier.class.getDeclaredField("pathArguments");
- } catch (NoSuchFieldException | SecurityException e) {
- throw new ExceptionInInitializerError(e);
- }
- f.setAccessible(true);
-
- PATHARGUMENTS_FIELD = f;
+ // Package-private to prevent outside subclassing
+ YangInstanceIdentifier(final int hash) {
+ this.hash = hash;
}
- private ImmutableList<PathArgument> getLegacyPath() {
- // Temporary variable saves a volatile read
- ImmutableList<PathArgument> ret = legacyPath;
- if (ret == null) {
- // We could have used a synchronized block, but the window is quite
- // small and worst that can happen is duplicate object construction.
- ret = ImmutableList.copyOf(pathArguments);
- LEGACYPATH_UPDATER.lazySet(this, ret);
- }
+ @Nonnull abstract YangInstanceIdentifier createRelativeIdentifier(int skipFromRoot);
+ @Nonnull abstract Iterable<PathArgument> tryPathArguments();
- return ret;
- }
+ /**
+ * Check if this instance identifier has empty path arguments, e.g. it is
+ * empty and corresponds to {@link #EMPTY}.
+ *
+ * @return True if this instance identifier is empty, false otherwise.
+ */
+ public abstract boolean isEmpty();
+
+ /**
+ * Return the conceptual parent {@link YangInstanceIdentifier}, which has
+ * one item less in {@link #getPathArguments()}.
+ *
+ * @return Parent {@link YangInstanceIdentifier}, or null if this is object is {@link #EMPTY}.
+ */
+ @Nullable public abstract YangInstanceIdentifier getParent();
/**
* Returns a list of path arguments.
* @return Immutable list of path arguments.
*/
@Deprecated
- public List<PathArgument> getPath() {
- return getLegacyPath();
- }
+ public abstract List<PathArgument> getPath();
/**
* Returns an ordered iteration of path arguments.
*
* @return Immutable iteration of path arguments.
*/
- public Iterable<PathArgument> getPathArguments() {
- return pathArguments;
- }
+ public abstract Iterable<PathArgument> getPathArguments();
/**
* Returns an iterable of path arguments in reverse order. This is useful
*
* @return Immutable iterable of path arguments in reverse order.
*/
- public Iterable<PathArgument> getReversePathArguments() {
- return getLegacyPath().reverse();
- }
+ public abstract Iterable<PathArgument> getReversePathArguments();
/**
* Returns the last PathArgument. This is equivalent of iterating
*
* @return The last past argument, or null if there are no PathArguments.
*/
- public PathArgument getLastPathArgument() {
- return Iterables.getFirst(getReversePathArguments(), null);
- }
+ public abstract PathArgument getLastPathArgument();
- YangInstanceIdentifier(final Iterable<PathArgument> path, final int hash) {
- this.pathArguments = Preconditions.checkNotNull(path, "path must not be null.");
- this.hash = hash;
- }
+ public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
+ if (Iterables.isEmpty(path)) {
+ return EMPTY;
+ }
- 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.build());
- }
-
- public static YangInstanceIdentifier create(final Iterable<? extends PathArgument> path) {
- if (Iterables.isEmpty(path)) {
- return EMPTY;
- }
-
- return trustedCreate(ImmutableList.copyOf(path));
+ return FixedYangInstanceIdentifier.create(path, hash.build());
}
public static YangInstanceIdentifier create(final PathArgument... path) {
}
@Override
- public int hashCode() {
+ public final int hashCode() {
/*
* The caching is safe, since the object contract requires
* immutability of the object and all objects referenced from this
return hash;
}
+ boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
+ return Iterables.elementsEqual(getPathArguments(), other.getPathArguments());
+ }
+
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
+ if (!(obj instanceof YangInstanceIdentifier)) {
return false;
}
YangInstanceIdentifier other = (YangInstanceIdentifier) obj;
if (this.hashCode() != obj.hashCode()) {
return false;
}
- return Iterables.elementsEqual(pathArguments, other.pathArguments);
+
+ return pathArgumentsEqual(other);
}
/**
* @param name QName of {@link NodeIdentifier}
* @return Instance Identifier with additional path argument added to the end.
*/
- public YangInstanceIdentifier node(final QName name) {
+ public final YangInstanceIdentifier node(final QName name) {
return node(new NodeIdentifier(name));
}
* @param arg Path argument which should be added to the end
* @return Instance Identifier with additional path argument added to the end.
*/
- public YangInstanceIdentifier node(final PathArgument arg) {
- return new YangInstanceIdentifier(Iterables.concat(pathArguments, Collections.singleton(arg)), HashCodeBuilder.nextHashCode(hash, arg));
+ public final YangInstanceIdentifier node(final PathArgument arg) {
+ return new StackedYangInstanceIdentifier(this, arg, HashCodeBuilder.nextHashCode(hash, arg));
}
/**
* the specified parent is not in fact an ancestor of this object.
*/
public Optional<YangInstanceIdentifier> relativeTo(final YangInstanceIdentifier ancestor) {
- final Iterator<?> lit = pathArguments.iterator();
- final Iterator<?> oit = ancestor.pathArguments.iterator();
+ final Iterator<?> lit = getPathArguments().iterator();
+ final Iterator<?> oit = ancestor.getPathArguments().iterator();
int common = 0;
while (oit.hasNext()) {
if (!lit.hasNext()) {
return Optional.of(EMPTY);
}
- return Optional.of(trustedCreate(Iterables.skip(pathArguments, common)));
+
+ return Optional.of(createRelativeIdentifier(common));
}
private static int hashCode(final Object value) {
}
@Override
- public boolean contains(final YangInstanceIdentifier other) {
+ public final boolean contains(final YangInstanceIdentifier other) {
Preconditions.checkArgument(other != null, "other should not be null");
- final Iterator<?> lit = pathArguments.iterator();
- final Iterator<?> oit = other.pathArguments.iterator();
+ final Iterator<?> lit = getPathArguments().iterator();
+ final Iterator<?> oit = other.getPathArguments().iterator();
while (lit.hasNext()) {
if (!oit.hasNext()) {
}
@Override
- public String toString() {
+ public final String toString() {
/*
* The toStringCache is safe, since the object contract requires
* immutability of the object and all objects referenced from this
}
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);
- }
}
*/
package org.opendaylight.yangtools.yang.data.api;
-import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import java.util.ArrayList;
import java.util.List;
@Override
public YangInstanceIdentifier build() {
- return new YangInstanceIdentifier(ImmutableList.copyOf(path), hash.build());
+ return FixedYangInstanceIdentifier.create(path, hash.build());
}
}
\ No newline at end of file