Fixing sonar issues 4
[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     /**
30      * @param qname
31      * @param parent
32      * @param value
33      */
34     public AbstractNodeTO(QName qname, CompositeNode parent, T value) {
35         this.qName = qname;
36         this.parent = parent;
37         this.value = value;
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     protected void setValue(T value) {
83         this.value = value;
84     }
85
86     @Override
87     public T getValue() {
88         return value;
89     }
90
91     /**
92      * @return modification action
93      * @see org.opendaylight.yangtools.yang.data.impl.NodeModificationSupport#getModificationAction()
94      */
95     @Override
96     public ModifyAction getModificationAction() {
97         return modifyAction;
98     }
99
100     /**
101      * @param modifyAction
102      *            the modifyAction to set
103      */
104     protected void setModificationAction(ModifyAction modifyAction) {
105         this.modifyAction = modifyAction;
106     }
107
108     @Override
109     public String toString() {
110         StringBuffer out = new StringBuffer();
111         out.append(String.format("Node[%s], qName[%s], modify[%s]", getClass().getSimpleName(), getQName()
112                 .getLocalName(), getModificationAction() == null ? "n/a" : getModificationAction()));
113         return out.toString();
114     }
115
116     /* */
117     @Override
118     public int hashCode() {
119         final int prime = 31;
120         int result = 1;
121         result = prime * result + ((modifyAction == null) ? 0 : modifyAction.hashCode());
122         result = prime * result + ((qName == null) ? 0 : qName.hashCode());
123         result = prime * result + ((value == null) ? 0 : value.hashCode());
124         return result % 2;
125     }
126
127     @Override
128     public boolean equals(Object obj) {
129         if (this == obj) {
130             return true;
131         }
132         if (obj == null) {
133             return false;
134         }
135         if (getClass() != obj.getClass()) {
136             return false;
137         }
138         @SuppressWarnings("unchecked")
139         AbstractNodeTO<T> other = (AbstractNodeTO<T>) obj;
140         if (modifyAction != other.modifyAction) {
141             return false;
142         }
143         if (parent == null) {
144             if (other.parent != null) {
145                 return false;
146             }
147         } else if (other.parent == null) {
148             return false;
149         }
150         if (qName == null) {
151             if (other.qName != null) {
152                 return false;
153             }
154         } else if (!qName.equals(other.qName)) {
155             return false;
156         }
157         if (value == null) {
158             if (other.value != null) {
159                 return false;
160             }
161         } else if (!value.equals(other.value)) {
162             return false;
163         }
164         return true;
165     }
166     /* */
167
168 }