89225876f1403359a4437ef1db14407e488a711f
[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 com.google.common.base.Optional;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Objects;
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 String REFERENCE = "https://tools.ietf.org/html/rfc6020#section-9.2";
39     private static final Optional<String> OPT_REF = Optional.of("https://tools.ietf.org/html/rfc6020#section-9.2.4");
40     private static final long MIN_VALUE = 0;
41     private final QName name;
42     private final SchemaPath path;
43     private final String description;
44     private final String units;
45     private final List<RangeConstraint> rangeStatements;
46
47     /**
48      * Construct Unsigned Integer
49      *
50      * @param name Name of type
51      * @param description Description of type
52      * @param maxRange Maximum value
53      * @param units Units
54      */
55     public AbstractUnsignedInteger(final QName name, final String description, final Number maxRange, final String units) {
56         this.name = name;
57         this.path = SchemaPath.create(true, name);
58         this.description = description;
59         this.units = units;
60         final String rangeDescription = "Integer values between " + MIN_VALUE + " and " + maxRange + ", inclusively.";
61         this.rangeStatements = Collections.singletonList(BaseConstraints.newRangeConstraint(MIN_VALUE, maxRange,
62                 Optional.of(rangeDescription), OPT_REF));
63     }
64
65     @Override
66     public UnsignedIntegerTypeDefinition getBaseType() {
67         return null;
68     }
69
70     @Override
71     public String getUnits() {
72         return units;
73     }
74
75     @Override
76     public QName getQName() {
77         return name;
78     }
79
80     @Override
81     public SchemaPath getPath() {
82         return path;
83     }
84
85     @Override
86     public String getDescription() {
87         return description;
88     }
89
90     @Override
91     public String getReference() {
92         return REFERENCE;
93     }
94
95     @Override
96     public Status getStatus() {
97         return Status.CURRENT;
98     }
99
100     @Override
101     public List<RangeConstraint> getRangeConstraints() {
102         return rangeStatements;
103     }
104
105     @Override
106     public List<UnknownSchemaNode> getUnknownSchemaNodes() {
107         return Collections.emptyList();
108     }
109
110     @Override
111     public int hashCode() {
112         final int prime = 31;
113         int result = 1;
114         result = prime * result + Objects.hashCode(description);
115         result = prime * result + Objects.hashCode(name);
116         result = prime * result + Objects.hashCode(path);
117         result = prime * result + Objects.hashCode(rangeStatements);
118         result = prime * result + Objects.hashCode(units);
119         return result;
120     }
121
122     @Override
123     public boolean equals(final Object obj) {
124         if (this == obj) {
125             return true;
126         }
127         if (obj == null) {
128             return false;
129         }
130         if (getClass() != obj.getClass()) {
131             return false;
132         }
133         AbstractUnsignedInteger other = (AbstractUnsignedInteger) obj;
134         if (!Objects.equals(description, other.description)) {
135             return false;
136         }
137         if (!Objects.equals(name, other.name)) {
138             return false;
139         }
140         if (!Objects.equals(path, other.path)) {
141             return false;
142         }
143         if (!Objects.equals(rangeStatements, other.rangeStatements)) {
144             return false;
145         }
146         if (!Objects.equals(units, other.units)) {
147             return false;
148         }
149         return true;
150     }
151
152     @Override
153     public String toString() {
154         StringBuilder builder = new StringBuilder();
155         builder.append("AbstractInteger [name=");
156         builder.append(name);
157         builder.append(", path=");
158         builder.append(path);
159         builder.append(", description=");
160         builder.append(description);
161         builder.append(", reference=");
162         builder.append(REFERENCE);
163         builder.append(", units=");
164         builder.append(units);
165         builder.append(", rangeStatements=");
166         builder.append(rangeStatements);
167         builder.append("]");
168         return builder.toString();
169     }
170 }