Merge "BUG-994: remove the legacy public constructor"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / AbstractNodeTO.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl;
9
10 import org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.data.api.*;
12
13 /**
14  * @author michal.rehak
15  * @param <T>
16  *            type of node value
17  *
18  */
19 public abstract class AbstractNodeTO<T> implements Node<T>, NodeModification {
20
21     private QName qName;
22     private CompositeNode parent;
23     private T value;
24     private ModifyAction modifyAction;
25
26     // Only for Serialization use
27     public AbstractNodeTO(){
28
29     }
30
31     /**
32      * @param qname
33      * @param parent
34      * @param value
35      */
36     public AbstractNodeTO(QName qname, CompositeNode parent, T value) {
37         this(qname, parent, value, null);
38     }
39
40     /**
41      * @param qname
42      * @param parent
43      * @param value
44      * @param modifyAction
45      */
46     public AbstractNodeTO(QName qname, CompositeNode parent, T value, ModifyAction modifyAction) {
47         this.qName = qname;
48         this.parent = parent;
49         this.value = value;
50         this.modifyAction = modifyAction;
51     }
52
53     @Override
54     public QName getNodeType() {
55         return qName;
56     }
57
58     /**
59      * @return the qName
60      */
61     public QName getQName() {
62         return qName;
63     }
64
65     @Override
66     public CompositeNode getParent() {
67         return parent;
68     }
69
70     /**
71      * @param parent
72      *            the parent to set
73      */
74     public void setParent(CompositeNode parent) {
75         this.parent = parent;
76     }
77
78     /**
79      * @param value
80      *            the value to set
81      */
82     public T setValue(T value) {
83         T oldValue = this.value;
84         this.value = value;
85         return oldValue;
86     }
87
88     @Override
89     public T getValue() {
90         return value;
91     }
92
93     /**
94      * @return modification action
95      * @see NodeModification#getModificationAction()
96      */
97     @Override
98     public ModifyAction getModificationAction() {
99         return modifyAction;
100     }
101
102     /**
103      * @param modifyAction
104      *            the modifyAction to set
105      */
106     protected void setModificationAction(ModifyAction modifyAction) {
107         this.modifyAction = modifyAction;
108     }
109
110     @Override
111     public String toString() {
112         StringBuffer out = new StringBuffer();
113         out.append(String.format("Node[%s], qName[%s], modify[%s]", getClass().getSimpleName(), getQName()
114                 .getLocalName(), getModificationAction() == null ? "n/a" : getModificationAction()));
115         return out.toString();
116     }
117
118
119     @Override
120     public final QName getKey() {
121         return getNodeType();
122     }
123
124     /* */
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = 1;
129         result = prime * result + ((qName == null) ? 0 : qName.hashCode());
130         result = prime * result + ((value == null) ? 0 : value.hashCode());
131         return result % 2;
132     }
133
134     @Override
135     public boolean equals(Object obj) {
136         if (this == obj) {
137             return true;
138         }
139         if (obj == null) {
140             return false;
141         }
142         if (getClass() != obj.getClass()) {
143             return false;
144         }
145         @SuppressWarnings("unchecked")
146         AbstractNodeTO<T> other = (AbstractNodeTO<T>) obj;
147         if (parent == null) {
148             if (other.parent != null) {
149                 return false;
150             }
151         } else if (other.parent == null) {
152             return false;
153         }
154         if (qName == null) {
155             if (other.qName != null) {
156                 return false;
157             }
158         } else if (!qName.equals(other.qName)) {
159             return false;
160         }
161         if (value == null) {
162             if (other.value != null) {
163                 return false;
164             }
165         } else if (!value.equals(other.value)) {
166             return false;
167         }
168         return true;
169     }
170     /* */
171
172
173     //Serialization related
174
175     protected final void init(QName qName, CompositeNode parent, T value, ModifyAction modifyAction){
176         this.qName = qName;
177         this.modifyAction = modifyAction;
178         this.parent = parent;
179         this.value = value;
180     }
181 }