Convert base types to implement CanonicalValue
[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.eclipse.jdt.annotation.NonNullByDefault;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Dedicated type for YANG's 'type uint8' type.
18  *
19  * @author Robert Varga
20  */
21 @Beta
22 @NonNullByDefault
23 public class Uint8 extends Number implements CanonicalValue<Uint8> {
24     private static final class Support extends AbstractCanonicalValueSupport<Uint8> {
25         Support() {
26             super(Uint8.class);
27         }
28
29         @Override
30         public Uint8 fromString(final String str) {
31             return Uint8.valueOf(str);
32         }
33     }
34
35     private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
36
37     static final short MIN_VALUE = 0;
38     static final short MAX_VALUE = 255;
39
40     private static final long serialVersionUID = 1L;
41     private static final Uint8[] CACHE = new Uint8[MAX_VALUE + 1];
42
43     private final byte value;
44
45     private Uint8(final byte value) {
46         this.value = value;
47     }
48
49     protected Uint8(final Uint8 other) {
50         this.value = other.value;
51     }
52
53     private static Uint8 instanceFor(final byte value) {
54         final int slot = Byte.toUnsignedInt(value);
55
56         Uint8 ret = CACHE[slot];
57         if (ret == null) {
58             synchronized (CACHE) {
59                 ret = CACHE[slot];
60                 if (ret == null) {
61                     ret = new Uint8(value);
62                     CACHE[slot] = ret;
63                 }
64             }
65         }
66
67         return ret;
68     }
69
70     public static Uint8 fromByteBits(final byte bits) {
71         return instanceFor(bits);
72     }
73
74     public static Uint8 valueOf(final byte byteVal) {
75         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
76         return instanceFor(byteVal);
77     }
78
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));
82     }
83
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));
87     }
88
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));
92     }
93
94     public static Uint8 valueOf(final Uint16 uint) {
95         return valueOf(uint.intValue());
96     }
97
98     public static Uint8 valueOf(final Uint32 uint) {
99         return valueOf(uint.longValue());
100     }
101
102     public static Uint8 valueOf(final Uint64 uint) {
103         return valueOf(uint.longValue());
104     }
105
106     public static Uint8 valueOf(final String string) {
107         return valueOf(string, 10);
108     }
109
110     public static Uint8 valueOf(final String string, final int radix) {
111         return valueOf(Short.parseShort(string, radix));
112     }
113
114     @Override
115     public final byte byteValue() {
116         return value;
117     }
118
119     @Override
120     public final int intValue() {
121         return Byte.toUnsignedInt(value);
122     }
123
124     @Override
125     public final long longValue() {
126         return Byte.toUnsignedLong(value);
127     }
128
129     @Override
130     public final float floatValue() {
131         return intValue();
132     }
133
134     @Override
135     public final double doubleValue() {
136         return intValue();
137     }
138
139     @Override
140     @SuppressWarnings("checkstyle:parameterName")
141     public final int compareTo(final Uint8 o) {
142         return intValue() - o.intValue();
143     }
144
145     @Override
146     public final String toCanonicalString() {
147         return String.valueOf(intValue());
148     }
149
150     @Override
151     public final CanonicalValueSupport<Uint8> support() {
152         return SUPPORT;
153     }
154
155     @Override
156     public final int hashCode() {
157         return Byte.hashCode(value);
158     }
159
160     @Override
161     public final boolean equals(final @Nullable Object obj) {
162         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
163     }
164
165     @Override
166     public final String toString() {
167         return toCanonicalString();
168     }
169
170     private Object readResolve() {
171         return instanceFor(value);
172     }
173 }