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
18 * Uniquely identifies data location in the overall of data tree
\r
23 public final class InstanceIdentifier implements Immutable {
\r
25 private final List<PathArgument> path;
\r
26 private final Class<? extends DataObject> targetType;
\r
28 public InstanceIdentifier(Class<? extends DataObject> type) {
\r
29 path = Collections.<PathArgument> singletonList(new Item<>(type));
\r
30 this.targetType = type;
\r
33 public InstanceIdentifier(List<PathArgument> path, Class<? extends DataObject> type) {
\r
34 this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));
\r
35 this.targetType = type;
\r
42 public List<PathArgument> getPath() {
\r
46 public Class<?> getTargetType() {
\r
47 return this.targetType;
\r
51 public String toString() {
\r
52 return "InstanceIdentifier [path=" + path + "]";
\r
56 * Path argument of {@link InstanceIdentifier}.
\r
58 * Interface which implementations are used as path components of the
\r
59 * path in overall data tree.
\r
62 public interface PathArgument {
\r
64 Class<? extends DataObject> getType();
\r
68 public static final class Item<T extends DataObject> implements PathArgument {
\r
69 private final Class<T> type;
\r
71 public Item(Class<T> type) {
\r
75 public Class<T> getType() {
\r
80 public int hashCode() {
\r
81 final int prime = 31;
\r
83 result = prime * result + ((type == null) ? 0 : type.hashCode());
\r
88 public boolean equals(Object obj) {
\r
93 if (getClass() != obj.getClass())
\r
95 Item<?> other = (Item<?>) obj;
\r
97 if (other.type != null)
\r
99 } else if (!type.equals(other.type))
\r
105 public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements
\r
108 private final T key;
\r
109 private final Class<I> type;
\r
111 public IdentifiableItem(Class<I> type, T key) {
\r
113 throw new IllegalArgumentException("Type must not be null.");
\r
115 throw new IllegalArgumentException("Key must not be null.");
\r
125 public Class<I> getType() {
\r
130 public boolean equals(Object obj) {
\r
134 if (obj.hashCode() != hashCode()) {
\r
137 if (!(obj instanceof IdentifiableItem<?, ?>)) {
\r
140 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
\r
141 return key.equals(foreign.getKey());
\r
145 public int hashCode() {
\r
146 return key.hashCode();
\r
150 public String toString() {
\r
151 return type.getName() + "[key=" + key + "]";
\r
155 public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {
\r
157 <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container);
\r
159 <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(
\r
160 Class<I> listItem, T listKey);
\r
164 public static InstanceIdentifierBuilder builder() {
\r
165 return new BuilderImpl();
\r
168 public static InstanceIdentifierBuilder builder(InstanceIdentifier basePath) {
\r
169 return new BuilderImpl(basePath.path);
\r
172 private static final class BuilderImpl implements InstanceIdentifierBuilder {
\r
174 private List<PathArgument> path;
\r
175 private Class<? extends DataObject> target = null;
\r
177 public BuilderImpl() {
\r
178 this.path = new ArrayList<>();
\r
182 public BuilderImpl(List<? extends PathArgument> prefix) {
\r
183 this.path = new ArrayList<>(prefix);
\r
187 public InstanceIdentifier toInstance() {
\r
188 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
\r
189 return new InstanceIdentifier(immutablePath, target);
\r
193 public <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container) {
\r
194 target = container;
\r
195 path.add(new Item<T>(container));
\r
200 public <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(
\r
201 Class<I> listItem, T listKey) {
\r
203 path.add(new IdentifiableItem<I, T>(listItem, listKey));
\r
209 public int hashCode() {
\r
210 final int prime = 31;
\r
212 result = prime * result + ((path == null) ? 0 : path.hashCode());
\r
217 public boolean equals(Object obj) {
\r
224 if (getClass() != obj.getClass()) {
\r
227 InstanceIdentifier other = (InstanceIdentifier) obj;
\r
228 if (path == null) {
\r
229 if (other.path != null) {
\r
232 } else if (!path.equals(other.path)) {
\r