*/
package org.opendaylight.yangtools.yang.model.api.stmt;
-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.ImmutableList;
import com.google.common.collect.Iterables;
+import com.google.common.collect.UnmodifiableIterator;
import java.util.Arrays;
-import java.util.Iterator;
-import java.util.List;
import java.util.NoSuchElementException;
+import java.util.Objects;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
/**
- * Represents unique path to the every schema node inside the schema node identifier
- * namespace.
- *
+ * Represents unique path to the every schema node inside the schema node identifier namespace.
*/
public abstract class SchemaNodeIdentifier implements Immutable {
+
/**
* An absolute schema node identifier.
*/
@Override
protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
- return new Absolute(parent, qname);
+ return new Absolute(parent, Preconditions.checkNotNull(qname));
}
}
/**
* A relative schema node identifier.
*/
- public static class Relative extends SchemaNodeIdentifier {
+ public static final class Relative extends SchemaNodeIdentifier {
private Relative(final SchemaNodeIdentifier parent, final QName qname) {
super(parent, qname);
}
@Override
protected SchemaNodeIdentifier createInstance(final SchemaNodeIdentifier parent, final QName qname) {
- return new Relative(parent, qname);
+ return new Relative(parent, Preconditions.checkNotNull(qname));
}
}
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, ImmutableList> LEGACYPATH_UPDATER =
AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, ImmutableList.class, "legacyPath");
-
+ private static final AtomicReferenceFieldUpdater<SchemaNodeIdentifier, SchemaPath> SCHEMAPATH_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(SchemaNodeIdentifier.class, SchemaPath.class, "schemaPath");
/**
* Shared instance of the conceptual root schema node.
*/
*/
private volatile ImmutableList<QName> legacyPath;
- private ImmutableList<QName> getLegacyPath() {
- ImmutableList<QName> ret = legacyPath;
- if (ret == null) {
- ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
- LEGACYPATH_UPDATER.lazySet(this, ret);
- }
-
- return ret;
- }
-
/**
- * Returns the complete path to schema node.
- *
- * @return list of <code>QName</code> instances which represents complete
- * path to schema node
- *
- * @deprecated Use {@link #getPathFromRoot()} instead.
+ * Cached SchemaPath.
*/
- @Deprecated
- public List<QName> getPath() {
- return getLegacyPath();
- }
+ private volatile SchemaPath schemaPath;
protected SchemaNodeIdentifier(final SchemaNodeIdentifier parent, final QName qname) {
this.parent = parent;
this.qname = qname;
- int h = parent == null ? 0 : parent.hashCode();
+ int tmp = Objects.hashCode(parent);
if (qname != null) {
- h = h * 31 + qname.hashCode();
+ tmp = tmp * 31 + qname.hashCode();
}
- hash = h;
+ hash = tmp;
+ }
+
+ private ImmutableList<QName> getLegacyPath() {
+ ImmutableList<QName> ret = legacyPath;
+ if (ret == null) {
+ ret = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
+ LEGACYPATH_UPDATER.lazySet(this, ret);
+ }
+
+ return ret;
}
/**
* boolean value which specifies if the path is absolute or
* relative
*
- * @return A SchemaPath instance.
+ * @return A SchemaNodeIdentifier instance.
*/
public static SchemaNodeIdentifier create(final Iterable<QName> path, final boolean absolute) {
final SchemaNodeIdentifier parent = absolute ? ROOT : SAME;
return this;
}
- SchemaNodeIdentifier parent = this;
+ SchemaNodeIdentifier parentNode = this;
for (QName qname : relative) {
- parent = parent.createInstance(parent, qname);
+ parentNode = parentNode.createInstance(parentNode, qname);
}
- return parent;
+ return parentNode;
}
/**
public SchemaNodeIdentifier createChild(final SchemaNodeIdentifier relative) {
Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
- SchemaNodeIdentifier parent = this;
+ SchemaNodeIdentifier parentNode = this;
for (QName qname : relative.getPathFromRoot()) {
- parent = parent.createInstance(parent, qname);
+ parentNode = parentNode.createInstance(parentNode, qname);
}
- return parent;
+ return parentNode;
}
/**
* path from the schema node towards the root.
*/
public Iterable<QName> getPathTowardsRoot() {
- return new Iterable<QName>() {
+ return () -> new UnmodifiableIterator<QName>() {
+ private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
+
+ @Override
+ public boolean hasNext() {
+ return current.parent != null;
+ }
+
@Override
- public Iterator<QName> iterator() {
- return new Iterator<QName>() {
- private SchemaNodeIdentifier current = SchemaNodeIdentifier.this;
-
- @Override
- public boolean hasNext() {
- return current.parent != null;
- }
-
- @Override
- public QName next() {
- if (current.parent != null) {
- final QName ret = current.qname;
- current = current.parent;
- return ret;
- } else {
- throw new NoSuchElementException("No more elements available");
- }
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Component removal not supported");
- }
- };
+ public QName next() {
+ if (current.parent != null) {
+ final QName ret = current.qname;
+ current = current.parent;
+ return ret;
+ } else {
+ throw new NoSuchElementException("No more elements available");
+ }
}
};
}
return qname;
}
+ private SchemaPath createSchemaPath() {
+ final SchemaPath newPath;
+ if (parent == null) {
+ final SchemaPath parentPath = isAbsolute() ? SchemaPath.ROOT : SchemaPath.SAME;
+ newPath = qname == null ? parentPath : parentPath.createChild(qname);
+ } else {
+ newPath = parent.asSchemaPath().createChild(qname);
+ }
+
+ return SCHEMAPATH_UPDATER.compareAndSet(this, null, newPath) ? newPath : schemaPath;
+ }
+
+ /**
+ * Create the {@link SchemaPath} equivalent of this identifier.
+ *
+ * @return SchemaPath equivalent.
+ */
+ public final SchemaPath asSchemaPath() {
+ final SchemaPath ret = schemaPath;
+ return ret != null ? ret : createSchemaPath();
+ }
+
/**
* Describes whether schema node identifier is|isn't absolute.
*
return false;
}
final SchemaNodeIdentifier other = (SchemaNodeIdentifier) obj;
-
- if (qname != null) {
- if (!qname.equals(other.qname)) {
- return false;
- }
- } else {
- if (other.qname != null) {
- return false;
- }
- }
-
- if (parent == null) {
- return other.parent == null;
- }
- return parent.equals(other.parent);
+ return Objects.equals(qname, other.qname) && Objects.equals(parent, other.parent);
}
@Override
public final String toString() {
- return addToStringAttributes(Objects.toStringHelper(this)).toString();
+ return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
}
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {