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.ImmutableList;
19 import com.google.common.collect.Iterables;
22 * Uniquely identifies data location in the overall of data tree
27 public final class InstanceIdentifier<T extends DataObject> implements Path<InstanceIdentifier<? extends DataObject>>,Immutable {
29 private final List<PathArgument> path;
30 private final Class<T> targetType;
32 public InstanceIdentifier(Class<T> type) {
33 path = Collections.<PathArgument> singletonList(new Item<>(type));
34 this.targetType = type;
37 public InstanceIdentifier(List<PathArgument> path, Class<T> type) {
38 this.path = ImmutableList.copyOf(path);
39 this.targetType = type;
46 public List<PathArgument> getPath() {
47 return getPathArguments();
50 public List<PathArgument> getPathArguments() {
54 public Class<T> getTargetType() {
55 return this.targetType;
59 public String toString() {
60 return "InstanceIdentifier [path=" + path + "]";
64 * Return an instance identifier trimmed at the first occurrence of a
65 * specific component type.
67 * @param type component type
68 * @return trimmed instance identifier, or null if the component type
71 public <T extends DataObject> InstanceIdentifier<T> firstIdentifierOf(final Class<T> type) {
73 for (final PathArgument a : path) {
74 if (type.equals(a.getType())) {
75 return new InstanceIdentifier<>(path.subList(0, i), type);
85 * Return the key associated with the first component of specified type in
88 * @param listItem component type
89 * @param listKey component key type
90 * @return key associated with the component, or null if the component type
93 public <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
94 for (PathArgument i : path) {
95 if (listItem.equals(i.getType())) {
96 @SuppressWarnings("unchecked")
97 final K ret = ((IdentifiableItem<N, K>)i).getKey();
106 * Return the key associated with the last component of the specified identifier.
108 * @param id instance identifier
109 * @return key associated with the last component
111 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
112 @SuppressWarnings("unchecked")
113 final K ret = ((IdentifiableItem<N, K>)Iterables.getLast(id.getPath())).getKey();
118 * Path argument of {@link InstanceIdentifier}.
120 * Interface which implementations are used as path components of the
121 * path in overall data tree.
124 public interface PathArgument {
126 Class<? extends DataObject> getType();
130 public static final class Item<T extends DataObject> implements PathArgument {
131 private final Class<T> type;
133 public Item(Class<T> type) {
137 public Class<T> getType() {
142 public int hashCode() {
143 final int prime = 31;
145 result = prime * result + ((type == null) ? 0 : type.hashCode());
150 public boolean equals(Object obj) {
155 if (getClass() != obj.getClass())
157 Item<?> other = (Item<?>) obj;
159 if (other.type != null)
161 } else if (!type.equals(other.type))
167 public String toString() {
168 return type.getName();
172 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
176 private final Class<I> type;
178 public IdentifiableItem(Class<I> type, T key) {
180 throw new IllegalArgumentException("Type must not be null.");
182 throw new IllegalArgumentException("Key must not be null.");
192 public Class<I> getType() {
197 public boolean equals(Object obj) {
201 if (obj.hashCode() != hashCode()) {
204 if (!(obj instanceof IdentifiableItem<?, ?>)) {
207 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
208 return key.equals(foreign.getKey());
212 public int hashCode() {
213 return key.hashCode();
217 public String toString() {
218 return type.getName() + "[key=" + key + "]";
222 public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
224 * @deprecated use {@link child(Class)} or {@link augmentation(Class)} instead.
227 <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);
230 * @deprecated use {@link child(Class,Identifier)} or {@link augmentation(Class,Identifier)} instead.
233 <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
234 Class<N> listItem, K listKey);
236 <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
238 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
239 Class<N> listItem, K listKey);
241 <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(Class<N> container);
243 InstanceIdentifier<T> build();
248 * @deprecated use {@link builder(Class)} or {@link builder(Class,Identifier)} instead.
251 @SuppressWarnings("rawtypes")
252 public static InstanceIdentifierBuilder<?> builder() {
253 return new BuilderImpl();
256 public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(Class<T> container) {
257 return new BuilderImpl<T>().addNode(container);
260 public static <N extends Identifiable<K> & ChildOf<? extends DataRoot>, K extends Identifier<N>> InstanceIdentifierBuilder<N> builder(
261 Class<N> listItem, K listKey) {
262 return new BuilderImpl<N>().addNode(listItem, listKey);
265 public static <T extends DataObject> InstanceIdentifierBuilder<T> builder(InstanceIdentifier<T> basePath) {
266 return new BuilderImpl<T>(basePath.path,basePath.targetType);
269 private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
271 private List<PathArgument> path;
272 private Class<? extends DataObject> target = null;
274 public BuilderImpl() {
275 this.path = new ArrayList<>();
278 public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
279 this.path = new ArrayList<>(prefix);
280 this.target = target;
283 @SuppressWarnings("unchecked")
284 private <N extends DataObject> InstanceIdentifierBuilder<N> addNode(Class<N> container) {
286 path.add(new Item<N>(container));
287 return (InstanceIdentifierBuilder<N>) this;
290 @SuppressWarnings("unchecked")
291 private <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> addNode(
292 Class<N> listItem, K listKey) {
294 path.add(new IdentifiableItem<N, K>(listItem, listKey));
295 return (InstanceIdentifierBuilder<N>) this;
298 @SuppressWarnings({ "unchecked", "rawtypes" })
300 public InstanceIdentifier<T> toInstance() {
301 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
302 return new InstanceIdentifier(immutablePath, target);
306 public InstanceIdentifier<T> build() {
311 public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {
312 return addNode(container);
316 public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
317 Class<N> listItem, K listKey) {
318 return addNode(listItem, listKey);
322 public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
323 return addNode(container);
327 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
328 Class<N> listItem, K listKey) {
329 return addNode(listItem,listKey);
333 public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(
334 Class<N> container) {
335 return addNode(container);
340 public int hashCode() {
341 final int prime = 31;
343 result = prime * result + ((path == null) ? 0 : path.hashCode());
348 public boolean equals(Object obj) {
355 if (getClass() != obj.getClass()) {
358 InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
360 if (other.path != null) {
363 } else if (!path.equals(other.path)) {
370 public boolean contains(final InstanceIdentifier<?> other) {
372 throw new IllegalArgumentException("other should not be null");
374 final int localSize = this.path.size();
375 final List<PathArgument> otherPath = other.getPath();
376 if(localSize > other.path.size()) {
379 for(int i = 0;i<localSize;i++ ) {
380 if(!path.get(i).equals(otherPath.get(i))) {
387 public boolean containsWildcarded(final InstanceIdentifier<?> other) {
389 throw new IllegalArgumentException("other should not be null");
391 final int localSize = this.path.size();
392 final List<PathArgument> otherPath = other.getPath();
393 if(localSize > other.path.size()) {
396 for(int i = 0;i<localSize;i++ ) {
397 final PathArgument localArgument = path.get(i);
398 if(!localArgument.getType().equals(otherPath.get(i).getType())) {
401 if(localArgument instanceof IdentifiableItem<?, ?> && !localArgument.equals(otherPath.get(i))) {
408 public boolean isWildcarded() {
409 for(PathArgument pathArgument : path) {
410 if(Identifiable.class.isAssignableFrom(pathArgument.getType()) && !(pathArgument instanceof IdentifiableItem<?, ?>)) {