*/
package org.opendaylight.yangtools.yang.model.api;
-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.Lists;
+import com.google.common.collect.UnmodifiableIterator;
+import java.util.ArrayList;
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;
* Represents unique path to the every node inside the module.
*/
public abstract class SchemaPath implements Immutable {
+
/**
* An absolute SchemaPath.
*/
@Override
protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
- return new AbsoluteSchemaPath(parent, qname);
+ return new AbsoluteSchemaPath(parent, Preconditions.checkNotNull(qname));
}
}
@Override
protected SchemaPath createInstance(final SchemaPath parent, final QName qname) {
- return new RelativeSchemaPath(parent, qname);
+ return new RelativeSchemaPath(parent, Preconditions.checkNotNull(qname));
}
}
+ @SuppressWarnings("rawtypes")
+ private static final AtomicReferenceFieldUpdater<SchemaPath, ImmutableList> LEGACYPATH_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(SchemaPath.class, ImmutableList.class, "legacyPath");
+
/**
* Shared instance of the conceptual root schema node.
*/
* Cached legacy path, filled-in when {@link #getPath()} or {@link #getPathTowardsRoot()}
* is invoked.
*/
- private ImmutableList<QName> legacyPath;
+ private volatile ImmutableList<QName> legacyPath;
+
+ SchemaPath(final SchemaPath parent, final QName qname) {
+ this.parent = parent;
+ this.qname = qname;
+
+ int tmp = Objects.hashCode(parent);
+ if (qname != null) {
+ tmp = tmp * 31 + qname.hashCode();
+ }
+
+ hash = tmp;
+ }
private ImmutableList<QName> getLegacyPath() {
- if (legacyPath == null) {
- legacyPath = ImmutableList.copyOf(getPathTowardsRoot()).reverse();
+ ImmutableList<QName> ret = legacyPath;
+ if (ret == null) {
+ final List<QName> tmp = new ArrayList<>();
+ for (QName qname : getPathTowardsRoot()) {
+ tmp.add(qname);
+ }
+ ret = ImmutableList.copyOf(Lists.reverse(tmp));
+ LEGACYPATH_UPDATER.lazySet(this, ret);
}
- return legacyPath;
+ return ret;
}
/**
return getLegacyPath();
}
- protected SchemaPath(final SchemaPath parent, final QName qname) {
- this.parent = parent;
- this.qname = qname;
-
- int h = parent == null ? 0 : parent.hashCode();
- if (qname != null) {
- h = h * 31 + qname.hashCode();
- }
-
- hash = h;
- }
-
/**
* Constructs new instance of this class with the concrete path.
*
/**
* Create a new instance.
*
- * @param path path from root
- * @param hash intended hash code
+ * @param parent Parent SchemaPath
+ * @param qname next path element
* @return A new SchemaPath instance
*/
protected abstract SchemaPath createInstance(SchemaPath parent, QName qname);
return this;
}
- SchemaPath parent = this;
+ SchemaPath parentPath = this;
for (QName qname : relative) {
- parent = parent.createInstance(parent, qname);
+ parentPath = parentPath.createInstance(parentPath, qname);
}
- return parent;
+ return parentPath;
}
/**
public SchemaPath createChild(final SchemaPath relative) {
Preconditions.checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
- SchemaPath parent = this;
+ SchemaPath parentPath = this;
for (QName qname : relative.getPathFromRoot()) {
- parent = parent.createInstance(parent, qname);
+ parentPath = parentPath.createInstance(parentPath, qname);
}
- return parent;
+ return parentPath;
}
/**
* path from the schema node towards the root.
*/
public Iterable<QName> getPathTowardsRoot() {
- return new Iterable<QName>() {
+ return () -> new UnmodifiableIterator<QName>() {
+ private SchemaPath current = SchemaPath.this;
+
@Override
- public Iterator<QName> iterator() {
- return new Iterator<QName>() {
- private SchemaPath current = SchemaPath.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 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");
+ }
}
};
}
return false;
}
final SchemaPath other = (SchemaPath) 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) {