import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+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;
return new InstanceIdentifier(ImmutableList.<PathArgument>builder().addAll(path).add(arg).build());
}
+ /**
+ * Get the relative path from an ancestor. This method attempts to perform the reverse
+ * of concatenating a base (ancestor) and a path.
+ *
+ * @param ancestor Ancestor against which the relative path should be calculated
+ * @return This object's relative path from parent, or Optional.absent() if the
+ * specified parent is not in fact an ancestor of this object.
+ */
+ public Optional<InstanceIdentifier> relativeTo(final InstanceIdentifier ancestor) {
+ if (ancestor.contains(this)) {
+ final int common = ancestor.path.size();
+ return Optional.of(new InstanceIdentifier(path.subList(common, path.size())));
+ } else {
+ return Optional.absent();
+ }
+ }
+
// Static factories & helpers
public static InstanceIdentifier of(final QName name) {
/**
* Simple path argument identifying a {@link ContainerNode} or {@link LeafNode} leaf
- * overal data tree.
- *
+ * overall data tree.
*/
- public static final class NodeIdentifier implements PathArgument {
-
- /**
- *
- */
+ public static final class NodeIdentifier implements PathArgument, Comparable<NodeIdentifier> {
private static final long serialVersionUID = -2255888212390871347L;
-
private final QName nodeType;
public NodeIdentifier(final QName node) {
- this.nodeType = node;
+ this.nodeType = Preconditions.checkNotNull(node);
}
@Override
@Override
public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((nodeType == null) ? 0 : nodeType.hashCode());
- return result;
+ return 31 + nodeType.hashCode();
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- NodeIdentifier other = (NodeIdentifier) obj;
- if (nodeType == null) {
- if (other.nodeType != null) {
- return false;
- }
- } else if (!nodeType.equals(other.nodeType)) {
- return false;
- }
- return true;
+ return true;
+ }
+ if (!(obj instanceof NodeIdentifier)) {
+ return false;
+ }
+ final NodeIdentifier other = (NodeIdentifier) obj;
+ return nodeType.equals(other.nodeType);
}
@Override
public String toString() {
return nodeType.toString();
}
+
+ @Override
+ public int compareTo(final NodeIdentifier o) {
+ return nodeType.compareTo(o.nodeType);
+ }
}
/**
- *
* Composite path argument identifying a {@link MapEntryNode} leaf
- * overal data tree.
- *
+ * overall data tree.
*/
public static final class NodeIdentifierWithPredicates implements PathArgument {
-
- /**
- *
- */
private static final long serialVersionUID = -4787195606494761540L;
private final QName nodeType;
private final Map<QName, Object> keyValues;
public NodeIdentifierWithPredicates(final QName node, final Map<QName, Object> keyValues) {
- this.nodeType = node;
+ this.nodeType = Preconditions.checkNotNull(node);
this.keyValues = ImmutableMap.copyOf(keyValues);
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
- return true;
- }
+ return true;
+ }
if (obj == null) {
- return false;
- }
+ return false;
+ }
if (getClass() != obj.getClass()) {
- return false;
- }
+ return false;
+ }
NodeIdentifierWithPredicates other = (NodeIdentifierWithPredicates) obj;
if (keyValues == null) {
if (other.keyValues != null) {
- return false;
- }
+ return false;
+ }
} else if (!keyValues.equals(other.keyValues)) {
- return false;
- }
+ return false;
+ }
if (nodeType == null) {
if (other.nodeType != null) {
- return false;
- }
+ return false;
+ }
} else if (!nodeType.equals(other.nodeType)) {
- return false;
- }
+ return false;
+ }
return true;
}
/**
* Simple path argument identifying a {@link LeafSetEntryNode} leaf
- * overal data tree.
- *
+ * overall data tree.
*/
public static final class NodeWithValue implements PathArgument {
-
- /**
- *
- * Composite path argument identifying a {@link AugmentationNode} leaf
- * overal data tree.
- *
- */
private static final long serialVersionUID = -3637456085341738431L;
private final QName nodeType;
private final Object value;
public NodeWithValue(final QName node, final Object value) {
- this.nodeType = node;
+ this.nodeType = Preconditions.checkNotNull(node);
this.value = value;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
- return true;
- }
+ return true;
+ }
if (obj == null) {
- return false;
- }
+ return false;
+ }
if (getClass() != obj.getClass()) {
- return false;
- }
+ return false;
+ }
NodeWithValue other = (NodeWithValue) obj;
if (value == null) {
if (other.value != null) {
- return false;
- }
+ return false;
+ }
} else if (!value.equals(other.value)) {
- return false;
- }
+ return false;
+ }
if (nodeType == null) {
if (other.nodeType != null) {
- return false;
- }
+ return false;
+ }
} else if (!nodeType.equals(other.nodeType)) {
- return false;
- }
+ return false;
+ }
return true;
}
}
-
+ /**
+ * Composite path argument identifying a {@link AugmentationNode} leaf
+ * overall data tree.
+ */
public static final class AugmentationIdentifier implements PathArgument {
-
private static final long serialVersionUID = -8122335594681936939L;
private final ImmutableSet<QName> childNames;
@Override
public boolean equals(final Object o) {
if (this == o) {
- return true;
- }
+ return true;
+ }
if (!(o instanceof AugmentationIdentifier)) {
- return false;
- }
+ return false;
+ }
AugmentationIdentifier that = (AugmentationIdentifier) o;
if (!childNames.equals(that.childNames)) {
- return false;
- }
+ return false;
+ }
return true;
}
if (toStringCache != null) {
return toStringCache;
}
- StringBuilder builder = new StringBuilder();
+
+ final StringBuilder builder = new StringBuilder('/');
+ boolean first = true;
for (PathArgument argument : path) {
- builder.append("/");
+ if (first) {
+ first = false;
+ } else {
+ builder.append('/');
+ }
builder.append(argument.toString());
}
+
toStringCache = builder.toString();
return toStringCache;
}