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;
15 import org.kohsuke.MetaInfServices;
16 import org.opendaylight.yangtools.concepts.Variant;
19 * Dedicated type for YANG's 'type uint8' type.
21 * @author Robert Varga
25 public class Uint8 extends Number implements CanonicalValue<Uint8> {
26 @MetaInfServices(value = CanonicalValueSupport.class)
27 public static final class Support extends AbstractCanonicalValueSupport<Uint8> {
33 public Variant<Uint8, CanonicalValueViolation> fromString(final String str) {
35 return Variant.ofFirst(Uint8.valueOf(str));
36 } catch (IllegalArgumentException e) {
37 return CanonicalValueViolation.variantOf(e);
42 private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
44 static final short MIN_VALUE = 0;
45 static final short MAX_VALUE = 255;
47 private static final long serialVersionUID = 1L;
48 private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
50 private final byte value;
52 private Uint8(final byte value) {
56 protected Uint8(final Uint8 other) {
57 this.value = other.value;
60 private static Uint8 instanceFor(final byte value) {
61 final int slot = Byte.toUnsignedInt(value);
63 Uint8 ret = CACHE[slot];
65 synchronized (CACHE) {
68 ret = new Uint8(value);
77 public static Uint8 fromByteBits(final byte bits) {
78 return instanceFor(bits);
81 public static Uint8 valueOf(final byte byteVal) {
82 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
83 return instanceFor(byteVal);
86 public static Uint8 valueOf(final short shortVal) {
87 checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
88 return instanceFor((byte)(shortVal & 0xff));
91 public static Uint8 valueOf(final int intVal) {
92 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
93 return instanceFor((byte)(intVal & 0xff));
96 public static Uint8 valueOf(final long longVal) {
97 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
98 return instanceFor((byte)(longVal & 0xff));
101 public static Uint8 valueOf(final Uint16 uint) {
102 return valueOf(uint.intValue());
105 public static Uint8 valueOf(final Uint32 uint) {
106 return valueOf(uint.longValue());
109 public static Uint8 valueOf(final Uint64 uint) {
110 return valueOf(uint.longValue());
113 public static Uint8 valueOf(final String string) {
114 return valueOf(string, 10);
117 public static Uint8 valueOf(final String string, final int radix) {
118 return valueOf(Short.parseShort(string, radix));
122 public final byte byteValue() {
127 public final int intValue() {
128 return Byte.toUnsignedInt(value);
132 public final long longValue() {
133 return Byte.toUnsignedLong(value);
137 public final float floatValue() {
142 public final double doubleValue() {
147 @SuppressWarnings("checkstyle:parameterName")
148 public final int compareTo(final Uint8 o) {
149 return intValue() - o.intValue();
153 public final String toCanonicalString() {
154 return String.valueOf(intValue());
158 public final CanonicalValueSupport<Uint8> support() {
163 public final int hashCode() {
164 return Byte.hashCode(value);
168 public final boolean equals(final @Nullable Object obj) {
169 return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
173 public final String toString() {
174 return toCanonicalString();
177 private Object readResolve() {
178 return instanceFor(value);