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 final 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 private static Uint8 instanceFor(final byte value) {
35 final int slot = Byte.toUnsignedInt(value);
37 Uint8 ret = CACHE[slot];
39 synchronized (CACHE) {
42 ret = new Uint8(value);
51 public static Uint8 fromByteBits(final byte bits) {
52 return instanceFor(bits);
55 public static Uint8 valueOf(final byte byteVal) {
56 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
57 return instanceFor(byteVal);
60 public static Uint8 valueOf(final short shortVal) {
61 checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
62 return instanceFor((byte)(shortVal & 0xff));
65 public static Uint8 valueOf(final int intVal) {
66 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
67 return instanceFor((byte)(intVal & 0xff));
70 public static Uint8 valueOf(final long longVal) {
71 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
72 return instanceFor((byte)(longVal & 0xff));
75 public static Uint8 valueOf(final Uint16 uint) {
76 return valueOf(uint.intValue());
79 public static Uint8 valueOf(final String string) {
80 return valueOf(string, 10);
83 public static Uint8 valueOf(final String string, final int radix) {
84 // FIXME: implement this
85 throw new UnsupportedOperationException();
89 public byte byteValue() {
94 public int intValue() {
95 return Byte.toUnsignedInt(value);
99 public long longValue() {
100 return Byte.toUnsignedLong(value);
104 public float floatValue() {
109 public double doubleValue() {
114 @SuppressWarnings("checkstyle:parameterName")
115 public int compareTo(final Uint8 o) {
116 return intValue() - o.intValue();
120 public int hashCode() {
121 return Byte.hashCode(value);
125 public boolean equals(final Object obj) {
130 return obj instanceof Uint8 && value == ((Uint8)obj).value;
134 public String toString() {
135 return String.valueOf(intValue());
138 private Object readResolve() {
139 return instanceFor(value);