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