-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.yangtools.yang.binding;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.opendaylight.yangtools.concepts.Builder;\r
-import org.opendaylight.yangtools.concepts.Immutable;\r
-import org.opendaylight.yangtools.concepts.Path;\r
-\r
-/**\r
- * Uniquely identifies data location in the overall of data tree \r
- * modeled by YANG.\r
- * \r
- * \r
- */\r
-public final class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<?>>,Immutable {\r
-\r
- private final List<PathArgument> path;\r
- private final Class<T> targetType;\r
- \r
- public InstanceIdentifier(Class<T> type) {\r
- path = Collections.<PathArgument> singletonList(new Item<>(type));\r
- this.targetType = type;\r
- }\r
-\r
- public InstanceIdentifier(List<PathArgument> path, Class<T> type) {\r
- this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));\r
- this.targetType = type;\r
- }\r
-\r
- /**\r
- * \r
- * @return path\r
- */\r
- public List<PathArgument> getPath() {\r
- return this.path;\r
- }\r
-\r
- public Class<T> getTargetType() {\r
- return this.targetType;\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return "InstanceIdentifier [path=" + path + "]";\r
- }\r
-\r
- /**\r
- * Path argument of {@link InstanceIdentifier}.\r
- * <p>\r
- * Interface which implementations are used as path components of the\r
- * path in overall data tree.\r
- *\r
- */\r
- public interface PathArgument {\r
-\r
- Class<? extends DataObject> getType();\r
-\r
- }\r
-\r
- public static final class Item<T extends DataObject> implements PathArgument {\r
- private final Class<T> type;\r
-\r
- public Item(Class<T> type) {\r
- this.type = type;\r
- }\r
-\r
- public Class<T> getType() {\r
- return type;\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result + ((type == null) ? 0 : type.hashCode());\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- Item<?> other = (Item<?>) obj;\r
- if (type == null) {\r
- if (other.type != null)\r
- return false;\r
- } else if (!type.equals(other.type))\r
- return false;\r
- return true;\r
- }\r
- \r
- @Override\r
- public String toString() {\r
- return type.getName();\r
- }\r
- }\r
-\r
- public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements\r
- PathArgument {\r
-\r
- private final T key;\r
- private final Class<I> type;\r
-\r
- public IdentifiableItem(Class<I> type, T key) {\r
- if (type == null)\r
- throw new IllegalArgumentException("Type must not be null.");\r
- if (key == null)\r
- throw new IllegalArgumentException("Key must not be null.");\r
- this.type = type;\r
- this.key = key;\r
- }\r
-\r
- T getKey() {\r
- return this.key;\r
- }\r
-\r
- @Override\r
- public Class<I> getType() {\r
- return this.type;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (obj == null) {\r
- return false;\r
- }\r
- if (obj.hashCode() != hashCode()) {\r
- return false;\r
- }\r
- if (!(obj instanceof IdentifiableItem<?, ?>)) {\r
- return false;\r
- }\r
- IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;\r
- return key.equals(foreign.getKey());\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- return key.hashCode();\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return type.getName() + "[key=" + key + "]";\r
- }\r
- }\r
-\r
- public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {\r
-\r
- <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);\r
-\r
- <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(\r
- Class<N> listItem, K listKey);\r
-\r
- <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);\r
- \r
- <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(\r
- Class<N> listItem, K listKey);\r
-\r
- }\r
-\r
- @SuppressWarnings("rawtypes")\r
- public static InstanceIdentifierBuilder<?> builder() {\r
- return new BuilderImpl();\r
- }\r
-\r
- @SuppressWarnings({ "rawtypes", "unchecked" })\r
- public static InstanceIdentifierBuilder<?> builder(InstanceIdentifier<?> basePath) {\r
- return new BuilderImpl(basePath.path,basePath.targetType);\r
- }\r
-\r
- private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {\r
-\r
- private List<PathArgument> path;\r
- private Class<? extends DataObject> target = null;\r
-\r
- public BuilderImpl() {\r
- this.path = new ArrayList<>();\r
- }\r
- \r
-\r
- public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {\r
- this.path = new ArrayList<>(prefix);\r
- this.target = target;\r
- }\r
-\r
- @SuppressWarnings({ "unchecked", "rawtypes" })\r
- @Override\r
- public InstanceIdentifier<T> toInstance() {\r
- List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));\r
- return new InstanceIdentifier(immutablePath, target);\r
- }\r
-\r
- @Override\r
- @SuppressWarnings("unchecked")\r
- public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {\r
- target = container;\r
- path.add(new Item<N>(container));\r
- return (InstanceIdentifierBuilder<N>) this;\r
- }\r
-\r
- @Override\r
- @SuppressWarnings("unchecked")\r
- public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(\r
- Class<N> listItem, K listKey) {\r
- target = listItem;\r
- path.add(new IdentifiableItem<N, K>(listItem, listKey));\r
- return (InstanceIdentifierBuilder<N>) this;\r
- }\r
- \r
- @Override\r
- public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {\r
- return node(container);\r
- }\r
- \r
- @Override\r
- public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(\r
- Class<N> listItem, K listKey) {\r
- return node(listItem,listKey);\r
- }\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result + ((path == null) ? 0 : path.hashCode());\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj) {\r
- return true;\r
- }\r
- if (obj == null) {\r
- return false;\r
- }\r
- if (getClass() != obj.getClass()) {\r
- return false;\r
- }\r
- InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;\r
- if (path == null) {\r
- if (other.path != null) {\r
- return false;\r
- }\r
- } else if (!path.equals(other.path)) {\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- @Override\r
- public boolean contains(final InstanceIdentifier<?> other) {\r
- if(other == null) {\r
- throw new IllegalArgumentException("other should not be null");\r
- }\r
- final int localSize = this.path.size();\r
- final List<PathArgument> otherPath = other.getPath();\r
- if(localSize > other.path.size()) {\r
- return false;\r
- }\r
- for(int i = 0;i<localSize;i++ ) {\r
- if(!path.get(i).equals(otherPath.get(i))) {\r
- return false;\r
- }\r
- }\r
- \r
- return true;\r
- }\r
-}\r
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.binding;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.concepts.Path;
+
+/**
+ * Uniquely identifies data location in the overall of data tree
+ * modeled by YANG.
+ *
+ *
+ */
+public final class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<?>>,Immutable {
+
+ private final List<PathArgument> path;
+ private final Class<T> targetType;
+
+ public InstanceIdentifier(Class<T> type) {
+ path = Collections.<PathArgument> singletonList(new Item<>(type));
+ this.targetType = type;
+ }
+
+ public InstanceIdentifier(List<PathArgument> path, Class<T> type) {
+ this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));
+ this.targetType = type;
+ }
+
+ /**
+ *
+ * @return path
+ */
+ public List<PathArgument> getPath() {
+ return this.path;
+ }
+
+ public Class<T> getTargetType() {
+ return this.targetType;
+ }
+
+ @Override
+ public String toString() {
+ return "InstanceIdentifier [path=" + path + "]";
+ }
+
+ /**
+ * Path argument of {@link InstanceIdentifier}.
+ * <p>
+ * Interface which implementations are used as path components of the
+ * path in overall data tree.
+ *
+ */
+ public interface PathArgument {
+
+ Class<? extends DataObject> getType();
+
+ }
+
+ public static final class Item<T extends DataObject> implements PathArgument {
+ private final Class<T> type;
+
+ public Item(Class<T> type) {
+ this.type = type;
+ }
+
+ public Class<T> 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();
+ }
+ }
+
+ public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
+ PathArgument {
+
+ private final T key;
+ private final Class<I> type;
+
+ public IdentifiableItem(Class<I> 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<I> 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<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
+
+ <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);
+
+ <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
+ Class<N> listItem, K listKey);
+
+ <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
+
+ <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
+ Class<N> listItem, K listKey);
+
+ }
+
+ @SuppressWarnings("rawtypes")
+ public static InstanceIdentifierBuilder<?> builder() {
+ return new BuilderImpl();
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public static InstanceIdentifierBuilder<?> builder(InstanceIdentifier<?> basePath) {
+ return new BuilderImpl(basePath.path,basePath.targetType);
+ }
+
+ private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
+
+ private List<PathArgument> path;
+ private Class<? extends DataObject> target = null;
+
+ public BuilderImpl() {
+ this.path = new ArrayList<>();
+ }
+
+
+ public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
+ this.path = new ArrayList<>(prefix);
+ this.target = target;
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ @Override
+ public InstanceIdentifier<T> toInstance() {
+ List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
+ return new InstanceIdentifier(immutablePath, target);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {
+ target = container;
+ path.add(new Item<N>(container));
+ return (InstanceIdentifierBuilder<N>) this;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
+ Class<N> listItem, K listKey) {
+ target = listItem;
+ path.add(new IdentifiableItem<N, K>(listItem, listKey));
+ return (InstanceIdentifierBuilder<N>) this;
+ }
+
+ @Override
+ public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
+ return node(container);
+ }
+
+ @Override
+ public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
+ Class<N> listItem, K listKey) {
+ return node(listItem,listKey);
+ }
+ }
+
+ @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;
+ }
+
+ @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<PathArgument> otherPath = other.getPath();
+ if(localSize > other.path.size()) {
+ return false;
+ }
+ for(int i = 0;i<localSize;i++ ) {
+ if(!path.get(i).equals(otherPath.get(i))) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}