48530d7a4580609caa08754efba92232bce668b4
[yangtools.git] / common / 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.Either;
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 Either<Uint8, CanonicalValueViolation> fromString(final String str) {
33             try {
34                 return Either.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      * Returns an {@code Uint8} corresponding to a given {@code byteVal} if it is representable. If the value is
222      * negative {@link #ZERO} will be returned.
223      *
224      * @param byteVal byte value
225      * @return A Uint8 instance
226      */
227     public static Uint8 saturatedOf(final byte byteVal) {
228         return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
229     }
230
231     /**
232      * Returns an {@code Uint8} corresponding to a given {@code shortVal} if it is representable. If the value is
233      * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
234      *
235      * @param shortVal short value
236      * @return A Uint8 instance
237      */
238     public static Uint8 saturatedOf(final short shortVal) {
239         if (shortVal <= 0) {
240             return Uint8.ZERO;
241         }
242         if (shortVal >= MAX_VALUE_SHORT) {
243             return Uint8.MAX_VALUE;
244         }
245         return instanceFor((byte) shortVal);
246     }
247
248     /**
249      * Returns an {@code Uint8} corresponding to a given {@code intVal} if it is representable. If the value is
250      * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
251      *
252      * @param intVal int value
253      * @return A Uint8 instance
254      */
255     public static Uint8 saturatedOf(final int intVal) {
256         if (intVal <= 0) {
257             return Uint8.ZERO;
258         }
259         if (intVal >= MAX_VALUE_SHORT) {
260             return Uint8.MAX_VALUE;
261         }
262         return instanceFor((byte) intVal);
263     }
264
265     /**
266      * Returns an {@code Uint8} corresponding to a given {@code longVal} if it is representable. If the value is
267      * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
268      *
269      * @param longVal long value
270      * @return A Uint8 instance
271      */
272     public static Uint8 saturatedOf(final long longVal) {
273         if (longVal <= 0) {
274             return Uint8.ZERO;
275         }
276         if (longVal >= MAX_VALUE_SHORT) {
277             return Uint8.MAX_VALUE;
278         }
279         return instanceFor((byte) longVal);
280     }
281
282     /**
283      * {@inheritDoc}
284      *
285      * <p>
286      * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
287      * the returned value will be equal to {@code this - 2^8}.
288      */
289     @Override
290     public final byte byteValue() {
291         return value;
292     }
293
294     @Override
295     public final int intValue() {
296         return Byte.toUnsignedInt(value);
297     }
298
299     @Override
300     public final long longValue() {
301         return Byte.toUnsignedLong(value);
302     }
303
304     @Override
305     public final float floatValue() {
306         return intValue();
307     }
308
309     @Override
310     public final double doubleValue() {
311         return intValue();
312     }
313
314     @Override
315     @SuppressWarnings("checkstyle:parameterName")
316     public final int compareTo(final Uint8 o) {
317         return Byte.compareUnsigned(value, o.value);
318     }
319
320     @Override
321     public final String toCanonicalString() {
322         return Integer.toString(intValue());
323     }
324
325     @Override
326     public final CanonicalValueSupport<Uint8> support() {
327         return SUPPORT;
328     }
329
330     /**
331      * Convert this value to a {@code short}.
332      *
333      * @return A short
334      */
335     public final short toJava() {
336         return shortValue();
337     }
338
339     /**
340      * Convert this value to a {@code Uint16}.
341      *
342      * @return A Uint16
343      */
344     public final Uint16 toUint16() {
345         return Uint16.fromShortBits(shortValue());
346     }
347
348     /**
349      * Convert this value to a {@code Uint32}.
350      *
351      * @return A Uint32
352      */
353     public final Uint32 toUint32() {
354         return Uint32.fromIntBits(intValue());
355     }
356
357     /**
358      * Convert this value to a {@code Uint64}.
359      *
360      * @return A Uint64
361      */
362     public final Uint64 toUint64() {
363         return Uint64.fromLongBits(longValue());
364     }
365
366     @Override
367     public final int hashCode() {
368         return Byte.hashCode(value);
369     }
370
371     @Override
372     public final boolean equals(final @Nullable Object obj) {
373         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
374     }
375
376     /**
377      * A slightly faster version of {@link #equals(Object)}.
378      *
379      * @param obj Uint8 object
380      * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
381      */
382     public final boolean equals(final @Nullable Uint8 obj) {
383         return this == obj || obj != null && value == obj.value;
384     }
385
386     @Override
387     public final String toString() {
388         return toCanonicalString();
389     }
390
391     private Object readResolve() {
392         return instanceFor(value);
393     }
394 }