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 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.emptyList();
\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 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 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
122 public Class<I> getType() {
\r
127 public boolean equals(Object obj) {
\r
131 if (obj.hashCode() != hashCode()) {
\r
134 if (!(obj instanceof IdentifiableItem<?, ?>)) {
\r
137 IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;
\r
138 return key.equals(foreign.getKey());
\r
142 public int hashCode() {
\r
143 return key.hashCode();
\r
147 public String toString() {
\r
148 return type.getName()+"[key=" + key + "]";
\r
152 public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {
\r
154 <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container);
\r
156 <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(
\r
157 Class<I> listItem, T listKey);
\r
161 public static InstanceIdentifierBuilder builder() {
\r
162 return new BuilderImpl();
\r
165 private static class BuilderImpl implements InstanceIdentifierBuilder {
\r
167 private List<PathArgument> path;
\r
168 private Class<? extends DataObject> target = null;
\r
171 public InstanceIdentifier toInstance() {
\r
172 List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));
\r
173 return new InstanceIdentifier(immutablePath, target);
\r
177 public <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container) {
\r
178 target = container;
\r
179 path.add(new Item<T>(container));
\r
184 public <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(
\r
185 Class<I> listItem, T listKey) {
\r
187 path.add(new IdentifiableItem<I, T>(listItem, listKey));
\r