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.Mutable;
\r
19 * Uniquely identifies instance of data tree.
\r
22 public final class InstanceIdentifier implements Immutable {
\r
24 private final List<PathArgument> path;
\r
25 private final Class<? extends DataObject> targetType;
\r
27 public InstanceIdentifier(Class<? extends DataObject> type) {
\r
28 path = Collections.<PathArgument> singletonList(new Item<>(type));
\r
29 this.targetType = type;
\r
32 public InstanceIdentifier(List<PathArgument> path, Class<? extends DataObject> type) {
\r
33 this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));
\r
34 this.targetType = type;
\r
41 public List<PathArgument> getPath() {
\r
45 public Class<?> getTargetType() {
\r
46 return this.targetType;
\r
50 public String toString() {
\r
51 return "InstanceIdentifier [path=" + path + "]";
\r
55 * Path argument of instance identifier.
\r
57 * Interface which implementations are used as path components of the
\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 static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
\r
109 private final T key;
\r
110 private final Class<I> type;
\r
112 public IdentifiableItem(Class<I> type, T key) {
\r
114 throw new IllegalArgumentException("Type must not be null.");
\r
116 throw new IllegalArgumentException("Key must not be null.");
\r
126 public Class<I> getType() {
\r
131 public boolean equals(Object obj) {
\r
135 if (obj.hashCode() != hashCode()) {
\r
138 if (!(obj instanceof IdentifiableItem<?, ?>)) {
\r
141 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
\r
142 return key.equals(foreign.getKey());
\r
146 public int hashCode() {
\r
147 return key.hashCode();
\r
151 public String toString() {
\r
152 return type.getName() + "[key=" + key + "]";
\r
156 public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {
\r
158 <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container);
\r
160 <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(
\r
161 Class<I> listItem, T listKey);
\r
165 public static InstanceIdentifierBuilder builder() {
\r
166 return new BuilderImpl();
\r
169 private static class BuilderImpl implements InstanceIdentifierBuilder {
\r
171 private List<PathArgument> path;
\r
172 private Class<? extends DataObject> target = null;
\r
175 public InstanceIdentifier toInstance() {
\r
176 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
\r
177 return new InstanceIdentifier(immutablePath, target);
\r
181 public <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container) {
\r
182 target = container;
\r
183 path.add(new Item<T>(container));
\r
188 public <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(
\r
189 Class<I> listItem, T listKey) {
\r
191 path.add(new IdentifiableItem<I, T>(listItem, listKey));
\r
197 public int hashCode() {
\r
198 final int prime = 31;
\r
200 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
205 public boolean equals(Object obj) {
\r
212 if (getClass() != obj.getClass()) {
\r
215 InstanceIdentifier other = (InstanceIdentifier) obj;
\r
216 if (path == null) {
\r
217 if (other.path != null) {
\r
220 } else if (!path.equals(other.path)) {
\r