)Iterables.getLast(id.getPath())).getKey();
return ret;
}
/**
* Path argument of {@link InstanceIdentifier}.
*
* Interface which implementations are used as path components of the
* path in overall data tree.
*
*/
public interface PathArgument {
Class extends DataObject> getType();
}
/**
* An Item represents an object that probably is only one of it's kind. For example a Nodes object is only one of
* a kind. In YANG terms this would probably represent a container.
*
* @param
*/
public static final class Item implements PathArgument {
private final Class type;
public Item(Class type) {
this.type = type;
}
@Override
public Class getType() {
return type;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Item> other = (Item>) obj;
if (type == null) {
if (other.type != null)
return false;
} else if (!type.equals(other.type))
return false;
return true;
}
@Override
public String toString() {
return type.getName();
}
}
/**
* An IdentifiableItem represents a object that is usually present in a collection and can be identified uniquely
* by a key. In YANG terms this would probably represent an item in a list.
*
* @param An object that is identifiable by an identifier
* @param The identifier of the object
*/
public static final class IdentifiableItem & DataObject, T extends Identifier> implements
PathArgument {
private final T key;
private final Class type;
public IdentifiableItem(Class type, T key) {
if (type == null)
throw new IllegalArgumentException("Type must not be null.");
if (key == null)
throw new IllegalArgumentException("Key must not be null.");
this.type = type;
this.key = key;
}
public T getKey() {
return this.key;
}
@Override
public Class getType() {
return this.type;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (obj.hashCode() != hashCode()) {
return false;
}
if (!(obj instanceof IdentifiableItem, ?>)) {
return false;
}
IdentifiableItem, ?> foreign = (IdentifiableItem, ?>) obj;
return key.equals(foreign.getKey());
}
@Override
public int hashCode() {
return key.hashCode();
}
@Override
public String toString() {
return type.getName() + "[key=" + key + "]";
}
}
public interface InstanceIdentifierBuilder extends Builder> {
/**
* @deprecated use {@link child(Class)} or {@link augmentation(Class)} instead.
*/
@Deprecated
InstanceIdentifierBuilder node(Class container);
/**
* @deprecated use {@link child(Class,Identifier)} or {@link augmentation(Class,Identifier)} instead.
*/
@Deprecated
& DataObject, K extends Identifier> InstanceIdentifierBuilder node(
Class listItem, K listKey);
/**
* Append the specified container as a child of the current InstanceIdentifier referenced by the builder.
*
* This method should be used when you want to build an instance identifier by appending top-level
* elements
*
* Example,
*
* InstanceIdentifier.builder().child(Nodes.class).build();
*
*
*
* NOTE :- The above example is only for illustration purposes InstanceIdentifier.builder() has been deprecated
* and should not be used. Use InstanceIdentifier.builder(Nodes.class) instead
*
* @param container
* @param
* @return
*/
> InstanceIdentifierBuilder child(Class container);
/**
* Append the specified listItem as a child of the current InstanceIdentifier referenced by the builder.
*
* This method should be used when you want to build an instance identifier by appending a specific list element
* to the identifier
*
* @param listItem
* @param listKey
* @param
* @param
* @return
*/
& ChildOf super T>, K extends Identifier> InstanceIdentifierBuilder child(
Class listItem, K listKey);
/**
* Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
* the builder
*
* @param container
* @param
* @return
*/
> InstanceIdentifierBuilder augmentation(Class container);
/**
* Build the instance identifier.
*
* @return
*/
InstanceIdentifier build();
}
/**
* @deprecated use {@link builder(Class)} or {@link builder(Class,Identifier)} instead.
*/
@Deprecated
@SuppressWarnings("rawtypes")
public static InstanceIdentifierBuilder> builder() {
return new BuilderImpl();
}
/**
* Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier as specified by container
*
* @param container
* @param
* @return
*/
public static > InstanceIdentifierBuilder builder(Class container) {
return new BuilderImpl().addNode(container);
}
/**
* Create an InstanceIdentifierBuilder for a specific type of InstanceIdentifier which represents an IdentifiableItem
*
* @param listItem
* @param listKey
* @param
* @param
* @return
*/
public static & ChildOf extends DataRoot>, K extends Identifier> InstanceIdentifierBuilder builder(
Class listItem, K listKey) {
return new BuilderImpl().addNode(listItem, listKey);
}
/**
* Create a new InstanceIdentifierBuilder given a base InstanceIdentifier
*
* @param basePath
* @param
* @return
*/
public static InstanceIdentifierBuilder builder(InstanceIdentifier basePath) {
return new BuilderImpl(basePath.path,basePath.targetType);
}
private static final class BuilderImpl implements InstanceIdentifierBuilder {
private final ImmutableList.Builder path;
private Class extends DataObject> target = null;
public BuilderImpl() {
this.path = ImmutableList.builder();
}
public BuilderImpl(List extends PathArgument> prefix,Class extends DataObject> target) {
this.path = ImmutableList.builder().addAll(prefix);
this.target = target;
}
@SuppressWarnings("unchecked")
private InstanceIdentifierBuilder addNode(Class container) {
target = container;
path.add(new Item(container));
return (InstanceIdentifierBuilder) this;
}
@SuppressWarnings("unchecked")
private , K extends Identifier> InstanceIdentifierBuilder addNode(
Class listItem, K listKey) {
target = listItem;
path.add(new IdentifiableItem(listItem, listKey));
return (InstanceIdentifierBuilder) this;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public InstanceIdentifier toInstance() {
return new InstanceIdentifier(path.build(), target);
}
@Override
public InstanceIdentifier build() {
return toInstance();
}
@Override
public InstanceIdentifierBuilder node(Class container) {
return addNode(container);
}
@Override
public , K extends Identifier> InstanceIdentifierBuilder node(
Class listItem, K listKey) {
return addNode(listItem, listKey);
}
@Override
public > InstanceIdentifierBuilder child(Class container) {
return addNode(container);
}
@Override
public & ChildOf super T>, K extends Identifier> InstanceIdentifierBuilder child(
Class listItem, K listKey) {
return addNode(listItem,listKey);
}
@Override
public > InstanceIdentifierBuilder augmentation(
Class container) {
return addNode(container);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
InstanceIdentifier> other = (InstanceIdentifier>) obj;
if (path == null) {
if (other.path != null) {
return false;
}
} else if (!path.equals(other.path)) {
return false;
}
return true;
}
/**
* The contains method checks if the other identifier is fully contained within the current identifier. It does this
* by looking at only the types of the path arguments and not by comparing the path arguments themselse.
* If you want to compare path arguments you must use containsWildcarded
*
* To illustrate here is an example which explains the working of this api.
*
* Let's say you have two instance identifiers as follows,
*
* this = /nodes/node/openflow:1
* other = /nodes/node/openflow:2
*
* then this.contains(other) will return true. To ensure that this and other are compared properly you must use
* containsWildcarded
*
* @param other
* @return
*/
@Override
public boolean contains(final InstanceIdentifier> other) {
if(other == null) {
throw new IllegalArgumentException("other should not be null");
}
final int localSize = this.path.size();
final List otherPath = other.getPath();
if(localSize > other.path.size()) {
return false;
}
for(int i = 0;i other) {
if(other == null) {
throw new IllegalArgumentException("other should not be null");
}
final int localSize = this.path.size();
final List otherPath = other.getPath();
if(localSize > other.path.size()) {
return false;
}
for(int i = 0;i && otherArgument instanceof IdentifiableItem, ?> && !localArgument.equals(otherPath.get(i))) {
return false;
}
}
return true;
}
public boolean isWildcarded() {
for(PathArgument pathArgument : path) {
if(Identifiable.class.isAssignableFrom(pathArgument.getType()) && !(pathArgument instanceof IdentifiableItem, ?>)) {
return true;
}
}
return false;
}
}