Bump odlparent to 13.1.3
[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 java.io.Serial;
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 {@code type uint8} type.
20  */
21 @NonNullByDefault
22 public class Uint8 extends Number implements CanonicalValue<Uint8> {
23     public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
24         public Support() {
25             super(Uint8.class);
26         }
27
28         @Override
29         public Either<Uint8, CanonicalValueViolation> fromString(final String str) {
30             try {
31                 return Either.ofFirst(Uint8.valueOf(str));
32             } catch (IllegalArgumentException e) {
33                 return CanonicalValueViolation.variantOf(e);
34             }
35         }
36     }
37
38     private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
39
40     private static final short MAX_VALUE_SHORT = 255;
41     private static final String MAX_VALUE_STR = "255";
42
43     @Serial
44     private static final long serialVersionUID = 1L;
45
46     private static final @NonNull Uint8[] CACHE;
47
48     static {
49         final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
50         for (int i = 0; i <= MAX_VALUE_SHORT; ++i) {
51             c[i] = new Uint8((byte)i);
52         }
53         CACHE = c;
54     }
55
56     /**
57      * Value of {@code 0}.
58      */
59     public static final Uint8 ZERO = valueOf(0);
60     /**
61      * Value of {@code 1}.
62      */
63     public static final Uint8 ONE = valueOf(1);
64     /**
65      * Value of {@code 2}.
66      */
67     public static final Uint8 TWO = valueOf(2);
68     /**
69      * Value of {@code 10}.
70      */
71     public static final Uint8 TEN = valueOf(10);
72     /**
73      * Value of {@code 255}.
74      */
75     public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
76
77     private final byte value;
78
79     private Uint8(final byte value) {
80         this.value = value;
81     }
82
83     protected Uint8(final Uint8 other) {
84         this(other.value);
85     }
86
87     private static Uint8 instanceFor(final byte value) {
88         return CACHE[Byte.toUnsignedInt(value)];
89     }
90
91     /**
92      * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
93      * unsigned 8-bit value.
94      *
95      * @param bits unsigned bit representation
96      * @return A Uint8 instance
97      */
98     public static Uint8 fromByteBits(final byte bits) {
99         return instanceFor(bits);
100     }
101
102     /**
103      * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
104      *
105      * @param byteVal byte value
106      * @return A Uint8 instance
107      * @throws IllegalArgumentException if byteVal is less than zero
108      */
109     public static Uint8 valueOf(final byte byteVal) {
110         UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
111         return instanceFor(byteVal);
112     }
113
114     /**
115      * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
116      * {@link #shortValue()}.
117      *
118      * @param shortVal short value
119      * @return A Uint8 instance
120      * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
121      */
122     public static Uint8 valueOf(final short shortVal) {
123         UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
124         return instanceFor((byte)shortVal);
125     }
126
127     /**
128      * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
129      *
130      * @param intVal int value
131      * @return A Uint8 instance
132      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
133      */
134     public static Uint8 valueOf(final int intVal) {
135         UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
136         return instanceFor((byte)intVal);
137     }
138
139     /**
140      * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
141      * {@link #longValue()}.
142      *
143      * @param longVal long value
144      * @return A Uint8 instance
145      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
146      */
147     public static Uint8 valueOf(final long longVal) {
148         UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
149         return instanceFor((byte)longVal);
150     }
151
152     /**
153      * Returns an {@code Uint8} corresponding to a given {@code uint}.
154      *
155      * @param uint Uint16 value
156      * @return A Uint8 instance
157      * @throws NullPointerException if uint is null
158      * @throws IllegalArgumentException if uint is greater than 255.
159      */
160     public static Uint8 valueOf(final Uint16 uint) {
161         return valueOf(uint.intValue());
162     }
163
164     /**
165      * Returns an {@code Uint8} corresponding to a given {@code uint}.
166      *
167      * @param uint Uint32 value
168      * @return A Uint8 instance
169      * @throws NullPointerException if uint is null
170      * @throws IllegalArgumentException if uint is greater than 255.
171      */
172     public static Uint8 valueOf(final Uint32 uint) {
173         return valueOf(uint.longValue());
174     }
175
176     /**
177      * Returns an {@code Uint8} corresponding to a given {@code uint}.
178      *
179      * @param uint Uint64 value
180      * @return A Uint8 instance
181      * @throws NullPointerException if uint is null
182      * @throws IllegalArgumentException if uint is greater than 255.
183      */
184     public static Uint8 valueOf(final Uint64 uint) {
185         return valueOf(uint.longValue());
186     }
187
188     /**
189      * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
190      * value.
191      *
192      * @param string String to parse
193      * @return A Uint8 instance
194      * @throws NullPointerException if string is null
195      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
196      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
197      */
198     public static Uint8 valueOf(final String string) {
199         return valueOf(string, 10);
200     }
201
202     /**
203      * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
204      * value.
205      *
206      * @param string String to parse
207      * @param radix Radix to use
208      * @return A Uint8 instance
209      * @throws NullPointerException if string is null
210      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
211      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
212      *                               {@code radix} is outside of allowed range.
213      */
214     public static Uint8 valueOf(final String string, final int radix) {
215         return valueOf(Short.parseShort(requireNonNull(string), radix));
216     }
217
218     /**
219      * Returns an {@code Uint8} corresponding to a given {@code byteVal} if it is representable. If the value is
220      * negative {@link #ZERO} will be returned.
221      *
222      * @param byteVal byte value
223      * @return A Uint8 instance
224      */
225     public static Uint8 saturatedOf(final byte byteVal) {
226         return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
227     }
228
229     /**
230      * Returns an {@code Uint8} corresponding to a given {@code shortVal} if it is representable. If the value is
231      * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
232      *
233      * @param shortVal short value
234      * @return A Uint8 instance
235      */
236     public static Uint8 saturatedOf(final short shortVal) {
237         if (shortVal <= 0) {
238             return Uint8.ZERO;
239         }
240         if (shortVal >= MAX_VALUE_SHORT) {
241             return Uint8.MAX_VALUE;
242         }
243         return instanceFor((byte) shortVal);
244     }
245
246     /**
247      * Returns an {@code Uint8} corresponding to a given {@code intVal} if it is representable. If the value is
248      * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
249      *
250      * @param intVal int value
251      * @return A Uint8 instance
252      */
253     public static Uint8 saturatedOf(final int intVal) {
254         if (intVal <= 0) {
255             return Uint8.ZERO;
256         }
257         if (intVal >= MAX_VALUE_SHORT) {
258             return Uint8.MAX_VALUE;
259         }
260         return instanceFor((byte) intVal);
261     }
262
263     /**
264      * Returns an {@code Uint8} corresponding to a given {@code longVal} if it is representable. If the value is
265      * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
266      *
267      * @param longVal long value
268      * @return A Uint8 instance
269      */
270     public static Uint8 saturatedOf(final long longVal) {
271         if (longVal <= 0) {
272             return Uint8.ZERO;
273         }
274         if (longVal >= MAX_VALUE_SHORT) {
275             return Uint8.MAX_VALUE;
276         }
277         return instanceFor((byte) longVal);
278     }
279
280     /**
281      * {@inheritDoc}
282      *
283      * <p>
284      * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
285      * the returned value will be equal to {@code this - 2^8}.
286      */
287     @Override
288     public final byte byteValue() {
289         return value;
290     }
291
292     @Override
293     public final int intValue() {
294         return Byte.toUnsignedInt(value);
295     }
296
297     @Override
298     public final long longValue() {
299         return Byte.toUnsignedLong(value);
300     }
301
302     @Override
303     public final float floatValue() {
304         return intValue();
305     }
306
307     @Override
308     public final double doubleValue() {
309         return intValue();
310     }
311
312     @Override
313     @SuppressWarnings("checkstyle:parameterName")
314     public final int compareTo(final Uint8 o) {
315         return Byte.compareUnsigned(value, o.value);
316     }
317
318     @Override
319     public final String toCanonicalString() {
320         return Integer.toString(intValue());
321     }
322
323     @Override
324     public final CanonicalValueSupport<Uint8> support() {
325         return SUPPORT;
326     }
327
328     /**
329      * Convert this value to a {@code short}.
330      *
331      * @return A short
332      */
333     public final short toJava() {
334         return shortValue();
335     }
336
337     /**
338      * Convert this value to a {@code Uint16}.
339      *
340      * @return A Uint16
341      */
342     public final Uint16 toUint16() {
343         return Uint16.fromShortBits(shortValue());
344     }
345
346     /**
347      * Convert this value to a {@code Uint32}.
348      *
349      * @return A Uint32
350      */
351     public final Uint32 toUint32() {
352         return Uint32.fromIntBits(intValue());
353     }
354
355     /**
356      * Convert this value to a {@code Uint64}.
357      *
358      * @return A Uint64
359      */
360     public final Uint64 toUint64() {
361         return Uint64.fromLongBits(longValue());
362     }
363
364     @Override
365     public final int hashCode() {
366         return Byte.hashCode(value);
367     }
368
369     @Override
370     public final boolean equals(final @Nullable Object obj) {
371         return this == obj || obj instanceof Uint8 other && value == other.value;
372     }
373
374     /**
375      * A slightly faster version of {@link #equals(Object)}.
376      *
377      * @param obj Uint8 object
378      * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
379      */
380     public final boolean equals(final @Nullable Uint8 obj) {
381         return this == obj || obj != null && value == obj.value;
382     }
383
384     @Override
385     public final String toString() {
386         return toCanonicalString();
387     }
388
389     @Serial
390     private Object readResolve() {
391         return instanceFor(value);
392     }
393 }