Make yang.common base types non-final
[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 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     protected Uint8(final Uint8 other) {
35         this.value = other.value;
36     }
37
38     private static Uint8 instanceFor(final byte value) {
39         final int slot = Byte.toUnsignedInt(value);
40
41         Uint8 ret = CACHE[slot];
42         if (ret == null) {
43             synchronized (CACHE) {
44                 ret = CACHE[slot];
45                 if (ret == null) {
46                     ret = new Uint8(value);
47                     CACHE[slot] = ret;
48                 }
49             }
50         }
51
52         return ret;
53     }
54
55     public static Uint8 fromByteBits(final byte bits) {
56         return instanceFor(bits);
57     }
58
59     public static Uint8 valueOf(final byte byteVal) {
60         checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
61         return instanceFor(byteVal);
62     }
63
64     public static Uint8 valueOf(final short shortVal) {
65         checkArgument(shortVal >= MIN_VALUE && shortVal <= MAX_VALUE, "Value %s is outside of allowed range", shortVal);
66         return instanceFor((byte)(shortVal & 0xff));
67     }
68
69     public static Uint8 valueOf(final int intVal) {
70         checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
71         return instanceFor((byte)(intVal & 0xff));
72     }
73
74     public static Uint8 valueOf(final long longVal) {
75         checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
76         return instanceFor((byte)(longVal & 0xff));
77     }
78
79     public static Uint8 valueOf(final Uint16 uint) {
80         return valueOf(uint.intValue());
81     }
82
83     public static Uint8 valueOf(final Uint32 uint) {
84         return valueOf(uint.longValue());
85     }
86
87     public static Uint8 valueOf(final Uint64 uint) {
88         return valueOf(uint.longValue());
89     }
90
91     public static Uint8 valueOf(final String string) {
92         return valueOf(string, 10);
93     }
94
95     public static Uint8 valueOf(final String string, final int radix) {
96         return valueOf(Short.parseShort(string, radix));
97     }
98
99     @Override
100     public final byte byteValue() {
101         return value;
102     }
103
104     @Override
105     public final int intValue() {
106         return Byte.toUnsignedInt(value);
107     }
108
109     @Override
110     public final long longValue() {
111         return Byte.toUnsignedLong(value);
112     }
113
114     @Override
115     public final float floatValue() {
116         return intValue();
117     }
118
119     @Override
120     public final double doubleValue() {
121         return intValue();
122     }
123
124     @Override
125     @SuppressWarnings("checkstyle:parameterName")
126     public final int compareTo(final Uint8 o) {
127         return intValue() - o.intValue();
128     }
129
130     @Override
131     public final int hashCode() {
132         return Byte.hashCode(value);
133     }
134
135     @Override
136     public final boolean equals(final Object obj) {
137         return this == obj || obj instanceof Uint8 && value == ((Uint8)obj).value;
138     }
139
140     @Override
141     public final String toString() {
142         return String.valueOf(intValue());
143     }
144
145     private Object readResolve() {
146         return instanceFor(value);
147     }
148 }