Merge "Added XmlTreeBuilder into yang-data-impl"
[yangtools.git] / yang / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / AbstractUnsignedInteger.java
1 /**
2  *
3  */
4 package org.opendaylight.yangtools.yang.model.util;
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.List;
9
10 import org.opendaylight.yangtools.yang.common.QName;
11 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
12 import org.opendaylight.yangtools.yang.model.api.Status;
13 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
15 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
16
17 /**
18  * The Abstract Integer class defines implementation of IntegerTypeDefinition
19  * interface which represents UNSIGNED Integer values defined in Yang language. <br>
20  * The integer built-in types in Yang are uint8, uint16, uint32, and uint64.
21  * They represent unsigned integers of different sizes:
22  *
23  * <ul>
24  * <li>uint8 - represents integer values between 0 and 255, inclusively.</li>
25  * <li>uint16 - represents integer values between 0 and 65535, inclusively.</li>
26  * <li>uint32 - represents integer values between 0 and 4294967295, inclusively.
27  * </li>
28  * <li>uint64 - represents integer values between 0 and 18446744073709551615,
29  * inclusively.</li>
30  * </ul>
31  *
32  */
33 abstract class AbstractUnsignedInteger implements UnsignedIntegerTypeDefinition {
34     private static final long MIN_VALUE = 0;
35     private final QName name;
36     private final SchemaPath path;
37     private final String description;
38     private static final String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.2";
39     private final String units;
40     private final List<RangeConstraint> rangeStatements;
41
42     /**
43      *
44      * @param name
45      * @param description
46      * @param maxRange
47      * @param units
48      */
49     public AbstractUnsignedInteger(final QName name, final String description, final Number maxRange, final String units) {
50         this.name = name;
51         this.path = new SchemaPath(Collections.singletonList(name), true);
52         this.description = description;
53         this.units = units;
54         this.rangeStatements = new ArrayList<RangeConstraint>();
55         final String rangeDescription = "Integer values between " + MIN_VALUE + " and " + maxRange + ", inclusively.";
56         this.rangeStatements.add(BaseConstraints.rangeConstraint(MIN_VALUE, maxRange, rangeDescription,
57                 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
58     }
59
60     @Override
61     public UnsignedIntegerTypeDefinition getBaseType() {
62         return null;
63     }
64
65     @Override
66     public String getUnits() {
67         return units;
68     }
69
70     @Override
71     public QName getQName() {
72         return name;
73     }
74
75     @Override
76     public SchemaPath getPath() {
77         return path;
78     }
79
80     @Override
81     public String getDescription() {
82         return description;
83     }
84
85     @Override
86     public String getReference() {
87         return REFERENCE;
88     }
89
90     @Override
91     public Status getStatus() {
92         return Status.CURRENT;
93     }
94
95     @Override
96     public List<RangeConstraint> getRangeConstraints() {
97         return rangeStatements;
98     }
99
100     @Override
101     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
102         return Collections.emptyList();
103     }
104
105     @Override
106     public int hashCode() {
107         final int prime = 31;
108         int result = 1;
109         result = prime * result + ((description == null) ? 0 : description.hashCode());
110         result = prime * result + ((name == null) ? 0 : name.hashCode());
111         result = prime * result + ((path == null) ? 0 : path.hashCode());
112         result = prime * result + ((rangeStatements == null) ? 0 : rangeStatements.hashCode());
113         result = prime * result + ((units == null) ? 0 : units.hashCode());
114         return result;
115     }
116
117     @Override
118     public boolean equals(Object obj) {
119         if (this == obj) {
120             return true;
121         }
122         if (obj == null) {
123             return false;
124         }
125         if (getClass() != obj.getClass()) {
126             return false;
127         }
128         AbstractUnsignedInteger other = (AbstractUnsignedInteger) obj;
129         if (description == null) {
130             if (other.description != null) {
131                 return false;
132             }
133         } else if (!description.equals(other.description)) {
134             return false;
135         }
136         if (name == null) {
137             if (other.name != null) {
138                 return false;
139             }
140         } else if (!name.equals(other.name)) {
141             return false;
142         }
143         if (path == null) {
144             if (other.path != null) {
145                 return false;
146             }
147         } else if (!path.equals(other.path)) {
148             return false;
149         }
150         if (rangeStatements == null) {
151             if (other.rangeStatements != null) {
152                 return false;
153             }
154         } else if (!rangeStatements.equals(other.rangeStatements)) {
155             return false;
156         }
157         if (units == null) {
158             if (other.units != null) {
159                 return false;
160             }
161         } else if (!units.equals(other.units)) {
162             return false;
163         }
164         return true;
165     }
166
167     @Override
168     public String toString() {
169         StringBuilder builder = new StringBuilder();
170         builder.append("AbstractInteger [name=");
171         builder.append(name);
172         builder.append(", path=");
173         builder.append(path);
174         builder.append(", description=");
175         builder.append(description);
176         builder.append(", reference=");
177         builder.append(REFERENCE);
178         builder.append(", units=");
179         builder.append(units);
180         builder.append(", rangeStatements=");
181         builder.append(rangeStatements);
182         builder.append("]");
183         return builder.toString();
184     }
185 }