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 org.opendaylight.yangtools.concepts.Immutable;
16 * Dedicated type for YANG's 'type uint8' type.
18 * @author Robert Varga
21 public class Uint8 extends Number implements Comparable<Uint8>, Immutable {
22 static final short MIN_VALUE = 0;
23 static final short MAX_VALUE = 255;
25 private static final long serialVersionUID = 1L;
26 private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
28 private final byte value;
30 private Uint8(final byte value) {
34 protected Uint8(final Uint8 other) {
35 this.value = other.value;
38 private static Uint8 instanceFor(final byte value) {
39 final int slot = Byte.toUnsignedInt(value);
41 Uint8 ret = CACHE[slot];
43 synchronized (CACHE) {
46 ret = new Uint8(value);
55 public static Uint8 fromByteBits(final byte bits) {
56 return instanceFor(bits);
59 public static Uint8 valueOf(final byte byteVal) {
60 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
61 return instanceFor(byteVal);
64 public static Uint8 valueOf(final short shortVal) {
65 checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
66 return instanceFor((byte)(shortVal & 0xff));
69 public static Uint8 valueOf(final int intVal) {
70 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
71 return instanceFor((byte)(intVal & 0xff));
74 public static Uint8 valueOf(final long longVal) {
75 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
76 return instanceFor((byte)(longVal & 0xff));
79 public static Uint8 valueOf(final Uint16 uint) {
80 return valueOf(uint.intValue());
83 public static Uint8 valueOf(final Uint32 uint) {
84 return valueOf(uint.longValue());
87 public static Uint8 valueOf(final Uint64 uint) {
88 return valueOf(uint.longValue());
91 public static Uint8 valueOf(final String string) {
92 return valueOf(string, 10);
95 public static Uint8 valueOf(final String string, final int radix) {
96 return valueOf(Short.parseShort(string, radix));
100 public final byte byteValue() {
105 public final int intValue() {
106 return Byte.toUnsignedInt(value);
110 public final long longValue() {
111 return Byte.toUnsignedLong(value);
115 public final float floatValue() {
120 public final double doubleValue() {
125 @SuppressWarnings("checkstyle:parameterName")
126 public final int compareTo(final Uint8 o) {
127 return intValue() - o.intValue();
131 public final int hashCode() {
132 return Byte.hashCode(value);
136 public final boolean equals(final Object obj) {
137 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
141 public final String toString() {
142 return String.valueOf(intValue());
145 private Object readResolve() {
146 return instanceFor(value);