Added codec for w3c.Document to yang-data-api with schema support
[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
16 /**
17  * @author michal.rehak
18  * @param <T>
19  *            type of node value
20  * 
21  */
22 public abstract class AbstractNodeTO<T> implements Node<T>, NodeModification {
23
24     private QName qName;
25     private CompositeNode parent;
26     private T value;
27     private ModifyAction modifyAction;
28
29     // Only for Serialization use
30     public AbstractNodeTO(){
31
32     }
33
34     /**
35      * @param qname
36      * @param parent
37      * @param value
38      */
39     public AbstractNodeTO(QName qname, CompositeNode parent, T value) {
40         this.qName = qname;
41         this.parent = parent;
42         this.value = value;
43     }
44
45     /**
46      * @param qname
47      * @param parent
48      * @param value
49      * @param modifyAction
50      */
51     public AbstractNodeTO(QName qname, CompositeNode parent, T value, 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(CompositeNode parent) {
80         this.parent = parent;
81     }
82
83     /**
84      * @param value
85      *            the value to set
86      */
87     public T setValue(T value) {
88         T oldValue = this.value;
89         this.value = value;
90         return oldValue;
91     }
92
93     @Override
94     public T getValue() {
95         return value;
96     }
97
98     /**
99      * @return modification action
100      * @see NodeModification#getModificationAction()
101      */
102     @Override
103     public ModifyAction getModificationAction() {
104         return modifyAction;
105     }
106
107     /**
108      * @param modifyAction
109      *            the modifyAction to set
110      */
111     protected void setModificationAction(ModifyAction modifyAction) {
112         this.modifyAction = modifyAction;
113     }
114
115     @Override
116     public String toString() {
117         StringBuffer out = new StringBuffer();
118         out.append(String.format("Node[%s], qName[%s], modify[%s]", getClass().getSimpleName(), getQName()
119                 .getLocalName(), getModificationAction() == null ? "n/a" : getModificationAction()));
120         return out.toString();
121     }
122     
123     
124     @Override
125     public final QName getKey() {
126         return getNodeType();
127     }
128
129     /* */
130     @Override
131     public int hashCode() {
132         final int prime = 31;
133         int result = 1;
134         result = prime * result + ((modifyAction == null) ? 0 : modifyAction.hashCode());
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 (modifyAction != other.modifyAction) {
154             return false;
155         }
156         if (parent == null) {
157             if (other.parent != null) {
158                 return false;
159             }
160         } else if (other.parent == null) {
161             return false;
162         }
163         if (qName == null) {
164             if (other.qName != null) {
165                 return false;
166             }
167         } else if (!qName.equals(other.qName)) {
168             return false;
169         }
170         if (value == null) {
171             if (other.value != null) {
172                 return false;
173             }
174         } else if (!value.equals(other.value)) {
175             return false;
176         }
177         return true;
178     }
179     /* */
180
181
182     //Serialization related
183
184     protected final void init(QName qName, CompositeNode parent, T value, ModifyAction modifyAction){
185         this.qName = qName;
186         this.modifyAction = modifyAction;
187         this.parent = parent;
188         this.value = value;
189     }
190 }