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