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 java.io.Serial;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.concepts.Either;
20 * Dedicated type for YANG's 'type uint8' type.
22 * @author Robert Varga
26 public class Uint8 extends Number implements CanonicalValue<Uint8> {
27 public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
33 public Either<Uint8, CanonicalValueViolation> fromString(final String str) {
35 return Either.ofFirst(Uint8.valueOf(str));
36 } catch (IllegalArgumentException e) {
37 return CanonicalValueViolation.variantOf(e);
42 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
44 private static final short MAX_VALUE_SHORT = 255;
45 private static final String MAX_VALUE_STR = "255";
48 private static final long serialVersionUID = 1L;
50 private static final @NonNull Uint8[] CACHE;
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);
63 public static final Uint8 ZERO = valueOf(0);
67 public static final Uint8 ONE = valueOf(1);
71 public static final Uint8 TWO = valueOf(2);
73 * Value of {@code 10}.
75 public static final Uint8 TEN = valueOf(10);
77 * Value of {@code 255}.
79 public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
81 private final byte value;
83 private Uint8(final byte value) {
87 protected Uint8(final Uint8 other) {
91 private static Uint8 instanceFor(final byte value) {
92 return CACHE[Byte.toUnsignedInt(value)];
96 * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
97 * unsigned 8-bit value.
99 * @param bits unsigned bit representation
100 * @return A Uint8 instance
102 public static Uint8 fromByteBits(final byte bits) {
103 return instanceFor(bits);
107 * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
109 * @param byteVal byte value
110 * @return A Uint8 instance
111 * @throws IllegalArgumentException if byteVal is less than zero
113 public static Uint8 valueOf(final byte byteVal) {
114 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
115 return instanceFor(byteVal);
119 * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
120 * {@link #shortValue()}.
122 * @param shortVal short value
123 * @return A Uint8 instance
124 * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
126 public static Uint8 valueOf(final short shortVal) {
127 UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
128 return instanceFor((byte)shortVal);
132 * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
134 * @param intVal int value
135 * @return A Uint8 instance
136 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
138 public static Uint8 valueOf(final int intVal) {
139 UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
140 return instanceFor((byte)intVal);
144 * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
145 * {@link #longValue()}.
147 * @param longVal long value
148 * @return A Uint8 instance
149 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
151 public static Uint8 valueOf(final long longVal) {
152 UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
153 return instanceFor((byte)longVal);
157 * Returns an {@code Uint8} corresponding to a given {@code uint}.
159 * @param uint Uint16 value
160 * @return A Uint8 instance
161 * @throws NullPointerException if uint is null
162 * @throws IllegalArgumentException if uint is greater than 255.
164 public static Uint8 valueOf(final Uint16 uint) {
165 return valueOf(uint.intValue());
169 * Returns an {@code Uint8} corresponding to a given {@code uint}.
171 * @param uint Uint32 value
172 * @return A Uint8 instance
173 * @throws NullPointerException if uint is null
174 * @throws IllegalArgumentException if uint is greater than 255.
176 public static Uint8 valueOf(final Uint32 uint) {
177 return valueOf(uint.longValue());
181 * Returns an {@code Uint8} corresponding to a given {@code uint}.
183 * @param uint Uint64 value
184 * @return A Uint8 instance
185 * @throws NullPointerException if uint is null
186 * @throws IllegalArgumentException if uint is greater than 255.
188 public static Uint8 valueOf(final Uint64 uint) {
189 return valueOf(uint.longValue());
193 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
196 * @param string String to parse
197 * @return A Uint8 instance
198 * @throws NullPointerException if string is null
199 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
200 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
202 public static Uint8 valueOf(final String string) {
203 return valueOf(string, 10);
207 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
210 * @param string String to parse
211 * @param radix Radix to use
212 * @return A Uint8 instance
213 * @throws NullPointerException if string is null
214 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
215 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
216 * {@code radix} is outside of allowed range.
218 public static Uint8 valueOf(final String string, final int radix) {
219 return valueOf(Short.parseShort(requireNonNull(string), radix));
223 * Returns an {@code Uint8} corresponding to a given {@code byteVal} if it is representable. If the value is
224 * negative {@link #ZERO} will be returned.
226 * @param byteVal byte value
227 * @return A Uint8 instance
229 public static Uint8 saturatedOf(final byte byteVal) {
230 return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
234 * Returns an {@code Uint8} corresponding to a given {@code shortVal} if it is representable. If the value is
235 * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
237 * @param shortVal short value
238 * @return A Uint8 instance
240 public static Uint8 saturatedOf(final short shortVal) {
244 if (shortVal >= MAX_VALUE_SHORT) {
245 return Uint8.MAX_VALUE;
247 return instanceFor((byte) shortVal);
251 * Returns an {@code Uint8} corresponding to a given {@code intVal} if it is representable. If the value is
252 * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
254 * @param intVal int value
255 * @return A Uint8 instance
257 public static Uint8 saturatedOf(final int intVal) {
261 if (intVal >= MAX_VALUE_SHORT) {
262 return Uint8.MAX_VALUE;
264 return instanceFor((byte) intVal);
268 * Returns an {@code Uint8} corresponding to a given {@code longVal} if it is representable. If the value is
269 * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
271 * @param longVal long value
272 * @return A Uint8 instance
274 public static Uint8 saturatedOf(final long longVal) {
278 if (longVal >= MAX_VALUE_SHORT) {
279 return Uint8.MAX_VALUE;
281 return instanceFor((byte) longVal);
288 * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
289 * the returned value will be equal to {@code this - 2^8}.
292 public final byte byteValue() {
297 public final int intValue() {
298 return Byte.toUnsignedInt(value);
302 public final long longValue() {
303 return Byte.toUnsignedLong(value);
307 public final float floatValue() {
312 public final double doubleValue() {
317 @SuppressWarnings("checkstyle:parameterName")
318 public final int compareTo(final Uint8 o) {
319 return Byte.compareUnsigned(value, o.value);
323 public final String toCanonicalString() {
324 return Integer.toString(intValue());
328 public final CanonicalValueSupport<Uint8> support() {
333 * Convert this value to a {@code short}.
337 public final short toJava() {
342 * Convert this value to a {@code Uint16}.
346 public final Uint16 toUint16() {
347 return Uint16.fromShortBits(shortValue());
351 * Convert this value to a {@code Uint32}.
355 public final Uint32 toUint32() {
356 return Uint32.fromIntBits(intValue());
360 * Convert this value to a {@code Uint64}.
364 public final Uint64 toUint64() {
365 return Uint64.fromLongBits(longValue());
369 public final int hashCode() {
370 return Byte.hashCode(value);
374 public final boolean equals(final @Nullable Object obj) {
375 return this == obj || obj instanceof Uint8 other && value == other.value;
379 * A slightly faster version of {@link #equals(Object)}.
381 * @param obj Uint8 object
382 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
384 public final boolean equals(final @Nullable Uint8 obj) {
385 return this == obj || obj != null && value == obj.value;
389 public final String toString() {
390 return toCanonicalString();
394 private Object readResolve() {
395 return instanceFor(value);