Added documentation to yang-binding, updated dependencies.
[yangtools.git] / yang / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / InstanceIdentifier.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.yangtools.yang.binding;\r
9 \r
10 import java.util.ArrayList;\r
11 import java.util.Collections;\r
12 import java.util.List;\r
13 \r
14 import org.opendaylight.yangtools.concepts.Builder;\r
15 import org.opendaylight.yangtools.concepts.Immutable;\r
16 \r
17 /**\r
18  * Uniquely identifies data location in the overall of data tree \r
19  * modeled by YANG.\r
20  * \r
21  * \r
22  */\r
23 public final class InstanceIdentifier implements Immutable {\r
24 \r
25     private final List<PathArgument> path;\r
26     private final Class<? extends DataObject> targetType;\r
27 \r
28     public InstanceIdentifier(Class<? extends DataObject> type) {\r
29         path = Collections.<PathArgument> singletonList(new Item<>(type));\r
30         this.targetType = type;\r
31     }\r
32 \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
36     }\r
37 \r
38     /**\r
39      * \r
40      * @return path\r
41      */\r
42     public List<PathArgument> getPath() {\r
43         return this.path;\r
44     }\r
45 \r
46     public Class<?> getTargetType() {\r
47         return this.targetType;\r
48     }\r
49 \r
50     @Override\r
51     public String toString() {\r
52         return "InstanceIdentifier [path=" + path + "]";\r
53     }\r
54 \r
55     /**\r
56      * Path argument of {@link InstanceIdentifier}.\r
57      * <p>\r
58      * Interface which implementations are used as path components of the\r
59      * path in overall data tree.\r
60      *\r
61      */\r
62     public interface PathArgument {\r
63 \r
64         Class<? extends DataObject> getType();\r
65 \r
66     }\r
67 \r
68     public static final class Item<T extends DataObject> implements PathArgument {\r
69         private final Class<T> type;\r
70 \r
71         public Item(Class<T> type) {\r
72             this.type = type;\r
73         }\r
74 \r
75         public Class<T> getType() {\r
76             return type;\r
77         }\r
78 \r
79         @Override\r
80         public int hashCode() {\r
81             final int prime = 31;\r
82             int result = 1;\r
83             result = prime * result + ((type == null) ? 0 : type.hashCode());\r
84             return result;\r
85         }\r
86 \r
87         @Override\r
88         public boolean equals(Object obj) {\r
89             if (this == obj)\r
90                 return true;\r
91             if (obj == null)\r
92                 return false;\r
93             if (getClass() != obj.getClass())\r
94                 return false;\r
95             Item<?> other = (Item<?>) obj;\r
96             if (type == null) {\r
97                 if (other.type != null)\r
98                     return false;\r
99             } else if (!type.equals(other.type))\r
100                 return false;\r
101             return true;\r
102         }\r
103     }\r
104 \r
105     public static final class IdentifiableItem<I extends Identifiable<T> & DataObject, T extends Identifier<I>> implements\r
106             PathArgument {\r
107 \r
108         private final T key;\r
109         private final Class<I> type;\r
110 \r
111         public IdentifiableItem(Class<I> type, T key) {\r
112             if (type == null)\r
113                 throw new IllegalArgumentException("Type must not be null.");\r
114             if (key == null)\r
115                 throw new IllegalArgumentException("Key must not be null.");\r
116             this.type = type;\r
117             this.key = key;\r
118         }\r
119 \r
120         T getKey() {\r
121             return this.key;\r
122         }\r
123 \r
124         @Override\r
125         public Class<I> getType() {\r
126             return this.type;\r
127         }\r
128 \r
129         @Override\r
130         public boolean equals(Object obj) {\r
131             if (obj == null) {\r
132                 return false;\r
133             }\r
134             if (obj.hashCode() != hashCode()) {\r
135                 return false;\r
136             }\r
137             if (!(obj instanceof IdentifiableItem<?, ?>)) {\r
138                 return false;\r
139             }\r
140             IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;\r
141             return key.equals(foreign.getKey());\r
142         }\r
143 \r
144         @Override\r
145         public int hashCode() {\r
146             return key.hashCode();\r
147         }\r
148 \r
149         @Override\r
150         public String toString() {\r
151             return type.getName() + "[key=" + key + "]";\r
152         }\r
153     }\r
154 \r
155     public interface InstanceIdentifierBuilder extends Builder<InstanceIdentifier> {\r
156 \r
157         <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container);\r
158 \r
159         <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(\r
160                 Class<I> listItem, T listKey);\r
161 \r
162     }\r
163 \r
164     public static InstanceIdentifierBuilder builder() {\r
165         return new BuilderImpl();\r
166     }\r
167 \r
168     public static InstanceIdentifierBuilder builder(InstanceIdentifier basePath) {\r
169         return new BuilderImpl(basePath.path);\r
170     }\r
171 \r
172     private static final class BuilderImpl implements InstanceIdentifierBuilder {\r
173 \r
174         private List<PathArgument> path;\r
175         private Class<? extends DataObject> target = null;\r
176 \r
177         public BuilderImpl() {\r
178             this.path = new ArrayList<>();\r
179         }\r
180         \r
181 \r
182         public BuilderImpl(List<? extends PathArgument> prefix) {\r
183             this.path = new ArrayList<>(prefix);\r
184         }\r
185 \r
186         @Override\r
187         public InstanceIdentifier toInstance() {\r
188             List<PathArgument> immutablePath = Collections.unmodifiableList(new ArrayList<PathArgument>(path));\r
189             return new InstanceIdentifier(immutablePath, target);\r
190         }\r
191 \r
192         @Override\r
193         public <T extends DataObject> InstanceIdentifierBuilder node(Class<T> container) {\r
194             target = container;\r
195             path.add(new Item<T>(container));\r
196             return this;\r
197         }\r
198 \r
199         @Override\r
200         public <I extends Identifiable<T> & DataObject, T extends Identifier<I>> InstanceIdentifierBuilder node(\r
201                 Class<I> listItem, T listKey) {\r
202             target = listItem;\r
203             path.add(new IdentifiableItem<I, T>(listItem, listKey));\r
204             return this;\r
205         }\r
206     }\r
207 \r
208     @Override\r
209     public int hashCode() {\r
210         final int prime = 31;\r
211         int result = 1;\r
212         result = prime * result + ((path == null) ? 0 : path.hashCode());\r
213         return result;\r
214     }\r
215 \r
216     @Override\r
217     public boolean equals(Object obj) {\r
218         if (this == obj) {\r
219             return true;\r
220         }\r
221         if (obj == null) {\r
222             return false;\r
223         }\r
224         if (getClass() != obj.getClass()) {\r
225             return false;\r
226         }\r
227         InstanceIdentifier other = (InstanceIdentifier) obj;\r
228         if (path == null) {\r
229             if (other.path != null) {\r
230                 return false;\r
231             }\r
232         } else if (!path.equals(other.path)) {\r
233             return false;\r
234         }\r
235         return true;\r
236     }\r
237 }\r