2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.binding;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.yangtools.concepts.Builder;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.concepts.Path;
18 import com.google.common.collect.Iterables;
21 * Uniquely identifies data location in the overall of data tree
26 public final class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>,Immutable {
28 private final List<PathArgument> path;
29 private final Class<T> targetType;
31 public InstanceIdentifier(Class<T> type) {
32 path = Collections.<PathArgument> singletonList(new Item<>(type));
33 this.targetType = type;
36 public InstanceIdentifier(List<PathArgument> path, Class<T> type) {
37 this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));
38 this.targetType = type;
45 public List<PathArgument> getPath() {
46 return getPathArguments();
49 public List<PathArgument> getPathArguments() {
53 public Class<T> getTargetType() {
54 return this.targetType;
58 public String toString() {
59 return "InstanceIdentifier [path=" + path + "]";
63 * Return an instance identifier trimmed at the first occurrence of a
64 * specific component type.
66 * @param type component type
67 * @return trimmed instance identifier, or null if the component type
70 public <T extends DataObject> InstanceIdentifier<T> firstIdentifierOf(final Class<T> type) {
72 for (final PathArgument a : path) {
73 if (type.equals(a.getType())) {
74 return new InstanceIdentifier<>(path.subList(0, i), type);
84 * Return the key associated with the first component of specified type in
87 * @param listItem component type
88 * @param listKey component key type
89 * @return key associated with the component, or null if the component type
92 public <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
93 for (PathArgument i : path) {
94 if (listItem.equals(i.getType())) {
95 @SuppressWarnings("unchecked")
96 final K ret = ((IdentifiableItem<N, K>)i).getKey();
105 * Return the key associated with the last component of the specified identifier.
107 * @param id instance identifier
108 * @return key associated with the last component
110 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
111 @SuppressWarnings("unchecked")
112 final K ret = ((IdentifiableItem<N, K>)Iterables.getLast(id.getPath())).getKey();
117 * Path argument of {@link InstanceIdentifier}.
119 * Interface which implementations are used as path components of the
120 * path in overall data tree.
123 public interface PathArgument {
125 Class<? extends DataObject> getType();
129 public static final class Item<T extends DataObject> implements PathArgument {
130 private final Class<T> type;
132 public Item(Class<T> type) {
136 public Class<T> getType() {
141 public int hashCode() {
142 final int prime = 31;
144 result = prime * result + ((type == null) ? 0 : type.hashCode());
149 public boolean equals(Object obj) {
154 if (getClass() != obj.getClass())
156 Item<?> other = (Item<?>) obj;
158 if (other.type != null)
160 } else if (!type.equals(other.type))
166 public String toString() {
167 return type.getName();
171 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
175 private final Class<I> type;
177 public IdentifiableItem(Class<I> type, T key) {
179 throw new IllegalArgumentException("Type must not be null.");
181 throw new IllegalArgumentException("Key must not be null.");
191 public Class<I> getType() {
196 public boolean equals(Object obj) {
200 if (obj.hashCode() != hashCode()) {
203 if (!(obj instanceof IdentifiableItem<?, ?>)) {
206 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
207 return key.equals(foreign.getKey());
211 public int hashCode() {
212 return key.hashCode();
216 public String toString() {
217 return type.getName() + "[key=" + key + "]";
221 public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
223 * @deprecated use {@link child(Class)} or {@link augmentation(Class)} instead.
226 <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);
229 * @deprecated use {@link child(Class,Identifier)} or {@link augmentation(Class,Identifier)} instead.
232 <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
233 Class<N> listItem, K listKey);
235 <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
237 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
238 Class<N> listItem, K listKey);
240 <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(Class<N> container);
242 InstanceIdentifier<T> build();
247 * @deprecated use {@link builder(Class)} or {@link builder(Class,Identifier)} instead.
250 @SuppressWarnings("rawtypes")
251 public static InstanceIdentifierBuilder<?> builder() {
252 return new BuilderImpl();
255 public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(Class<T> container) {
256 return new BuilderImpl<T>().addNode(container);
259 public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>, K extends Identifier<N>> InstanceIdentifierBuilder<N> builder(
260 Class<N> listItem, K listKey) {
261 return new BuilderImpl<N>().addNode(listItem, listKey);
264 public static <T extends DataObject> InstanceIdentifierBuilder<T> builder(InstanceIdentifier<T> basePath) {
265 return new BuilderImpl<T>(basePath.path,basePath.targetType);
268 private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
270 private List<PathArgument> path;
271 private Class<? extends DataObject> target = null;
273 public BuilderImpl() {
274 this.path = new ArrayList<>();
277 public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
278 this.path = new ArrayList<>(prefix);
279 this.target = target;
282 @SuppressWarnings("unchecked")
283 private <N extends DataObject> InstanceIdentifierBuilder<N> addNode(Class<N> container) {
285 path.add(new Item<N>(container));
286 return (InstanceIdentifierBuilder<N>) this;
289 @SuppressWarnings("unchecked")
290 private <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> addNode(
291 Class<N> listItem, K listKey) {
293 path.add(new IdentifiableItem<N, K>(listItem, listKey));
294 return (InstanceIdentifierBuilder<N>) this;
297 @SuppressWarnings({ "unchecked", "rawtypes" })
299 public InstanceIdentifier<T> toInstance() {
300 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
301 return new InstanceIdentifier(immutablePath, target);
305 public InstanceIdentifier<T> build() {
310 public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {
311 return addNode(container);
315 public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
316 Class<N> listItem, K listKey) {
317 return addNode(listItem, listKey);
321 public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
322 return addNode(container);
326 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
327 Class<N> listItem, K listKey) {
328 return addNode(listItem,listKey);
332 public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(
333 Class<N> container) {
334 return addNode(container);
339 public int hashCode() {
340 final int prime = 31;
342 result = prime * result + ((path == null) ? 0 : path.hashCode());
347 public boolean equals(Object obj) {
354 if (getClass() != obj.getClass()) {
357 InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
359 if (other.path != null) {
362 } else if (!path.equals(other.path)) {
369 public boolean contains(final InstanceIdentifier<?> other) {
371 throw new IllegalArgumentException("other should not be null");
373 final int localSize = this.path.size();
374 final List<PathArgument> otherPath = other.getPath();
375 if(localSize > other.path.size()) {
378 for(int i = 0;i<localSize;i++ ) {
379 if(!path.get(i).equals(otherPath.get(i))) {
386 public boolean containsWildcarded(final InstanceIdentifier<?> other) {
388 throw new IllegalArgumentException("other should not be null");
390 final int localSize = this.path.size();
391 final List<PathArgument> otherPath = other.getPath();
392 if(localSize > other.path.size()) {
395 for(int i = 0;i<localSize;i++ ) {
396 final PathArgument localArgument = path.get(i);
397 if(!localArgument.getType().equals(otherPath.get(i).getType())) {
400 if(localArgument instanceof IdentifiableItem<?, ?> && !localArgument.equals(otherPath.get(i))) {
407 public boolean isWildcarded() {
408 for(PathArgument pathArgument : path) {
409 if(Identifiable.class.isAssignableFrom(pathArgument.getType()) && !(pathArgument instanceof IdentifiableItem<?, ?>)) {