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.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
17 * Dedicated type for YANG's 'type uint8' type.
19 * @author Robert Varga
23 public class Uint8 extends Number implements CanonicalValue<Uint8> {
24 private static final class Support extends AbstractCanonicalValueSupport<Uint8> {
30 public Uint8 fromString(final String str) {
31 return Uint8.valueOf(str);
35 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
37 static final short MIN_VALUE = 0;
38 static final short MAX_VALUE = 255;
40 private static final long serialVersionUID = 1L;
41 private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
43 private final byte value;
45 private Uint8(final byte value) {
49 protected Uint8(final Uint8 other) {
50 this.value = other.value;
53 private static Uint8 instanceFor(final byte value) {
54 final int slot = Byte.toUnsignedInt(value);
56 Uint8 ret = CACHE[slot];
58 synchronized (CACHE) {
61 ret = new Uint8(value);
70 public static Uint8 fromByteBits(final byte bits) {
71 return instanceFor(bits);
74 public static Uint8 valueOf(final byte byteVal) {
75 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
76 return instanceFor(byteVal);
79 public static Uint8 valueOf(final short shortVal) {
80 checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
81 return instanceFor((byte)(shortVal & 0xff));
84 public static Uint8 valueOf(final int intVal) {
85 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
86 return instanceFor((byte)(intVal & 0xff));
89 public static Uint8 valueOf(final long longVal) {
90 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
91 return instanceFor((byte)(longVal & 0xff));
94 public static Uint8 valueOf(final Uint16 uint) {
95 return valueOf(uint.intValue());
98 public static Uint8 valueOf(final Uint32 uint) {
99 return valueOf(uint.longValue());
102 public static Uint8 valueOf(final Uint64 uint) {
103 return valueOf(uint.longValue());
106 public static Uint8 valueOf(final String string) {
107 return valueOf(string, 10);
110 public static Uint8 valueOf(final String string, final int radix) {
111 return valueOf(Short.parseShort(string, radix));
115 public final byte byteValue() {
120 public final int intValue() {
121 return Byte.toUnsignedInt(value);
125 public final long longValue() {
126 return Byte.toUnsignedLong(value);
130 public final float floatValue() {
135 public final double doubleValue() {
140 @SuppressWarnings("checkstyle:parameterName")
141 public final int compareTo(final Uint8 o) {
142 return intValue() - o.intValue();
146 public final String toCanonicalString() {
147 return String.valueOf(intValue());
151 public final CanonicalValueSupport<Uint8> support() {
156 public final int hashCode() {
157 return Byte.hashCode(value);
161 public final boolean equals(final @Nullable Object obj) {
162 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
166 public final String toString() {
167 return toCanonicalString();
170 private Object readResolve() {
171 return instanceFor(value);