Merge "Added RoutingContext annotation, updated InstanceIdentifier"
[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 /**\r
15  * Uniquely identifies instance of data tree.\r
16  * \r
17  * \r
18  */\r
19 public class InstanceIdentifier {\r
20 \r
21     private final List<PathArgument> path;\r
22     private final Class<? extends DataObject> targetType;\r
23 \r
24     public InstanceIdentifier(Class<? extends DataObject> type) {\r
25         path = Collections.emptyList();\r
26         this.targetType = type;\r
27     }\r
28 \r
29     public InstanceIdentifier(List<PathArgument> path, Class<? extends DataObject> type) {\r
30         this.path = Collections.<PathArgument> unmodifiableList(new ArrayList<>(path));\r
31         this.targetType = type;\r
32     }\r
33 \r
34     /**\r
35      * \r
36      * @return\r
37      */\r
38     public List<PathArgument> getPath() {\r
39         return this.path;\r
40     }\r
41 \r
42     public Class<?> getTargetType() {\r
43         return this.targetType;\r
44     }\r
45     \r
46     \r
47     \r
48     \r
49     @Override\r
50     public String toString() {\r
51         return "InstanceIdentifier [path=" + path + "]";\r
52     }\r
53 \r
54     /**\r
55      * Path argument of instance identifier.\r
56      * \r
57      * Interface which implementations are used as path components of the\r
58      * instance path.\r
59      * \r
60      * @author ttkacik\r
61      * \r
62      */\r
63     public interface PathArgument {\r
64 \r
65     }\r
66 \r
67     public static class IdentifiableItem<I extends Identifiable<T>, T extends Identifier<I>> implements PathArgument {\r
68 \r
69         private final T key;\r
70         private final Class<? extends I> type;\r
71 \r
72         public IdentifiableItem(Class<I> type, T key) {\r
73             this.type = type;\r
74             this.key = key;\r
75         }\r
76 \r
77         T getKey() {\r
78             return this.key;\r
79         }\r
80 \r
81         Class<? extends I> getType() {\r
82             return this.type;\r
83         }\r
84         \r
85         @Override\r
86         public boolean equals(Object obj) {\r
87             if(obj == null) {\r
88                 return false;\r
89             }\r
90             if(obj.hashCode() != hashCode()) {\r
91                 return false;\r
92             }\r
93             if(!(obj instanceof IdentifiableItem<?, ?>)) {\r
94                 return false;\r
95             }\r
96             IdentifiableItem<?, ?> foreign = (IdentifiableItem<?, ?>) obj;\r
97             return key.equals(foreign.getKey());\r
98         }\r
99         \r
100         @Override\r
101         public int hashCode() {\r
102             return key.hashCode();\r
103         }\r
104 \r
105         @Override\r
106         public String toString() {\r
107             return "IdentifiableItem [key=" + key + "]";\r
108         }\r
109     }\r
110 }\r