04ee7cecd203e7dec3c7f12c99dae6f566bb2cbd
[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 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 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> getRangeConstraints() {
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 + ((units == null) ? 0 : units.hashCode());
109         return result;
110     }
111
112     @Override
113     public boolean equals(Object obj) {
114         if (this == obj) {
115             return true;
116         }
117         if (obj == null) {
118             return false;
119         }
120         if (getClass() != obj.getClass()) {
121             return false;
122         }
123         AbstractUnsignedInteger other = (AbstractUnsignedInteger) obj;
124         if (description == null) {
125             if (other.description != null) {
126                 return false;
127             }
128         } else if (!description.equals(other.description)) {
129             return false;
130         }
131         if (name == null) {
132             if (other.name != null) {
133                 return false;
134             }
135         } else if (!name.equals(other.name)) {
136             return false;
137         }
138         if (path == null) {
139             if (other.path != null) {
140                 return false;
141             }
142         } else if (!path.equals(other.path)) {
143             return false;
144         }
145         if (rangeStatements == null) {
146             if (other.rangeStatements != null) {
147                 return false;
148             }
149         } else if (!rangeStatements.equals(other.rangeStatements)) {
150             return false;
151         }
152         if (units == null) {
153             if (other.units != null) {
154                 return false;
155             }
156         } else if (!units.equals(other.units)) {
157             return false;
158         }
159         return true;
160     }
161
162     @Override
163     public String toString() {
164         StringBuilder builder = new StringBuilder();
165         builder.append("AbstractInteger [name=");
166         builder.append(name);
167         builder.append(", path=");
168         builder.append(path);
169         builder.append(", description=");
170         builder.append(description);
171         builder.append(", reference=");
172         builder.append(REFERENCE);
173         builder.append(", units=");
174         builder.append(units);
175         builder.append(", rangeStatements=");
176         builder.append(rangeStatements);
177         builder.append("]");
178         return builder.toString();
179     }
180 }