Merge "Added documentation for web socket client"
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.*;
15
16 import java.net.URI;
17 import java.util.Map;
18
19 /**
20  * @author michal.rehak
21  * @param <T>
22  *            type of node value
23  *
24  */
25 public abstract class AbstractNodeTO<T> implements Node<T>, NodeModification {
26
27     private QName qName;
28     private CompositeNode parent;
29     private T value;
30     private ModifyAction modifyAction;
31
32     // Only for Serialization use
33     public AbstractNodeTO(){
34
35     }
36
37     /**
38      * @param qname
39      * @param parent
40      * @param value
41      */
42     public AbstractNodeTO(QName qname, CompositeNode parent, T value) {
43         this(qname, parent, value, null);
44     }
45
46     /**
47      * @param qname
48      * @param parent
49      * @param value
50      * @param modifyAction
51      */
52     public AbstractNodeTO(QName qname, CompositeNode parent, T value, ModifyAction modifyAction) {
53         this.qName = qname;
54         this.parent = parent;
55         this.value = value;
56         this.modifyAction = modifyAction;
57     }
58
59     @Override
60     public QName getNodeType() {
61         return qName;
62     }
63
64     /**
65      * @return the qName
66      */
67     public QName getQName() {
68         return qName;
69     }
70
71     @Override
72     public CompositeNode getParent() {
73         return parent;
74     }
75
76     /**
77      * @param parent
78      *            the parent to set
79      */
80     public void setParent(CompositeNode parent) {
81         this.parent = parent;
82     }
83
84     /**
85      * @param value
86      *            the value to set
87      */
88     public T setValue(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(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(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(QName qName, CompositeNode parent, T value, ModifyAction modifyAction){
182         this.qName = qName;
183         this.modifyAction = modifyAction;
184         this.parent = parent;
185         this.value = value;
186     }
187 }