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.kohsuke.MetaInfServices;
17 import org.opendaylight.yangtools.concepts.Variant;
20 * Dedicated type for YANG's 'type uint8' type.
22 * @author Robert Varga
26 public class Uint8 extends Number implements CanonicalValue<Uint8> {
27 @MetaInfServices(value = CanonicalValueSupport.class)
28 public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
34 public Variant<Uint8, CanonicalValueViolation> fromString(final String str) {
36 return Variant.ofFirst(Uint8.valueOf(str));
37 } catch (IllegalArgumentException e) {
38 return CanonicalValueViolation.variantOf(e);
43 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
45 private static final short MAX_VALUE_SHORT = 255;
46 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);
60 public static final Uint8 ZERO = valueOf(0);
61 public static final Uint8 ONE = valueOf(1);
62 public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
64 private final byte value;
66 private Uint8(final byte value) {
70 protected Uint8(final Uint8 other) {
74 private static Uint8 instanceFor(final byte value) {
75 return CACHE[Byte.toUnsignedInt(value)];
79 * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
80 * unsigned 8-bit value.
82 * @param bits unsigned bit representation
83 * @return A Uint8 instance
85 public static Uint8 fromByteBits(final byte bits) {
86 return instanceFor(bits);
90 * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
92 * @param byteVal byte value
93 * @return A Uint8 instance
94 * @throws IllegalArgumentException if byteVal is less than zero
96 public static Uint8 valueOf(final byte byteVal) {
97 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
98 return instanceFor(byteVal);
102 * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
103 * {@link #shortValue()}.
105 * @param shortVal short value
106 * @return A Uint8 instance
107 * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
109 public static Uint8 valueOf(final short shortVal) {
110 UintConversions.checkRange(shortVal, MAX_VALUE_SHORT);
111 return instanceFor((byte)shortVal);
115 * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
117 * @param intVal int value
118 * @return A Uint8 instance
119 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
121 public static Uint8 valueOf(final int intVal) {
122 UintConversions.checkRange(intVal, MAX_VALUE_SHORT);
123 return instanceFor((byte)intVal);
127 * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
128 * {@link #longValue()}.
130 * @param longVal long 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 long longVal) {
135 UintConversions.checkRange(longVal, MAX_VALUE_SHORT);
136 return instanceFor((byte)longVal);
140 * Returns an {@code Uint8} corresponding to a given {@code uint}.
142 * @param uint Uint16 value
143 * @return A Uint8 instance
144 * @throws NullPointerException if uint is null
145 * @throws IllegalArgumentException if uint is greater than 255.
147 public static Uint8 valueOf(final Uint16 uint) {
148 return valueOf(uint.intValue());
152 * Returns an {@code Uint8} corresponding to a given {@code uint}.
154 * @param uint Uint32 value
155 * @return A Uint8 instance
156 * @throws NullPointerException if uint is null
157 * @throws IllegalArgumentException if uint is greater than 255.
159 public static Uint8 valueOf(final Uint32 uint) {
160 return valueOf(uint.longValue());
164 * Returns an {@code Uint8} corresponding to a given {@code uint}.
166 * @param uint Uint64 value
167 * @return A Uint8 instance
168 * @throws NullPointerException if uint is null
169 * @throws IllegalArgumentException if uint is greater than 255.
171 public static Uint8 valueOf(final Uint64 uint) {
172 return valueOf(uint.longValue());
176 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
179 * @param string String to parse
180 * @return A Uint8 instance
181 * @throws NullPointerException if string is null
182 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
183 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
185 public static Uint8 valueOf(final String string) {
186 return valueOf(string, 10);
190 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
193 * @param string String to parse
194 * @param radix Radix to use
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, or if the
199 * {@code radix} is outside of allowed range.
201 public static Uint8 valueOf(final String string, final int radix) {
202 return valueOf(Short.parseShort(requireNonNull(string), radix));
209 * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
210 * the returned value will be equal to {@code this - 2^8}.
213 public final byte byteValue() {
218 public final int intValue() {
219 return Byte.toUnsignedInt(value);
223 public final long longValue() {
224 return Byte.toUnsignedLong(value);
228 public final float floatValue() {
233 public final double doubleValue() {
238 @SuppressWarnings("checkstyle:parameterName")
239 public final int compareTo(final Uint8 o) {
240 return Byte.compareUnsigned(value, o.value);
244 public final String toCanonicalString() {
245 return Integer.toString(intValue());
249 public final CanonicalValueSupport<Uint8> support() {
254 * Convert this value to a {@code short}.
258 public final short toJava() {
263 public final int hashCode() {
264 return Byte.hashCode(value);
268 public final boolean equals(final @Nullable Object obj) {
269 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
273 public final String toString() {
274 return toCanonicalString();
277 private Object readResolve() {
278 return instanceFor(value);