2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static java.util.Objects.requireNonNull;
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;
19 * Dedicated type for YANG's 'type uint8' type.
21 * @author Robert Varga
25 public class Uint8 extends Number implements CanonicalValue<Uint8> {
26 public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
32 public Variant<Uint8, CanonicalValueViolation> fromString(final String str) {
34 return Variant.ofFirst(Uint8.valueOf(str));
35 } catch (IllegalArgumentException e) {
36 return CanonicalValueViolation.variantOf(e);
41 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
43 private static final short MAX_VALUE_SHORT = 255;
44 private static final String MAX_VALUE_STR = "255";
46 private static final long serialVersionUID = 1L;
48 private static final @NonNull Uint8[] CACHE;
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);
61 public static final Uint8 ZERO = valueOf(0);
65 public static final Uint8 ONE = valueOf(1);
69 public static final Uint8 TWO = valueOf(2);
71 * Value of {@code 10}.
73 public static final Uint8 TEN = valueOf(10);
75 * Value of {@code 255}.
77 public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
79 private final byte value;
81 private Uint8(final byte value) {
85 protected Uint8(final Uint8 other) {
89 private static Uint8 instanceFor(final byte value) {
90 return CACHE[Byte.toUnsignedInt(value)];
94 * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
95 * unsigned 8-bit value.
97 * @param bits unsigned bit representation
98 * @return A Uint8 instance
100 public static Uint8 fromByteBits(final byte bits) {
101 return instanceFor(bits);
105 * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
107 * @param byteVal byte value
108 * @return A Uint8 instance
109 * @throws IllegalArgumentException if byteVal is less than zero
111 public static Uint8 valueOf(final byte byteVal) {
112 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
113 return instanceFor(byteVal);
117 * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
118 * {@link #shortValue()}.
120 * @param shortVal short value
121 * @return A Uint8 instance
122 * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
124 public static Uint8 valueOf(final short shortVal) {
125 UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
126 return instanceFor((byte)shortVal);
130 * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
132 * @param intVal int value
133 * @return A Uint8 instance
134 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
136 public static Uint8 valueOf(final int intVal) {
137 UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
138 return instanceFor((byte)intVal);
142 * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
143 * {@link #longValue()}.
145 * @param longVal long value
146 * @return A Uint8 instance
147 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
149 public static Uint8 valueOf(final long longVal) {
150 UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
151 return instanceFor((byte)longVal);
155 * Returns an {@code Uint8} corresponding to a given {@code uint}.
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.
162 public static Uint8 valueOf(final Uint16 uint) {
163 return valueOf(uint.intValue());
167 * Returns an {@code Uint8} corresponding to a given {@code uint}.
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.
174 public static Uint8 valueOf(final Uint32 uint) {
175 return valueOf(uint.longValue());
179 * Returns an {@code Uint8} corresponding to a given {@code uint}.
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.
186 public static Uint8 valueOf(final Uint64 uint) {
187 return valueOf(uint.longValue());
191 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
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.
200 public static Uint8 valueOf(final String string) {
201 return valueOf(string, 10);
205 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
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.
216 public static Uint8 valueOf(final String string, final int radix) {
217 return valueOf(Short.parseShort(requireNonNull(string), radix));
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.
224 * @param byteVal byte value
225 * @return A Uint8 instance
227 public static Uint8 saturatedOf(final byte byteVal) {
228 return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
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.
235 * @param shortVal short value
236 * @return A Uint8 instance
238 public static Uint8 saturatedOf(final short shortVal) {
242 if (shortVal >= MAX_VALUE_SHORT) {
243 return Uint8.MAX_VALUE;
245 return instanceFor((byte) shortVal);
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.
252 * @param intVal int value
253 * @return A Uint8 instance
255 public static Uint8 saturatedOf(final int intVal) {
259 if (intVal >= MAX_VALUE_SHORT) {
260 return Uint8.MAX_VALUE;
262 return instanceFor((byte) intVal);
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.
269 * @param longVal long value
270 * @return A Uint8 instance
272 public static Uint8 saturatedOf(final long longVal) {
276 if (longVal >= MAX_VALUE_SHORT) {
277 return Uint8.MAX_VALUE;
279 return instanceFor((byte) longVal);
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}.
290 public final byte byteValue() {
295 public final int intValue() {
296 return Byte.toUnsignedInt(value);
300 public final long longValue() {
301 return Byte.toUnsignedLong(value);
305 public final float floatValue() {
310 public final double doubleValue() {
315 @SuppressWarnings("checkstyle:parameterName")
316 public final int compareTo(final Uint8 o) {
317 return Byte.compareUnsigned(value, o.value);
321 public final String toCanonicalString() {
322 return Integer.toString(intValue());
326 public final CanonicalValueSupport<Uint8> support() {
331 * Convert this value to a {@code short}.
335 public final short toJava() {
340 * Convert this value to a {@code Uint16}.
344 public final Uint16 toUint16() {
345 return Uint16.fromShortBits(shortValue());
349 * Convert this value to a {@code Uint32}.
353 public final Uint32 toUint32() {
354 return Uint32.fromIntBits(intValue());
358 * Convert this value to a {@code Uint64}.
362 public final Uint64 toUint64() {
363 return Uint64.fromLongBits(longValue());
367 public final int hashCode() {
368 return Byte.hashCode(value);
372 public final boolean equals(final @Nullable Object obj) {
373 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
377 * A slightly faster version of {@link #equals(Object)}.
379 * @param obj Uint8 object
380 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
382 public final boolean equals(final @Nullable Uint8 obj) {
383 return this == obj || obj != null && value == obj.value;
387 public final String toString() {
388 return toCanonicalString();
391 private Object readResolve() {
392 return instanceFor(value);