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() {
49 public Class<T> getTargetType() {
50 return this.targetType;
54 public String toString() {
55 return "InstanceIdentifier [path=" + path + "]";
59 * Return an instance identifier trimmed at the first occurrence of a
60 * specific component type.
62 * @param type component type
63 * @return trimmed instance identifier, or null if the component type
66 public <T extends DataObject> InstanceIdentifier<T> firstIdentifierOf(final Class<T> type) {
68 for (final PathArgument a : path) {
69 if (type.equals(a.getType())) {
70 return new InstanceIdentifier<>(path.subList(0, i), type);
80 * Return the key associated with the first component of specified type in
83 * @param listItem component type
84 * @param listKey component key type
85 * @return key associated with the component, or null if the component type
88 public <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K firstKeyOf(final Class<N> listItem, final Class<K> listKey) {
89 for (PathArgument i : path) {
90 if (listItem.equals(i.getType())) {
91 @SuppressWarnings("unchecked")
92 final K ret = ((IdentifiableItem<N, K>)i).getKey();
101 * Return the key associated with the last component of the specified identifier.
103 * @param id instance identifier
104 * @return key associated with the last component
106 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> K keyOf(final InstanceIdentifier<N> id) {
107 @SuppressWarnings("unchecked")
108 final K ret = ((IdentifiableItem<N, K>)Iterables.getLast(id.getPath())).getKey();
113 * Path argument of {@link InstanceIdentifier}.
115 * Interface which implementations are used as path components of the
116 * path in overall data tree.
119 public interface PathArgument {
121 Class<? extends DataObject> getType();
125 public static final class Item<T extends DataObject> implements PathArgument {
126 private final Class<T> type;
128 public Item(Class<T> type) {
132 public Class<T> getType() {
137 public int hashCode() {
138 final int prime = 31;
140 result = prime * result + ((type == null) ? 0 : type.hashCode());
145 public boolean equals(Object obj) {
150 if (getClass() != obj.getClass())
152 Item<?> other = (Item<?>) obj;
154 if (other.type != null)
156 } else if (!type.equals(other.type))
162 public String toString() {
163 return type.getName();
167 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
171 private final Class<I> type;
173 public IdentifiableItem(Class<I> type, T key) {
175 throw new IllegalArgumentException("Type must not be null.");
177 throw new IllegalArgumentException("Key must not be null.");
187 public Class<I> getType() {
192 public boolean equals(Object obj) {
196 if (obj.hashCode() != hashCode()) {
199 if (!(obj instanceof IdentifiableItem<?, ?>)) {
202 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
203 return key.equals(foreign.getKey());
207 public int hashCode() {
208 return key.hashCode();
212 public String toString() {
213 return type.getName() + "[key=" + key + "]";
217 public interface InstanceIdentifierBuilder<T extends DataObject> extends Builder<InstanceIdentifier<T>> {
219 * @deprecated use {@link child(Class)} or {@link augmentation(Class)} instead.
222 <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container);
225 * @deprecated use {@link child(Class,Identifier)} or {@link augmentation(Class,Identifier)} instead.
228 <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
229 Class<N> listItem, K listKey);
231 <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container);
233 <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
234 Class<N> listItem, K listKey);
236 <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(Class<N> container);
241 * @deprecated use {@link builder(Class)} or {@link builder(Class,Identifier)} instead.
244 @SuppressWarnings("rawtypes")
245 public static InstanceIdentifierBuilder<?> builder() {
246 return new BuilderImpl();
249 public static <T extends ChildOf<? extends DataRoot>> InstanceIdentifierBuilder<T> builder(Class<T> container) {
250 return new BuilderImpl<T>().addNode(container);
253 public static <N extends Identifiable<K> & DataObject, K extends Identifier<N>> InstanceIdentifierBuilder<N> builder(
254 Class<N> listItem, K listKey) {
255 return new BuilderImpl<N>().addNode(listItem, listKey);
258 public static <T extends DataObject> InstanceIdentifierBuilder<T> builder(InstanceIdentifier<T> basePath) {
259 return new BuilderImpl<T>(basePath.path,basePath.targetType);
262 private static final class BuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
264 private List<PathArgument> path;
265 private Class<? extends DataObject> target = null;
267 public BuilderImpl() {
268 this.path = new ArrayList<>();
271 public BuilderImpl(List<? extends PathArgument> prefix,Class<? extends DataObject> target) {
272 this.path = new ArrayList<>(prefix);
273 this.target = target;
276 @SuppressWarnings("unchecked")
277 private <N extends DataObject> InstanceIdentifierBuilder<N> addNode(Class<N> container) {
279 path.add(new Item<N>(container));
280 return (InstanceIdentifierBuilder<N>) this;
283 @SuppressWarnings("unchecked")
284 private <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> addNode(
285 Class<N> listItem, K listKey) {
287 path.add(new IdentifiableItem<N, K>(listItem, listKey));
288 return (InstanceIdentifierBuilder<N>) this;
291 @SuppressWarnings({ "unchecked", "rawtypes" })
293 public InstanceIdentifier<T> toInstance() {
294 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
295 return new InstanceIdentifier(immutablePath, target);
299 public <N extends DataObject> InstanceIdentifierBuilder<N> node(Class<N> container) {
300 return addNode(container);
304 public <N extends DataObject & Identifiable<K> , K extends Identifier<N>> InstanceIdentifierBuilder<N> node(
305 Class<N> listItem, K listKey) {
306 return addNode(listItem, listKey);
310 public <N extends ChildOf<? super T>> InstanceIdentifierBuilder<N> child(Class<N> container) {
311 return addNode(container);
315 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilder<N> child(
316 Class<N> listItem, K listKey) {
317 return addNode(listItem,listKey);
321 public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilder<N> augmentation(
322 Class<N> container) {
323 return addNode(container);
328 public int hashCode() {
329 final int prime = 31;
331 result = prime * result + ((path == null) ? 0 : path.hashCode());
336 public boolean equals(Object obj) {
343 if (getClass() != obj.getClass()) {
346 InstanceIdentifier<?> other = (InstanceIdentifier<?>) obj;
348 if (other.path != null) {
351 } else if (!path.equals(other.path)) {
358 public boolean contains(final InstanceIdentifier<?> other) {
360 throw new IllegalArgumentException("other should not be null");
362 final int localSize = this.path.size();
363 final List<PathArgument> otherPath = other.getPath();
364 if(localSize > other.path.size()) {
367 for(int i = 0;i<localSize;i++ ) {
368 if(!path.get(i).equals(otherPath.get(i))) {