BUG-4661: Introduce Decimal64, Empty, Uint{8,16,32,64}
[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 org.opendaylight.yangtools.concepts.Immutable;
14
15 /**
16  * Dedicated type for YANG's 'type uint8' type.
17  *
18  * @author Robert Varga
19  */
20 @Beta
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;
24
25     private static final long serialVersionUID = 1L;
26     private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
27
28     private final byte value;
29
30     private Uint8(final byte value) {
31         this.value = value;
32     }
33
34     private static Uint8 instanceFor(final byte value) {
35         final int slot = Byte.toUnsignedInt(value);
36
37         Uint8 ret = CACHE[slot];
38         if (ret == null) {
39             synchronized (CACHE) {
40                 ret = CACHE[slot];
41                 if (ret == null) {
42                     ret = new Uint8(value);
43                     CACHE[slot] = ret;
44                 }
45             }
46         }
47
48         return ret;
49     }
50
51     public static Uint8 fromByteBits(final byte bits) {
52         return instanceFor(bits);
53     }
54
55     public static Uint8 valueOf(final byte byteVal) {
56         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
57         return instanceFor(byteVal);
58     }
59
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));
63     }
64
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));
68     }
69
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));
73     }
74
75     public static Uint8 valueOf(final Uint16 uint) {
76         return valueOf(uint.intValue());
77     }
78
79     public static Uint8 valueOf(final String string) {
80         return valueOf(string, 10);
81     }
82
83     public static Uint8 valueOf(final String string, final int radix) {
84         // FIXME: implement this
85         throw new UnsupportedOperationException();
86     }
87
88     @Override
89     public byte byteValue() {
90         return value;
91     }
92
93     @Override
94     public int intValue() {
95         return Byte.toUnsignedInt(value);
96     }
97
98     @Override
99     public long longValue() {
100         return Byte.toUnsignedLong(value);
101     }
102
103     @Override
104     public float floatValue() {
105         return intValue();
106     }
107
108     @Override
109     public double doubleValue() {
110         return intValue();
111     }
112
113     @Override
114     @SuppressWarnings("checkstyle:parameterName")
115     public int compareTo(final Uint8 o) {
116         return intValue() - o.intValue();
117     }
118
119     @Override
120     public int hashCode() {
121         return Byte.hashCode(value);
122     }
123
124     @Override
125     public boolean equals(final Object obj) {
126         if (this == obj) {
127             return true;
128         }
129
130         return obj instanceof Uint8 && value == ((Uint8)obj).value;
131     }
132
133     @Override
134     public String toString() {
135         return String.valueOf(intValue());
136     }
137
138     private Object readResolve() {
139         return instanceFor(value);
140     }
141 }