Added support for parsing yang models with already resolved context.
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-util / src / main / java / org / opendaylight / controller / yang / model / util / AbstractUnsignedInteger.java
1 /**
2  *
3  */
4 package org.opendaylight.controller.yang.model.util;
5
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.List;
9
10 import org.opendaylight.controller.yang.common.QName;
11 import org.opendaylight.controller.yang.model.api.SchemaPath;
12 import org.opendaylight.controller.yang.model.api.Status;
13 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
14 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
15 import org.opendaylight.controller.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 actualPath
45      * @param namespace
46      * @param revision
47      * @param name
48      * @param description
49      * @param MIN_VALUE
50      * @param maxRange
51      * @param units
52      */
53     public AbstractUnsignedInteger(final SchemaPath path, final QName name, final String description,
54             final Number maxRange, final String units) {
55         this.name = name;
56         this.description = description;
57         this.path = path;
58         this.units = units;
59         this.rangeStatements = new ArrayList<RangeConstraint>();
60         final String rangeDescription = "Integer values between " + MIN_VALUE + " and " + maxRange + ", inclusively.";
61         this.rangeStatements.add(BaseConstraints.rangeConstraint(MIN_VALUE, maxRange, rangeDescription,
62                 "https://tools.ietf.org/html/rfc6020#section-9.2.4"));
63     }
64
65     /**
66      * @param name
67      * @param description
68      * @param rangeStatements
69      * @param units
70      */
71     public AbstractUnsignedInteger(final SchemaPath path, final QName name, final String description,
72             final List<RangeConstraint> rangeStatements, final String units) {
73         this.name = name;
74         this.description = description;
75         this.path = path;
76         this.units = units;
77         this.rangeStatements = rangeStatements;
78     }
79
80     @Override
81     public String getUnits() {
82         return units;
83     }
84
85     @Override
86     public QName getQName() {
87         return name;
88     }
89
90     @Override
91     public SchemaPath getPath() {
92         return path;
93     }
94
95     @Override
96     public String getDescription() {
97         return description;
98     }
99
100     @Override
101     public String getReference() {
102         return reference;
103     }
104
105     @Override
106     public Status getStatus() {
107         return Status.CURRENT;
108     }
109
110     @Override
111     public List<RangeConstraint> getRangeStatements() {
112         return rangeStatements;
113     }
114
115     @Override
116     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
117         return Collections.emptyList();
118     }
119
120     @Override
121     public int hashCode() {
122         final int prime = 31;
123         int result = 1;
124         result = prime * result + ((description == null) ? 0 : description.hashCode());
125         result = prime * result + ((name == null) ? 0 : name.hashCode());
126         result = prime * result + ((path == null) ? 0 : path.hashCode());
127         result = prime * result + ((rangeStatements == null) ? 0 : rangeStatements.hashCode());
128         result = prime * result + ((reference == null) ? 0 : reference.hashCode());
129         result = prime * result + ((units == null) ? 0 : units.hashCode());
130         return result;
131     }
132
133     @Override
134     public boolean equals(Object obj) {
135         if (this == obj) {
136             return true;
137         }
138         if (obj == null) {
139             return false;
140         }
141         if (getClass() != obj.getClass()) {
142             return false;
143         }
144         AbstractUnsignedInteger other = (AbstractUnsignedInteger) obj;
145         if (description == null) {
146             if (other.description != null) {
147                 return false;
148             }
149         } else if (!description.equals(other.description)) {
150             return false;
151         }
152         if (name == null) {
153             if (other.name != null) {
154                 return false;
155             }
156         } else if (!name.equals(other.name)) {
157             return false;
158         }
159         if (path == null) {
160             if (other.path != null) {
161                 return false;
162             }
163         } else if (!path.equals(other.path)) {
164             return false;
165         }
166         if (rangeStatements == null) {
167             if (other.rangeStatements != null) {
168                 return false;
169             }
170         } else if (!rangeStatements.equals(other.rangeStatements)) {
171             return false;
172         }
173         if (reference == null) {
174             if (other.reference != null) {
175                 return false;
176             }
177         } else if (!reference.equals(other.reference)) {
178             return false;
179         }
180         if (units == null) {
181             if (other.units != null) {
182                 return false;
183             }
184         } else if (!units.equals(other.units)) {
185             return false;
186         }
187         return true;
188     }
189
190     @Override
191     public String toString() {
192         StringBuilder builder = new StringBuilder();
193         builder.append("AbstractInteger [name=");
194         builder.append(name);
195         builder.append(", path=");
196         builder.append(path);
197         builder.append(", description=");
198         builder.append(description);
199         builder.append(", reference=");
200         builder.append(reference);
201         builder.append(", units=");
202         builder.append(units);
203         builder.append(", rangeStatements=");
204         builder.append(rangeStatements);
205         builder.append("]");
206         return builder.toString();
207     }
208 }