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