Convert yang-common to a JPMS module
[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     public static final Uint8 ZERO = valueOf(0);
59     public static final Uint8 ONE = valueOf(1);
60     public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
61
62     private final byte value;
63
64     private Uint8(final byte value) {
65         this.value = value;
66     }
67
68     protected Uint8(final Uint8 other) {
69         this(other.value);
70     }
71
72     private static Uint8 instanceFor(final byte value) {
73         return CACHE[Byte.toUnsignedInt(value)];
74     }
75
76     /**
77      * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
78      * unsigned 8-bit value.
79      *
80      * @param bits unsigned bit representation
81      * @return A Uint8 instance
82      */
83     public static Uint8 fromByteBits(final byte bits) {
84         return instanceFor(bits);
85     }
86
87     /**
88      * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
89      *
90      * @param byteVal byte value
91      * @return A Uint8 instance
92      * @throws IllegalArgumentException if byteVal is less than zero
93      */
94     public static Uint8 valueOf(final byte byteVal) {
95         UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
96         return instanceFor(byteVal);
97     }
98
99     /**
100      * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
101      * {@link #shortValue()}.
102      *
103      * @param shortVal short value
104      * @return A Uint8 instance
105      * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
106      */
107     public static Uint8 valueOf(final short shortVal) {
108         UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
109         return instanceFor((byte)shortVal);
110     }
111
112     /**
113      * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
114      *
115      * @param intVal int value
116      * @return A Uint8 instance
117      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
118      */
119     public static Uint8 valueOf(final int intVal) {
120         UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
121         return instanceFor((byte)intVal);
122     }
123
124     /**
125      * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
126      * {@link #longValue()}.
127      *
128      * @param longVal long value
129      * @return A Uint8 instance
130      * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
131      */
132     public static Uint8 valueOf(final long longVal) {
133         UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
134         return instanceFor((byte)longVal);
135     }
136
137     /**
138      * Returns an {@code Uint8} corresponding to a given {@code uint}.
139      *
140      * @param uint Uint16 value
141      * @return A Uint8 instance
142      * @throws NullPointerException if uint is null
143      * @throws IllegalArgumentException if uint is greater than 255.
144      */
145     public static Uint8 valueOf(final Uint16 uint) {
146         return valueOf(uint.intValue());
147     }
148
149     /**
150      * Returns an {@code Uint8} corresponding to a given {@code uint}.
151      *
152      * @param uint Uint32 value
153      * @return A Uint8 instance
154      * @throws NullPointerException if uint is null
155      * @throws IllegalArgumentException if uint is greater than 255.
156      */
157     public static Uint8 valueOf(final Uint32 uint) {
158         return valueOf(uint.longValue());
159     }
160
161     /**
162      * Returns an {@code Uint8} corresponding to a given {@code uint}.
163      *
164      * @param uint Uint64 value
165      * @return A Uint8 instance
166      * @throws NullPointerException if uint is null
167      * @throws IllegalArgumentException if uint is greater than 255.
168      */
169     public static Uint8 valueOf(final Uint64 uint) {
170         return valueOf(uint.longValue());
171     }
172
173     /**
174      * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
175      * value.
176      *
177      * @param string String to parse
178      * @return A Uint8 instance
179      * @throws NullPointerException if string is null
180      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
181      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
182      */
183     public static Uint8 valueOf(final String string) {
184         return valueOf(string, 10);
185     }
186
187     /**
188      * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
189      * value.
190      *
191      * @param string String to parse
192      * @param radix Radix to use
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, or if the
197      *                               {@code radix} is outside of allowed range.
198      */
199     public static Uint8 valueOf(final String string, final int radix) {
200         return valueOf(Short.parseShort(requireNonNull(string), radix));
201     }
202
203     /**
204      * {@inheritDoc}
205      *
206      * <p>
207      * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
208      * the returned value will be equal to {@code this - 2^8}.
209      */
210     @Override
211     public final byte byteValue() {
212         return value;
213     }
214
215     @Override
216     public final int intValue() {
217         return Byte.toUnsignedInt(value);
218     }
219
220     @Override
221     public final long longValue() {
222         return Byte.toUnsignedLong(value);
223     }
224
225     @Override
226     public final float floatValue() {
227         return intValue();
228     }
229
230     @Override
231     public final double doubleValue() {
232         return intValue();
233     }
234
235     @Override
236     @SuppressWarnings("checkstyle:parameterName")
237     public final int compareTo(final Uint8 o) {
238         return Byte.compareUnsigned(value, o.value);
239     }
240
241     @Override
242     public final String toCanonicalString() {
243         return Integer.toString(intValue());
244     }
245
246     @Override
247     public final CanonicalValueSupport<Uint8> support() {
248         return SUPPORT;
249     }
250
251     /**
252      * Convert this value to a {@code short}.
253      *
254      * @return A short
255      */
256     public final short toJava() {
257         return shortValue();
258     }
259
260     @Override
261     public final int hashCode() {
262         return Byte.hashCode(value);
263     }
264
265     @Override
266     public final boolean equals(final @Nullable Object obj) {
267         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
268     }
269
270     @Override
271     public final String toString() {
272         return toCanonicalString();
273     }
274
275     private Object readResolve() {
276         return instanceFor(value);
277     }
278 }