-/**
+/*
* Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
*/
package org.opendaylight.yangtools.yang.data.impl.leafref;
-import org.opendaylight.yangtools.concepts.Immutable;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
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;
public abstract class LeafRefPath implements Immutable {
}
@Override
- protected LeafRefPath createInstance(final LeafRefPath parent,
- final QNameWithPredicate qname) {
- return new AbsoluteLeafRefPath(parent, qname);
+ protected LeafRefPath createInstance(final LeafRefPath newParent,
+ final QNameWithPredicate newQname) {
+ return new AbsoluteLeafRefPath(newParent, newQname);
}
}
}
@Override
- protected LeafRefPath createInstance(final LeafRefPath parent,
- final QNameWithPredicate qname) {
- return new RelativeLeafRefPath(parent, qname);
+ protected LeafRefPath createInstance(final LeafRefPath newParent,
+ final QNameWithPredicate newQname) {
+ return new RelativeLeafRefPath(newParent, newQname);
}
}
@SuppressWarnings("rawtypes")
- private static final AtomicReferenceFieldUpdater<LeafRefPath, ImmutableList> LEGACYPATH_UPDATER = AtomicReferenceFieldUpdater
- .newUpdater(LeafRefPath.class, ImmutableList.class, "legacyPath");
+ private static final AtomicReferenceFieldUpdater<LeafRefPath, ImmutableList> LEGACYPATH_UPDATER =
+ AtomicReferenceFieldUpdater.newUpdater(LeafRefPath.class, ImmutableList.class, "legacyPath");
/**
* Shared instance of the conceptual root schema node.
private final int hash;
/**
- * Cached legacy path, filled-in when {@link #getPath()} or
+ * Cached legacy path, filled-in when {@link #getPathFromRoot()} or
* {@link #getPathTowardsRoot()} is invoked.
*/
private volatile ImmutableList<QNameWithPredicate> legacyPath;
return ret;
}
- /**
- * Returns the complete path to schema node.
- *
- * @return list of <code>QNameWithPredicate</code> instances which
- * represents complete path to schema node
- *
- * @deprecated Use {@link #getPathFromRoot()} instead.
- */
- @Deprecated
- public List<QNameWithPredicate> getPath() {
- return getLegacyPath();
- }
-
- protected LeafRefPath(final LeafRefPath parent,
- final QNameWithPredicate qname) {
+ protected LeafRefPath(final LeafRefPath parent, final QNameWithPredicate qname) {
this.parent = parent;
this.qname = qname;
- int h = parent == null ? 0 : parent.hashCode();
+ int hc = Objects.hashCode(parent);
if (qname != null) {
- h = h * 31 + qname.hashCode();
+ hc = hc * 31 + qname.hashCode();
}
- hash = h;
+ hash = hc;
}
/**
/**
* Create a new instance.
*
- * @param parent
+ * @param newParent
* Parent LeafRefPath
- * @param qname
+ * @param newQname
* next path element
* @return A new LeafRefPath instance
*/
- protected abstract LeafRefPath createInstance(LeafRefPath parent,
- QNameWithPredicate qname);
+ protected abstract LeafRefPath createInstance(LeafRefPath newParent,
+ QNameWithPredicate newQname);
/**
* Create a child path based on concatenation of this path and a relative
return this;
}
- LeafRefPath parent = this;
- for (QNameWithPredicate qname : relative) {
- parent = parent.createInstance(parent, qname);
+ LeafRefPath newParent = this;
+ for (QNameWithPredicate relativeQname : relative) {
+ newParent = newParent.createInstance(newParent, relativeQname);
}
- return parent;
+ return newParent;
}
/**
* @return A new child path
*/
public LeafRefPath createChild(final LeafRefPath relative) {
- Preconditions.checkArgument(!relative.isAbsolute(),
- "Child creation requires relative path");
+ checkArgument(!relative.isAbsolute(), "Child creation requires relative path");
- LeafRefPath parent = this;
- for (QNameWithPredicate qname : relative.getPathFromRoot()) {
- parent = parent.createInstance(parent, qname);
+ LeafRefPath newParent = this;
+ for (QNameWithPredicate relativeQname : relative.getPathFromRoot()) {
+ newParent = newParent.createInstance(newParent, relativeQname);
}
- return parent;
+ return newParent;
}
/**
* the schema node towards the root.
*/
public Iterable<QNameWithPredicate> getPathTowardsRoot() {
- return new Iterable<QNameWithPredicate>() {
+ return () -> new Iterator<QNameWithPredicate>() {
+ private LeafRefPath current = LeafRefPath.this;
+
@Override
- public Iterator<QNameWithPredicate> iterator() {
- return new Iterator<QNameWithPredicate>() {
- private LeafRefPath current = LeafRefPath.this;
-
- @Override
- public boolean hasNext() {
- return current.parent != null;
- }
-
- @Override
- public QNameWithPredicate next() {
- if (current.parent != null) {
- final QNameWithPredicate 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 QNameWithPredicate next() {
+ if (current.parent == null) {
+ throw new NoSuchElementException("No more elements available");
+ }
+
+ final QNameWithPredicate ret = current.qname;
+ current = current.parent;
+ return ret;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("Component removal not supported");
}
};
}
return false;
}
final LeafRefPath other = (LeafRefPath) 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 String toString() {
StringBuilder sb = new StringBuilder();
- Iterable<QNameWithPredicate> pathFromRoot = this.getPathFromRoot();
-
sb.append(isAbsolute() ? "Absolute path:" : "Relative path:");
- for (QNameWithPredicate qName : pathFromRoot) {
- sb.append("/" + qName);
+ for (QNameWithPredicate qnameWithPredicate : getPathFromRoot()) {
+ sb.append('/').append(qnameWithPredicate);
}
return sb.toString();
-
}
// @Override