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 com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.kohsuke.MetaInfServices;
18 import org.opendaylight.yangtools.concepts.Variant;
21 * Dedicated type for YANG's 'type uint8' type.
23 * @author Robert Varga
27 public class Uint8 extends Number implements CanonicalValue<Uint8> {
28 @MetaInfServices(value = CanonicalValueSupport.class)
29 public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
35 public Variant<Uint8, CanonicalValueViolation> fromString(final String str) {
37 return Variant.ofFirst(Uint8.valueOf(str));
38 } catch (IllegalArgumentException e) {
39 return CanonicalValueViolation.variantOf(e);
44 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
46 private static final short MIN_VALUE_SHORT = 0;
47 private static final short MAX_VALUE_SHORT = 255;
49 private static final long serialVersionUID = 1L;
51 private static final @NonNull Uint8[] CACHE;
54 final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
55 for (int i = MIN_VALUE_SHORT; i <= MAX_VALUE_SHORT; ++i) {
56 c[i] = new Uint8((byte)i);
61 public static final Uint8 ZERO = valueOf(MIN_VALUE_SHORT);
62 public static final Uint8 ONE = valueOf(1);
63 public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
65 private final byte value;
67 private Uint8(final byte value) {
71 protected Uint8(final Uint8 other) {
75 private static Uint8 instanceFor(final byte value) {
76 return CACHE[Byte.toUnsignedInt(value)];
80 * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
81 * unsigned 8-bit value.
83 * @param bits unsigned bit representation
84 * @return A Uint8 instance
86 public static Uint8 fromByteBits(final byte bits) {
87 return instanceFor(bits);
91 * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
93 * @param byteVal byte value
94 * @return A Uint8 instance
95 * @throws IllegalArgumentException if byteVal is less than zero
97 public static Uint8 valueOf(final byte byteVal) {
98 checkArgument(byteVal >= MIN_VALUE_SHORT, "Negative values are not allowed");
99 return instanceFor(byteVal);
103 * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
104 * {@link #shortValue()}.
106 * @param shortVal short value
107 * @return A Uint8 instance
108 * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
110 public static Uint8 valueOf(final short shortVal) {
111 checkArgument(shortVal >= MIN_VALUE_SHORT && shortVal <= MAX_VALUE_SHORT,
112 "Value %s is outside of allowed range", shortVal);
113 return instanceFor((byte)(shortVal & 0xff));
117 * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
119 * @param intVal int value
120 * @return A Uint8 instance
121 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
123 public static Uint8 valueOf(final int intVal) {
124 checkArgument(intVal >= MIN_VALUE_SHORT && intVal <= MAX_VALUE_SHORT,
125 "Value %s is outside of allowed range", intVal);
126 return instanceFor((byte)(intVal & 0xff));
130 * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
131 * {@link #longValue()}.
133 * @param longVal long value
134 * @return A Uint8 instance
135 * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
137 public static Uint8 valueOf(final long longVal) {
138 checkArgument(longVal >= MIN_VALUE_SHORT && longVal <= MAX_VALUE_SHORT,
139 "Value %s is outside of allowed range", longVal);
140 return instanceFor((byte)(longVal & 0xff));
144 * Returns an {@code Uint8} corresponding to a given {@code uint}.
146 * @param uint Uint16 value
147 * @return A Uint8 instance
148 * @throws NullPointerException if uint is null
149 * @throws IllegalArgumentException if uint is greater than 255.
151 public static Uint8 valueOf(final Uint16 uint) {
152 return valueOf(uint.intValue());
156 * Returns an {@code Uint8} corresponding to a given {@code uint}.
158 * @param uint Uint32 value
159 * @return A Uint8 instance
160 * @throws NullPointerException if uint is null
161 * @throws IllegalArgumentException if uint is greater than 255.
163 public static Uint8 valueOf(final Uint32 uint) {
164 return valueOf(uint.longValue());
168 * Returns an {@code Uint8} corresponding to a given {@code uint}.
170 * @param uint Uint64 value
171 * @return A Uint8 instance
172 * @throws NullPointerException if uint is null
173 * @throws IllegalArgumentException if uint is greater than 255.
175 public static Uint8 valueOf(final Uint64 uint) {
176 return valueOf(uint.longValue());
180 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
183 * @param string String to parse
184 * @return A Uint8 instance
185 * @throws NullPointerException if string is null
186 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
187 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
189 public static Uint8 valueOf(final String string) {
190 return valueOf(string, 10);
194 * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
197 * @param string String to parse
198 * @param radix Radix to use
199 * @return A Uint8 instance
200 * @throws NullPointerException if string is null
201 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
202 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
203 * {@code radix} is outside of allowed range.
205 public static Uint8 valueOf(final String string, final int radix) {
206 return valueOf(Short.parseShort(requireNonNull(string), radix));
213 * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
214 * the returned value will be equal to {@code this - 2^8}.
217 public final byte byteValue() {
222 public final int intValue() {
223 return Byte.toUnsignedInt(value);
227 public final long longValue() {
228 return Byte.toUnsignedLong(value);
232 public final float floatValue() {
237 public final double doubleValue() {
242 @SuppressWarnings("checkstyle:parameterName")
243 public final int compareTo(final Uint8 o) {
244 return Byte.compareUnsigned(value, o.value);
248 public final String toCanonicalString() {
249 return Integer.toString(intValue());
253 public final CanonicalValueSupport<Uint8> support() {
258 * Convert this value to a {@code short}.
262 public final short toJava() {
267 public final int hashCode() {
268 return Byte.hashCode(value);
272 public final boolean equals(final @Nullable Object obj) {
273 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
277 public final String toString() {
278 return toCanonicalString();
281 private Object readResolve() {
282 return instanceFor(value);