2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.yangtools.yang.binding;
\r
10 import java.util.ArrayList;
\r
11 import java.util.Collections;
\r
12 import java.util.List;
\r
14 import org.opendaylight.yangtools.concepts.Builder;
\r
15 import org.opendaylight.yangtools.concepts.Immutable;
\r
16 import org.opendaylight.yangtools.concepts.Path;
\r
19 * Uniquely identifies data location in the overall of data tree
\r
24 public final class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<?>>,Immutable {
\r
26 private final List<PathArgument> path;
\r
27 private final Class<T> targetType;
\r
29 public InstanceIdentifier(Class<T> type) {
\r
30 path = Collections.<PathArgument> singletonList(new Item<>(type));
\r
31 this.targetType = type;
\r
34 public InstanceIdentifier(List<PathArgument> path, Class<T> type) {
\r
35 this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));
\r
36 this.targetType = type;
\r
43 public List<PathArgument> getPath() {
\r
47 public Class<T> getTargetType() {
\r
48 return this.targetType;
\r
52 public String toString() {
\r
53 return "InstanceIdentifier [path=" + path + "]";
\r
57 * Path argument of {@link InstanceIdentifier}.
\r
59 * Interface which implementations are used as path components of the
\r
60 * path in overall data tree.
\r
63 public interface PathArgument {
\r
65 Class<? extends DataObject> getType();
\r
69 public static final class Item<T extends DataObject> implements PathArgument {
\r
70 private final Class<T> type;
\r
72 public Item(Class<T> type) {
\r
76 public Class<T> getType() {
\r
81 public int hashCode() {
\r
82 final int prime = 31;
\r
84 result = prime * result + ((type == null) ? 0 : type.hashCode());
\r
89 public boolean equals(Object obj) {
\r
94 if (getClass() != obj.getClass())
\r
96 Item<?> other = (Item<?>) obj;
\r
98 if (other.type != null)
\r
100 } else if (!type.equals(other.type))
\r
106 public String toString() {
\r
107 return type.getName();
\r
111 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
\r
114 private final T key;
\r
115 private final Class<I> type;
\r
117 public IdentifiableItem(Class<I> type, T key) {
\r
119 throw new IllegalArgumentException("Type must not be null.");
\r
121 throw new IllegalArgumentException("Key must not be null.");
\r
131 public Class<I> getType() {
\r
136 public boolean equals(Object obj) {
\r
140 if (obj.hashCode() != hashCode()) {
\r
143 if (!(obj instanceof IdentifiableItem<?, ?>)) {
\r
146 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
\r
147 return key.equals(foreign.getKey());
\r
151 public int hashCode() {
\r
152 return key.hashCode();
\r
156 public String toString() {
\r
157 return type.getName() + "[key=" + key + "]";
\r
161 public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
\r
163 <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);
\r
165 <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
\r
166 Class<N> listItem, K listKey);
\r
168 <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
\r
170 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
\r
171 Class<N> listItem, K listKey);
\r
175 @SuppressWarnings("rawtypes")
\r
176 public static InstanceIdentifierBuilder<?> builder() {
\r
177 return new BuilderImpl();
\r
180 @SuppressWarnings({ "rawtypes", "unchecked" })
\r
181 public static InstanceIdentifierBuilder<?> builder(InstanceIdentifier<?> basePath) {
\r
182 return new BuilderImpl(basePath.path,basePath.targetType);
\r
185 private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
\r
187 private List<PathArgument> path;
\r
188 private Class<? extends DataObject> target = null;
\r
190 public BuilderImpl() {
\r
191 this.path = new ArrayList<>();
\r
195 public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
\r
196 this.path = new ArrayList<>(prefix);
\r
197 this.target = target;
\r
200 @SuppressWarnings({ "unchecked", "rawtypes" })
\r
202 public InstanceIdentifier<T> toInstance() {
\r
203 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
\r
204 return new InstanceIdentifier(immutablePath, target);
\r
208 @SuppressWarnings("unchecked")
\r
209 public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {
\r
210 target = container;
\r
211 path.add(new Item<N>(container));
\r
212 return (InstanceIdentifierBuilder<N>) this;
\r
216 @SuppressWarnings("unchecked")
\r
217 public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
\r
218 Class<N> listItem, K listKey) {
\r
220 path.add(new IdentifiableItem<N, K>(listItem, listKey));
\r
221 return (InstanceIdentifierBuilder<N>) this;
\r
225 public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
\r
226 return node(container);
\r
230 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
\r
231 Class<N> listItem, K listKey) {
\r
232 return node(listItem,listKey);
\r
237 public int hashCode() {
\r
238 final int prime = 31;
\r
240 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
245 public boolean equals(Object obj) {
\r
252 if (getClass() != obj.getClass()) {
\r
255 InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
\r
256 if (path == null) {
\r
257 if (other.path != null) {
\r
260 } else if (!path.equals(other.path)) {
\r
267 public boolean contains(final InstanceIdentifier<?> other) {
\r
268 if(other == null) {
\r
269 throw new IllegalArgumentException("other should not be null");
\r
271 final int localSize = this.path.size();
\r
272 final List<PathArgument> otherPath = other.getPath();
\r
273 if(localSize > other.path.size()) {
\r
276 for(int i = 0;i<localSize;i++ ) {
\r
277 if(!path.get(i).equals(otherPath.get(i))) {
\r