groovy node-tree integration
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-data-impl / src / main / java / org / opendaylight / controller / yang / data / impl / AbstractNodeTO.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.controller.yang.data.impl;\r
9 \r
10 import org.opendaylight.controller.yang.common.QName;\r
11 import org.opendaylight.controller.yang.data.api.CompositeNode;\r
12 import org.opendaylight.controller.yang.data.api.ModifyAction;\r
13 import org.opendaylight.controller.yang.data.api.Node;\r
14 import org.opendaylight.controller.yang.data.api.NodeModification;\r
15 \r
16 /**\r
17  * @author michal.rehak\r
18  * @param <T>\r
19  *            type of node value\r
20  * \r
21  */\r
22 public abstract class AbstractNodeTO<T> implements Node<T>, NodeModification {\r
23 \r
24     private QName qName;\r
25     private CompositeNode parent;\r
26     private T value;\r
27     private ModifyAction modifyAction;\r
28     \r
29     /**\r
30      * @param qname\r
31      * @param parent\r
32      * @param value\r
33      */\r
34     public AbstractNodeTO(QName qname, CompositeNode parent, T value) {\r
35         this.qName = qname;\r
36         this.parent = parent;\r
37         this.value = value;\r
38     }\r
39     \r
40     /**\r
41      * @param qname\r
42      * @param parent\r
43      * @param value\r
44      * @param modifyAction \r
45      */\r
46     public AbstractNodeTO(QName qname, CompositeNode parent, T value, ModifyAction modifyAction) {\r
47         this.qName = qname;\r
48         this.parent = parent;\r
49         this.value = value;\r
50         this.modifyAction = modifyAction;\r
51     }\r
52 \r
53     @Override\r
54     public QName getNodeType() {\r
55         return qName;\r
56     }\r
57 \r
58     /**\r
59      * @return the qName\r
60      */\r
61     public QName getQName() {\r
62         return qName;\r
63     }\r
64 \r
65     @Override\r
66     public CompositeNode getParent() {\r
67         return parent;\r
68     }\r
69     \r
70     /**\r
71      * @param parent the parent to set\r
72      */\r
73     public void setParent(CompositeNode parent) {\r
74         this.parent = parent;\r
75     }\r
76     \r
77     /**\r
78      * @param value the value to set\r
79      */\r
80     protected void setValue(T value) {\r
81         this.value = value;\r
82     }\r
83 \r
84     @Override\r
85     public T getValue() {\r
86         return value;\r
87     }\r
88 \r
89     /**\r
90      * @return modification action\r
91      * @see org.opendaylight.controller.yang.data.impl.NodeModificationSupport#getModificationAction()\r
92      */\r
93     @Override\r
94     public ModifyAction getModificationAction() {\r
95         return modifyAction;\r
96     }\r
97 \r
98     /**\r
99      * @param modifyAction\r
100      *            the modifyAction to set\r
101      */\r
102     protected void setModificationAction(ModifyAction modifyAction) {\r
103         this.modifyAction = modifyAction;\r
104     }\r
105     \r
106     @Override\r
107     public String toString() {\r
108         StringBuffer out = new StringBuffer();\r
109         out.append(String.format("Node[%s], qName[%s], modify[%s]", \r
110                 getClass().getSimpleName(), getQName().getLocalName(),\r
111                 getModificationAction() == null ? "n/a" : getModificationAction()));\r
112         return out.toString();\r
113     }\r
114 \r
115     /* */\r
116     @Override\r
117     public int hashCode() {\r
118         final int prime = 31;\r
119         int result = 1;\r
120         result = prime * result\r
121                 + ((modifyAction == null) ? 0 : modifyAction.hashCode());\r
122 //        result = prime * result + ((parent == null) ? 0 : parent.hashCode());\r
123         result = prime * result + ((qName == null) ? 0 : qName.hashCode());\r
124         result = prime * result + ((value == null) ? 0 : value.hashCode());\r
125         return result % 2;\r
126     }\r
127 \r
128     @Override\r
129     public boolean equals(Object obj) {\r
130         if (this == obj)\r
131             return true;\r
132         if (obj == null)\r
133             return false;\r
134         if (getClass() != obj.getClass())\r
135             return false;\r
136         @SuppressWarnings("unchecked")\r
137         AbstractNodeTO<T> other = (AbstractNodeTO<T>) obj;\r
138         if (modifyAction != other.modifyAction)\r
139             return false;\r
140         if (parent == null) {\r
141             if (other.parent != null)\r
142                 return false;\r
143         } else if (other.parent == null) {\r
144             return false;\r
145         } \r
146         if (qName == null) {\r
147             if (other.qName != null)\r
148                 return false;\r
149         } else if (!qName.equals(other.qName))\r
150             return false;\r
151         if (value == null) {\r
152             if (other.value != null)\r
153                 return false;\r
154         } else if (!value.equals(other.value))\r
155             return false;\r
156         return true;\r
157     }\r
158     /* */\r
159     \r
160 }\r