Add Uint constants
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint8.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.common;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.concepts.Variant;
17
18 /**
19  * Dedicated type for YANG's 'type uint8' type.
20  *
21  * @author Robert Varga
22  */
23 @Beta
24 @NonNullByDefault
25 public class Uint8 extends Number implements CanonicalValue<Uint8> {
26     public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
27         public Support() {
28             super(Uint8.class);
29         }
30
31         @Override
32         public Variant<Uint8, CanonicalValueViolation> fromString(final String str) {
33             try {
34                 return Variant.ofFirst(Uint8.valueOf(str));
35             } catch (IllegalArgumentException e) {
36                 return CanonicalValueViolation.variantOf(e);
37             }
38         }
39     }
40
41     private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
42
43     private static final short MAX_VALUE_SHORT = 255;
44     private static final String MAX_VALUE_STR = "255";
45
46     private static final long serialVersionUID = 1L;
47
48     private static final @NonNull Uint8[] CACHE;
49
50     static {
51         final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
52         for (int i = 0; i <= MAX_VALUE_SHORT; ++i) {
53             c[i] = new Uint8((byte)i);
54         }
55         CACHE = c;
56     }
57
58     /**
59      * Value of {@code 0}.
60      */
61     public static final Uint8 ZERO = valueOf(0);
62     /**
63      * Value of {@code 1}.
64      */
65     public static final Uint8 ONE = valueOf(1);
66     /**
67      * Value of {@code 2}.
68      */
69     public static final Uint8 TWO = valueOf(2);
70     /**
71      * Value of {@code 10}.
72      */
73     public static final Uint8 TEN = valueOf(10);
74     /**
75      * Value of {@code 255}.
76      */
77     public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
78
79     private final byte value;
80
81     private Uint8(final byte value) {
82         this.value = value;
83     }
84
85     protected Uint8(final Uint8 other) {
86         this(other.value);
87     }
88
89     private static Uint8 instanceFor(final byte value) {
90         return CACHE[Byte.toUnsignedInt(value)];
91     }
92
93     /**
94      * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
95      * unsigned 8-bit value.
96      *
97      * @param bits unsigned bit representation
98      * @return A Uint8 instance
99      */
100     public static Uint8 fromByteBits(final byte bits) {
101         return instanceFor(bits);
102     }
103
104     /**
105      * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
106      *
107      * @param byteVal byte value
108      * @return A Uint8 instance
109      * @throws IllegalArgumentException if byteVal is less than zero
110      */
111     public static Uint8 valueOf(final byte byteVal) {
112         UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
113         return instanceFor(byteVal);
114     }
115
116     /**
117      * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
118      * {@link #shortValue()}.
119      *
120      * @param shortVal short value
121      * @return A Uint8 instance
122      * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
123      */
124     public static Uint8 valueOf(final short shortVal) {
125         UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
126         return instanceFor((byte)shortVal);
127     }
128
129     /**
130      * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
131      *
132      * @param intVal int value
133      * @return A Uint8 instance
134      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
135      */
136     public static Uint8 valueOf(final int intVal) {
137         UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
138         return instanceFor((byte)intVal);
139     }
140
141     /**
142      * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
143      * {@link #longValue()}.
144      *
145      * @param longVal long value
146      * @return A Uint8 instance
147      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
148      */
149     public static Uint8 valueOf(final long longVal) {
150         UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
151         return instanceFor((byte)longVal);
152     }
153
154     /**
155      * Returns an {@code Uint8} corresponding to a given {@code uint}.
156      *
157      * @param uint Uint16 value
158      * @return A Uint8 instance
159      * @throws NullPointerException if uint is null
160      * @throws IllegalArgumentException if uint is greater than 255.
161      */
162     public static Uint8 valueOf(final Uint16 uint) {
163         return valueOf(uint.intValue());
164     }
165
166     /**
167      * Returns an {@code Uint8} corresponding to a given {@code uint}.
168      *
169      * @param uint Uint32 value
170      * @return A Uint8 instance
171      * @throws NullPointerException if uint is null
172      * @throws IllegalArgumentException if uint is greater than 255.
173      */
174     public static Uint8 valueOf(final Uint32 uint) {
175         return valueOf(uint.longValue());
176     }
177
178     /**
179      * Returns an {@code Uint8} corresponding to a given {@code uint}.
180      *
181      * @param uint Uint64 value
182      * @return A Uint8 instance
183      * @throws NullPointerException if uint is null
184      * @throws IllegalArgumentException if uint is greater than 255.
185      */
186     public static Uint8 valueOf(final Uint64 uint) {
187         return valueOf(uint.longValue());
188     }
189
190     /**
191      * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
192      * value.
193      *
194      * @param string String to parse
195      * @return A Uint8 instance
196      * @throws NullPointerException if string is null
197      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
198      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
199      */
200     public static Uint8 valueOf(final String string) {
201         return valueOf(string, 10);
202     }
203
204     /**
205      * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
206      * value.
207      *
208      * @param string String to parse
209      * @param radix Radix to use
210      * @return A Uint8 instance
211      * @throws NullPointerException if string is null
212      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
213      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
214      *                               {@code radix} is outside of allowed range.
215      */
216     public static Uint8 valueOf(final String string, final int radix) {
217         return valueOf(Short.parseShort(requireNonNull(string), radix));
218     }
219
220     /**
221      * {@inheritDoc}
222      *
223      * <p>
224      * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
225      * the returned value will be equal to {@code this - 2^8}.
226      */
227     @Override
228     public final byte byteValue() {
229         return value;
230     }
231
232     @Override
233     public final int intValue() {
234         return Byte.toUnsignedInt(value);
235     }
236
237     @Override
238     public final long longValue() {
239         return Byte.toUnsignedLong(value);
240     }
241
242     @Override
243     public final float floatValue() {
244         return intValue();
245     }
246
247     @Override
248     public final double doubleValue() {
249         return intValue();
250     }
251
252     @Override
253     @SuppressWarnings("checkstyle:parameterName")
254     public final int compareTo(final Uint8 o) {
255         return Byte.compareUnsigned(value, o.value);
256     }
257
258     @Override
259     public final String toCanonicalString() {
260         return Integer.toString(intValue());
261     }
262
263     @Override
264     public final CanonicalValueSupport<Uint8> support() {
265         return SUPPORT;
266     }
267
268     /**
269      * Convert this value to a {@code short}.
270      *
271      * @return A short
272      */
273     public final short toJava() {
274         return shortValue();
275     }
276
277     @Override
278     public final int hashCode() {
279         return Byte.hashCode(value);
280     }
281
282     @Override
283     public final boolean equals(final @Nullable Object obj) {
284         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
285     }
286
287     /**
288      * A slightly faster version of {@link #equals(Object)}.
289      *
290      * @param obj Uint8 object
291      * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
292      */
293     public final boolean equals(final @Nullable Uint8 obj) {
294         return this == obj || obj != null && value == obj.value;
295     }
296
297     @Override
298     public final String toString() {
299         return toCanonicalString();
300     }
301
302     private Object readResolve() {
303         return instanceFor(value);
304     }
305 }