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 java.io.Serial;
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.Either;
19 * Dedicated type for YANG's {@code type uint8} type.
22 public class Uint8 extends Number implements CanonicalValue<Uint8> {
23 public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
29 public Either<Uint8, CanonicalValueViolation> fromString(final String str) {
31 return Either.ofFirst(Uint8.valueOf(str));
32 } catch (IllegalArgumentException e) {
33 return CanonicalValueViolation.variantOf(e);
38 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
40 private static final short MAX_VALUE_SHORT = 255;
41 private static final String MAX_VALUE_STR = "255";
44 private static final long serialVersionUID = 1L;
46 private static final @NonNull Uint8[] CACHE;
49 final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
50 for (int i = 0; i <= MAX_VALUE_SHORT; ++i) {
51 c[i] = new Uint8((byte)i);
59 public static final Uint8 ZERO = valueOf(0);
63 public static final Uint8 ONE = valueOf(1);
67 public static final Uint8 TWO = valueOf(2);
69 * Value of {@code 10}.
71 public static final Uint8 TEN = valueOf(10);
73 * Value of {@code 255}.
75 public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
77 private final byte value;
79 private Uint8(final byte value) {
83 protected Uint8(final Uint8 other) {
87 private static Uint8 instanceFor(final byte value) {
88 return CACHE[Byte.toUnsignedInt(value)];
92 * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
93 * unsigned 8-bit value.
95 * @param bits unsigned bit representation
96 * @return A Uint8 instance
98 public static Uint8 fromByteBits(final byte bits) {
99 return instanceFor(bits);
103 * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
105 * @param byteVal byte value
106 * @return A Uint8 instance
107 * @throws IllegalArgumentException if byteVal is less than zero
109 public static Uint8 valueOf(final byte byteVal) {
110 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
111 return instanceFor(byteVal);
115 * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
116 * {@link #shortValue()}.
118 * @param shortVal short value
119 * @return A Uint8 instance
120 * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
122 public static Uint8 valueOf(final short shortVal) {
123 UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
124 return instanceFor((byte)shortVal);
128 * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
130 * @param intVal int value
131 * @return A Uint8 instance
132 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
134 public static Uint8 valueOf(final int intVal) {
135 UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
136 return instanceFor((byte)intVal);
140 * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
141 * {@link #longValue()}.
143 * @param longVal long value
144 * @return A Uint8 instance
145 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
147 public static Uint8 valueOf(final long longVal) {
148 UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
149 return instanceFor((byte)longVal);
153 * Returns an {@code Uint8} corresponding to a given {@code uint}.
155 * @param uint Uint16 value
156 * @return A Uint8 instance
157 * @throws NullPointerException if uint is null
158 * @throws IllegalArgumentException if uint is greater than 255.
160 public static Uint8 valueOf(final Uint16 uint) {
161 return valueOf(uint.intValue());
165 * Returns an {@code Uint8} corresponding to a given {@code uint}.
167 * @param uint Uint32 value
168 * @return A Uint8 instance
169 * @throws NullPointerException if uint is null
170 * @throws IllegalArgumentException if uint is greater than 255.
172 public static Uint8 valueOf(final Uint32 uint) {
173 return valueOf(uint.longValue());
177 * Returns an {@code Uint8} corresponding to a given {@code uint}.
179 * @param uint Uint64 value
180 * @return A Uint8 instance
181 * @throws NullPointerException if uint is null
182 * @throws IllegalArgumentException if uint is greater than 255.
184 public static Uint8 valueOf(final Uint64 uint) {
185 return valueOf(uint.longValue());
189 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
192 * @param string String to parse
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.
198 public static Uint8 valueOf(final String string) {
199 return valueOf(string, 10);
203 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
206 * @param string String to parse
207 * @param radix Radix to use
208 * @return A Uint8 instance
209 * @throws NullPointerException if string is null
210 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
211 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
212 * {@code radix} is outside of allowed range.
214 public static Uint8 valueOf(final String string, final int radix) {
215 return valueOf(Short.parseShort(requireNonNull(string), radix));
219 * Returns an {@code Uint8} corresponding to a given {@code byteVal} if it is representable. If the value is
220 * negative {@link #ZERO} will be returned.
222 * @param byteVal byte value
223 * @return A Uint8 instance
225 public static Uint8 saturatedOf(final byte byteVal) {
226 return byteVal <= 0 ? Uint8.ZERO : instanceFor(byteVal);
230 * Returns an {@code Uint8} corresponding to a given {@code shortVal} if it is representable. If the value is
231 * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
233 * @param shortVal short value
234 * @return A Uint8 instance
236 public static Uint8 saturatedOf(final short shortVal) {
240 if (shortVal >= MAX_VALUE_SHORT) {
241 return Uint8.MAX_VALUE;
243 return instanceFor((byte) shortVal);
247 * Returns an {@code Uint8} corresponding to a given {@code intVal} if it is representable. If the value is
248 * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
250 * @param intVal int value
251 * @return A Uint8 instance
253 public static Uint8 saturatedOf(final int intVal) {
257 if (intVal >= MAX_VALUE_SHORT) {
258 return Uint8.MAX_VALUE;
260 return instanceFor((byte) intVal);
264 * Returns an {@code Uint8} corresponding to a given {@code longVal} if it is representable. If the value is
265 * negative {@link #ZERO} will be returned. If the value is greater than 255, {@link #MAX_VALUE} will be returned.
267 * @param longVal long value
268 * @return A Uint8 instance
270 public static Uint8 saturatedOf(final long longVal) {
274 if (longVal >= MAX_VALUE_SHORT) {
275 return Uint8.MAX_VALUE;
277 return instanceFor((byte) longVal);
284 * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
285 * the returned value will be equal to {@code this - 2^8}.
288 public final byte byteValue() {
293 public final int intValue() {
294 return Byte.toUnsignedInt(value);
298 public final long longValue() {
299 return Byte.toUnsignedLong(value);
303 public final float floatValue() {
308 public final double doubleValue() {
313 @SuppressWarnings("checkstyle:parameterName")
314 public final int compareTo(final Uint8 o) {
315 return Byte.compareUnsigned(value, o.value);
319 public final String toCanonicalString() {
320 return Integer.toString(intValue());
324 public final CanonicalValueSupport<Uint8> support() {
329 * Convert this value to a {@code short}.
333 public final short toJava() {
338 * Convert this value to a {@code Uint16}.
342 public final Uint16 toUint16() {
343 return Uint16.fromShortBits(shortValue());
347 * Convert this value to a {@code Uint32}.
351 public final Uint32 toUint32() {
352 return Uint32.fromIntBits(intValue());
356 * Convert this value to a {@code Uint64}.
360 public final Uint64 toUint64() {
361 return Uint64.fromLongBits(longValue());
365 public final int hashCode() {
366 return Byte.hashCode(value);
370 public final boolean equals(final @Nullable Object obj) {
371 return this == obj || obj instanceof Uint8 other && value == other.value;
375 * A slightly faster version of {@link #equals(Object)}.
377 * @param obj Uint8 object
378 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
380 public final boolean equals(final @Nullable Uint8 obj) {
381 return this == obj || obj != null && value == obj.value;
385 public final String toString() {
386 return toCanonicalString();
390 private Object readResolve() {
391 return instanceFor(value);