BUG-1472: deprecated CompositeNode and friends
[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.CompositeNode;
12 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
13 import org.opendaylight.yangtools.yang.data.api.Node;
14 import org.opendaylight.yangtools.yang.data.api.NodeModification;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
16
17 /**
18  * @author michal.rehak
19  * @param <T>
20  *            type of node value
21  * @deprecated Use one of the {@link NormalizedNodeBuilder} implementations.
22  */
23 @Deprecated
24 public abstract class AbstractNodeTO<T> implements Node<T>, NodeModification {
25
26     private QName qName;
27     private CompositeNode parent;
28     private T value;
29     private ModifyAction modifyAction;
30
31     // Only for Serialization use
32     public AbstractNodeTO(){
33
34     }
35
36     /**
37      * @param qname
38      * @param parent
39      * @param value
40      */
41     public AbstractNodeTO(final QName qname, final CompositeNode parent, final T value) {
42         this(qname, parent, value, null);
43     }
44
45     /**
46      * @param qname
47      * @param parent
48      * @param value
49      * @param modifyAction
50      */
51     public AbstractNodeTO(final QName qname, final CompositeNode parent, final T value, final ModifyAction modifyAction) {
52         this.qName = qname;
53         this.parent = parent;
54         this.value = value;
55         this.modifyAction = modifyAction;
56     }
57
58     @Override
59     public QName getNodeType() {
60         return qName;
61     }
62
63     /**
64      * @return the qName
65      */
66     public QName getQName() {
67         return qName;
68     }
69
70     @Override
71     public CompositeNode getParent() {
72         return parent;
73     }
74
75     /**
76      * @param parent
77      *            the parent to set
78      */
79     public void setParent(final CompositeNode parent) {
80         this.parent = parent;
81     }
82
83     /**
84      * @param value
85      *            the value to set
86      */
87     @Override
88     public T setValue(final T value) {
89         T oldValue = this.value;
90         this.value = value;
91         return oldValue;
92     }
93
94     @Override
95     public T getValue() {
96         return value;
97     }
98
99     /**
100      * @return modification action
101      * @see NodeModification#getModificationAction()
102      */
103     @Override
104     public ModifyAction getModificationAction() {
105         return modifyAction;
106     }
107
108     /**
109      * @param modifyAction
110      *            the modifyAction to set
111      */
112     protected void setModificationAction(final ModifyAction modifyAction) {
113         this.modifyAction = modifyAction;
114     }
115
116     @Override
117     public String toString() {
118         StringBuffer out = new StringBuffer();
119         out.append(String.format("Node[%s], qName[%s], modify[%s]", getClass().getSimpleName(), getQName()
120                 .getLocalName(), getModificationAction() == null ? "n/a" : getModificationAction()));
121         return out.toString();
122     }
123
124
125     @Override
126     public final QName getKey() {
127         return getNodeType();
128     }
129
130     /* */
131     @Override
132     public int hashCode() {
133         final int prime = 31;
134         int result = 1;
135         result = prime * result + ((qName == null) ? 0 : qName.hashCode());
136         result = prime * result + ((value == null) ? 0 : value.hashCode());
137         return result % 2;
138     }
139
140     @Override
141     public boolean equals(final Object obj) {
142         if (this == obj) {
143             return true;
144         }
145         if (obj == null) {
146             return false;
147         }
148         if (getClass() != obj.getClass()) {
149             return false;
150         }
151         @SuppressWarnings("unchecked")
152         AbstractNodeTO<T> other = (AbstractNodeTO<T>) obj;
153         if (parent == null) {
154             if (other.parent != null) {
155                 return false;
156             }
157         } else if (other.parent == null) {
158             return false;
159         }
160         if (qName == null) {
161             if (other.qName != null) {
162                 return false;
163             }
164         } else if (!qName.equals(other.qName)) {
165             return false;
166         }
167         if (value == null) {
168             if (other.value != null) {
169                 return false;
170             }
171         } else if (!value.equals(other.value)) {
172             return false;
173         }
174         return true;
175     }
176     /* */
177
178
179     //Serialization related
180
181     protected final void init(final QName qName, final CompositeNode parent, final T value, final ModifyAction modifyAction){
182         this.qName = qName;
183         this.modifyAction = modifyAction;
184         this.parent = parent;
185         this.value = value;
186     }
187 }