Refactored base yang-java types.
[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 public 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 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 String getUnits() {
62         return units;
63     }
64
65     @Override
66     public QName getQName() {
67         return name;
68     }
69
70     @Override
71     public SchemaPath getPath() {
72         return path;
73     }
74
75     @Override
76     public String getDescription() {
77         return description;
78     }
79
80     @Override
81     public String getReference() {
82         return reference;
83     }
84
85     @Override
86     public Status getStatus() {
87         return Status.CURRENT;
88     }
89
90     @Override
91     public List<RangeConstraint> getRangeStatements() {
92         return rangeStatements;
93     }
94
95     @Override
96     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
97         return Collections.emptyList();
98     }
99
100     @Override
101     public int hashCode() {
102         final int prime = 31;
103         int result = 1;
104         result = prime * result + ((description == null) ? 0 : description.hashCode());
105         result = prime * result + ((name == null) ? 0 : name.hashCode());
106         result = prime * result + ((path == null) ? 0 : path.hashCode());
107         result = prime * result + ((rangeStatements == null) ? 0 : rangeStatements.hashCode());
108         result = prime * result + ((reference == null) ? 0 : reference.hashCode());
109         result = prime * result + ((units == null) ? 0 : units.hashCode());
110         return result;
111     }
112
113     @Override
114     public boolean equals(Object obj) {
115         if (this == obj) {
116             return true;
117         }
118         if (obj == null) {
119             return false;
120         }
121         if (getClass() != obj.getClass()) {
122             return false;
123         }
124         AbstractUnsignedInteger other = (AbstractUnsignedInteger) obj;
125         if (description == null) {
126             if (other.description != null) {
127                 return false;
128             }
129         } else if (!description.equals(other.description)) {
130             return false;
131         }
132         if (name == null) {
133             if (other.name != null) {
134                 return false;
135             }
136         } else if (!name.equals(other.name)) {
137             return false;
138         }
139         if (path == null) {
140             if (other.path != null) {
141                 return false;
142             }
143         } else if (!path.equals(other.path)) {
144             return false;
145         }
146         if (rangeStatements == null) {
147             if (other.rangeStatements != null) {
148                 return false;
149             }
150         } else if (!rangeStatements.equals(other.rangeStatements)) {
151             return false;
152         }
153         if (reference == null) {
154             if (other.reference != null) {
155                 return false;
156             }
157         } else if (!reference.equals(other.reference)) {
158             return false;
159         }
160         if (units == null) {
161             if (other.units != null) {
162                 return false;
163             }
164         } else if (!units.equals(other.units)) {
165             return false;
166         }
167         return true;
168     }
169
170     @Override
171     public String toString() {
172         StringBuilder builder = new StringBuilder();
173         builder.append("AbstractInteger [name=");
174         builder.append(name);
175         builder.append(", path=");
176         builder.append(path);
177         builder.append(", description=");
178         builder.append(description);
179         builder.append(", reference=");
180         builder.append(reference);
181         builder.append(", units=");
182         builder.append(units);
183         builder.append(", rangeStatements=");
184         builder.append(rangeStatements);
185         builder.append("]");
186         return builder.toString();
187     }
188 }