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;
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import org.opendaylight.yangtools.concepts.Immutable;
17 * Dedicated type for YANG's 'type uint8' type.
19 * @author Robert Varga
22 public final class Uint8 extends Number implements Comparable<Uint8>, Immutable {
23 static final short MIN_VALUE = 0;
24 static final short MAX_VALUE = 255;
26 private static final long serialVersionUID = 1L;
27 private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
29 private final byte value;
32 Uint8(final byte value) {
36 private static Uint8 instanceFor(final byte value) {
37 final int slot = Byte.toUnsignedInt(value);
39 Uint8 ret = CACHE[slot];
41 synchronized (CACHE) {
44 ret = new Uint8(value);
53 public static Uint8 fromByteBits(final byte bits) {
54 return instanceFor(bits);
57 public static Uint8 valueOf(final byte byteVal) {
58 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
59 return instanceFor(byteVal);
62 public static Uint8 valueOf(final short shortVal) {
63 checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
64 return instanceFor((byte)(shortVal & 0xff));
67 public static Uint8 valueOf(final int intVal) {
68 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
69 return instanceFor((byte)(intVal & 0xff));
72 public static Uint8 valueOf(final long longVal) {
73 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
74 return instanceFor((byte)(longVal & 0xff));
77 public static Uint8 valueOf(final Uint16 uint) {
78 return valueOf(uint.intValue());
81 public static Uint8 valueOf(final Uint32 uint) {
82 return valueOf(uint.longValue());
85 public static Uint8 valueOf(final Uint64 uint) {
86 return valueOf(uint.longValue());
89 public static Uint8 valueOf(final String string) {
90 return valueOf(string, 10);
93 public static Uint8 valueOf(final String string, final int radix) {
94 return valueOf(Short.parseShort(string, radix));
98 public byte byteValue() {
103 public int intValue() {
104 return Byte.toUnsignedInt(value);
108 public long longValue() {
109 return Byte.toUnsignedLong(value);
113 public float floatValue() {
118 public double doubleValue() {
123 @SuppressWarnings("checkstyle:parameterName")
124 public int compareTo(final Uint8 o) {
125 return intValue() - o.intValue();
129 public int hashCode() {
130 return Byte.hashCode(value);
134 public boolean equals(final Object obj) {
139 return obj instanceof Uint8 && value == ((Uint8)obj).value;
143 public String toString() {
144 return String.valueOf(intValue());
147 private Object readResolve() {
148 return instanceFor(value);