Implement Uint{8,16}.valueOf(String, int)
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint8.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import org.opendaylight.yangtools.concepts.Immutable;
15
16 /**
17  * Dedicated type for YANG's 'type uint8' type.
18  *
19  * @author Robert Varga
20  */
21 @Beta
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;
25
26     private static final long serialVersionUID = 1L;
27     private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
28
29     private final byte value;
30
31     @VisibleForTesting
32     Uint8(final byte value) {
33         this.value = value;
34     }
35
36     private static Uint8 instanceFor(final byte value) {
37         final int slot = Byte.toUnsignedInt(value);
38
39         Uint8 ret = CACHE[slot];
40         if (ret == null) {
41             synchronized (CACHE) {
42                 ret = CACHE[slot];
43                 if (ret == null) {
44                     ret = new Uint8(value);
45                     CACHE[slot] = ret;
46                 }
47             }
48         }
49
50         return ret;
51     }
52
53     public static Uint8 fromByteBits(final byte bits) {
54         return instanceFor(bits);
55     }
56
57     public static Uint8 valueOf(final byte byteVal) {
58         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
59         return instanceFor(byteVal);
60     }
61
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));
65     }
66
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));
70     }
71
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));
75     }
76
77     public static Uint8 valueOf(final Uint16 uint) {
78         return valueOf(uint.intValue());
79     }
80
81     public static Uint8 valueOf(final Uint32 uint) {
82         return valueOf(uint.longValue());
83     }
84
85     public static Uint8 valueOf(final Uint64 uint) {
86         return valueOf(uint.longValue());
87     }
88
89     public static Uint8 valueOf(final String string) {
90         return valueOf(string, 10);
91     }
92
93     public static Uint8 valueOf(final String string, final int radix) {
94         return valueOf(Short.parseShort(string, radix));
95     }
96
97     @Override
98     public byte byteValue() {
99         return value;
100     }
101
102     @Override
103     public int intValue() {
104         return Byte.toUnsignedInt(value);
105     }
106
107     @Override
108     public long longValue() {
109         return Byte.toUnsignedLong(value);
110     }
111
112     @Override
113     public float floatValue() {
114         return intValue();
115     }
116
117     @Override
118     public double doubleValue() {
119         return intValue();
120     }
121
122     @Override
123     @SuppressWarnings("checkstyle:parameterName")
124     public int compareTo(final Uint8 o) {
125         return intValue() - o.intValue();
126     }
127
128     @Override
129     public int hashCode() {
130         return Byte.hashCode(value);
131     }
132
133     @Override
134     public boolean equals(final Object obj) {
135         if (this == obj) {
136             return true;
137         }
138
139         return obj instanceof Uint8 && value == ((Uint8)obj).value;
140     }
141
142     @Override
143     public String toString() {
144         return String.valueOf(intValue());
145     }
146
147     private Object readResolve() {
148         return instanceFor(value);
149     }
150 }