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);
58 public static final Uint8 ZERO = valueOf(0);
59 public static final Uint8 ONE = valueOf(1);
60 public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
62 private final byte value;
64 private Uint8(final byte value) {
68 protected Uint8(final Uint8 other) {
72 private static Uint8 instanceFor(final byte value) {
73 return CACHE[Byte.toUnsignedInt(value)];
77 * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
78 * unsigned 8-bit value.
80 * @param bits unsigned bit representation
81 * @return A Uint8 instance
83 public static Uint8 fromByteBits(final byte bits) {
84 return instanceFor(bits);
88 * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
90 * @param byteVal byte value
91 * @return A Uint8 instance
92 * @throws IllegalArgumentException if byteVal is less than zero
94 public static Uint8 valueOf(final byte byteVal) {
95 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
96 return instanceFor(byteVal);
100 * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
101 * {@link #shortValue()}.
103 * @param shortVal short value
104 * @return A Uint8 instance
105 * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
107 public static Uint8 valueOf(final short shortVal) {
108 UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
109 return instanceFor((byte)shortVal);
113 * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
115 * @param intVal int value
116 * @return A Uint8 instance
117 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
119 public static Uint8 valueOf(final int intVal) {
120 UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
121 return instanceFor((byte)intVal);
125 * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
126 * {@link #longValue()}.
128 * @param longVal long value
129 * @return A Uint8 instance
130 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
132 public static Uint8 valueOf(final long longVal) {
133 UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
134 return instanceFor((byte)longVal);
138 * Returns an {@code Uint8} corresponding to a given {@code uint}.
140 * @param uint Uint16 value
141 * @return A Uint8 instance
142 * @throws NullPointerException if uint is null
143 * @throws IllegalArgumentException if uint is greater than 255.
145 public static Uint8 valueOf(final Uint16 uint) {
146 return valueOf(uint.intValue());
150 * Returns an {@code Uint8} corresponding to a given {@code uint}.
152 * @param uint Uint32 value
153 * @return A Uint8 instance
154 * @throws NullPointerException if uint is null
155 * @throws IllegalArgumentException if uint is greater than 255.
157 public static Uint8 valueOf(final Uint32 uint) {
158 return valueOf(uint.longValue());
162 * Returns an {@code Uint8} corresponding to a given {@code uint}.
164 * @param uint Uint64 value
165 * @return A Uint8 instance
166 * @throws NullPointerException if uint is null
167 * @throws IllegalArgumentException if uint is greater than 255.
169 public static Uint8 valueOf(final Uint64 uint) {
170 return valueOf(uint.longValue());
174 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
177 * @param string String to parse
178 * @return A Uint8 instance
179 * @throws NullPointerException if string is null
180 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
181 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
183 public static Uint8 valueOf(final String string) {
184 return valueOf(string, 10);
188 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
191 * @param string String to parse
192 * @param radix Radix to use
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, or if the
197 * {@code radix} is outside of allowed range.
199 public static Uint8 valueOf(final String string, final int radix) {
200 return valueOf(Short.parseShort(requireNonNull(string), radix));
207 * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
208 * the returned value will be equal to {@code this - 2^8}.
211 public final byte byteValue() {
216 public final int intValue() {
217 return Byte.toUnsignedInt(value);
221 public final long longValue() {
222 return Byte.toUnsignedLong(value);
226 public final float floatValue() {
231 public final double doubleValue() {
236 @SuppressWarnings("checkstyle:parameterName")
237 public final int compareTo(final Uint8 o) {
238 return Byte.compareUnsigned(value, o.value);
242 public final String toCanonicalString() {
243 return Integer.toString(intValue());
247 public final CanonicalValueSupport<Uint8> support() {
252 * Convert this value to a {@code short}.
256 public final short toJava() {
261 public final int hashCode() {
262 return Byte.hashCode(value);
266 public final boolean equals(final @Nullable Object obj) {
267 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
271 public final String toString() {
272 return toCanonicalString();
275 private Object readResolve() {
276 return instanceFor(value);