4af4172e5f6fc8b2e13bc30426cc8fc2df060314
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / Uint16.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 java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Variant;
19
20 /**
21  * Dedicated type for YANG's 'type uint16' type.
22  *
23  * @author Robert Varga
24  */
25 @Beta
26 @NonNullByDefault
27 public class Uint16 extends Number implements CanonicalValue<Uint16> {
28     public static final class Support extends AbstractCanonicalValueSupport<Uint16> {
29         public Support() {
30             super(Uint16.class);
31         }
32
33         @Override
34         public Variant<Uint16, CanonicalValueViolation> fromString(final String str) {
35             try {
36                 return Variant.ofFirst(Uint16.valueOf(str));
37             } catch (IllegalArgumentException e) {
38                 return CanonicalValueViolation.variantOf(e);
39             }
40         }
41     }
42
43     private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
44     private static final long serialVersionUID = 1L;
45     private static final int MAX_VALUE_INT = 65535;
46     private static final String MAX_VALUE_STR = "65535";
47
48     private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.cache.size";
49     private static final int DEFAULT_CACHE_SIZE = 256;
50
51     /**
52      * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
53      * changed via {@value #CACHE_SIZE_PROPERTY} system property.
54      */
55     private static final int CACHE_SIZE;
56
57     static {
58         final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
59         CACHE_SIZE = p >= 0 ? Math.min(p, MAX_VALUE_INT + 1) : DEFAULT_CACHE_SIZE;
60     }
61
62     private static final @NonNull Uint16[] CACHE;
63
64     static {
65         final Uint16[] c = new Uint16[CACHE_SIZE];
66         for (int i = 0; i < c.length; ++i) {
67             c[i] = new Uint16((short) i);
68         }
69         CACHE = c;
70     }
71
72     private static final Interner<Uint16> INTERNER = Interners.newWeakInterner();
73
74     public static final Uint16 ZERO = valueOf(0).intern();
75     public static final Uint16 ONE = valueOf(1).intern();
76     public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
77
78     private final short value;
79
80     private Uint16(final short value) {
81         this.value = value;
82     }
83
84     protected Uint16(final Uint16 other) {
85         this(other.value);
86     }
87
88     private static Uint16 instanceFor(final short value) {
89         final int slot = Short.toUnsignedInt(value);
90         return slot < CACHE.length ? CACHE[slot] : new Uint16(value);
91     }
92
93     /**
94      * Returns an {@code Uint16} corresponding to a given bit representation. The argument is interpreted as an
95      * unsigned 16-bit value.
96      *
97      * @param bits unsigned bit representation
98      * @return A Uint16 instance
99      */
100     public static Uint16 fromShortBits(final short bits) {
101         return instanceFor(bits);
102     }
103
104     /**
105      * Returns an {@code Uint16} corresponding to a given {@code byteVal}. The inverse operation is
106      * {@link #byteValue()}.
107      *
108      * @param byteVal byte value
109      * @return A Uint16 instance
110      * @throws IllegalArgumentException if byteVal is less than zero
111      */
112     public static Uint16 valueOf(final byte byteVal) {
113         UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
114         return instanceFor(byteVal);
115     }
116
117     /**
118      * Returns an {@code Uint16} corresponding to a given {@code shortVal}. The inverse operation is
119      * {@link #shortValue()}.
120      *
121      * @param shortVal short value
122      * @return A Uint16 instance
123      * @throws IllegalArgumentException if shortVal is less than zero.
124      */
125     public static Uint16 valueOf(final short shortVal) {
126         UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
127         return instanceFor(shortVal);
128     }
129
130     /**
131      * Returns an {@code Uint16} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
132      *
133      * @param intVal int value
134      * @return A Uint16 instance
135      * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
136      */
137     public static Uint16 valueOf(final int intVal) {
138         UintConversions.checkRange(intVal, MAX_VALUE_INT);
139         return instanceFor((short)intVal);
140     }
141
142     /**
143      * Returns an {@code Uint16} corresponding to a given {@code longVal}. The inverse operation is
144      * {@link #longValue()}.
145      *
146      * @param longVal long value
147      * @return A Uint16 instance
148      * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
149      */
150     public static Uint16 valueOf(final long longVal) {
151         UintConversions.checkRange(longVal, MAX_VALUE_INT);
152         return instanceFor((short)longVal);
153     }
154
155     /**
156      * Returns an {@code Uint16} corresponding to a given {@code uint}.
157      *
158      * @param uint Uint8 value
159      * @return A Uint16 instance
160      * @throws NullPointerException if uint is null
161      */
162     public static Uint16 valueOf(final Uint8 uint) {
163         return instanceFor(uint.shortValue());
164     }
165
166     /**
167      * Returns an {@code Uint8} corresponding to a given {@code uint}.
168      *
169      * @param uint Uint32 value
170      * @return A Uint16 instance
171      * @throws NullPointerException if uint is null
172      * @throws IllegalArgumentException if uint is greater than 65535.
173      */
174     public static Uint16 valueOf(final Uint32 uint) {
175         return valueOf(uint.longValue());
176     }
177
178     /**
179      * Returns an {@code Uint8} corresponding to a given {@code uint}.
180      *
181      * @param uint Uint64 value
182      * @return A Uint16 instance
183      * @throws NullPointerException if uint is null
184      * @throws IllegalArgumentException if uint is greater than 65535.
185      */
186     public static Uint16 valueOf(final Uint64 uint) {
187         return valueOf(uint.longValue());
188     }
189
190     /**
191      * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
192      * value.
193      *
194      * @param string String to parse
195      * @return A Uint16 instance
196      * @throws NullPointerException if string is null
197      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
198      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value.
199      */
200     public static Uint16 valueOf(final String string) {
201         return valueOf(string, 10);
202     }
203
204     /**
205      * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
206      * value.
207      *
208      * @param string String to parse
209      * @param radix Radix to use
210      * @return A Uint16 instance
211      * @throws NullPointerException if string is null
212      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
213      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value, or if the
214      *                               {@code radix} is outside of allowed range.
215      */
216     public static Uint16 valueOf(final String string, final int radix) {
217         return valueOf(Integer.parseInt(requireNonNull(string), radix));
218     }
219
220     /**
221      * {@inheritDoc}
222      *
223      * <p>
224      * The inverse operation is {@link #fromShortBits(short)}. In case this value is greater than
225      * {@link Short#MAX_VALUE}, the returned value will be equal to {@code this - 2^16}.
226      */
227     @Override
228     public final short shortValue() {
229         return value;
230     }
231
232     @Override
233     public final int intValue() {
234         return Short.toUnsignedInt(value);
235     }
236
237     @Override
238     public final long longValue() {
239         return Short.toUnsignedLong(value);
240     }
241
242     @Override
243     public final float floatValue() {
244         return intValue();
245     }
246
247     @Override
248     public final double doubleValue() {
249         return intValue();
250     }
251
252     @Override
253     @SuppressWarnings("checkstyle:parameterName")
254     public final int compareTo(final Uint16 o) {
255         return Short.compareUnsigned(value, o.value);
256     }
257
258     @Override
259     public final String toCanonicalString() {
260         return Integer.toString(intValue());
261     }
262
263     @Override
264     public final CanonicalValueSupport<Uint16> support() {
265         return SUPPORT;
266     }
267
268     /**
269      * Return an interned (shared) instance equivalent to this object. This may return the same object.
270      *
271      * @return A shared instance.
272      */
273     public final Uint16 intern() {
274         return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
275     }
276
277     /**
278      * Convert this value to an {@code int}.
279      *
280      * @return An int
281      */
282     public final int toJava() {
283         return intValue();
284     }
285
286     @Override
287     public final int hashCode() {
288         return Short.hashCode(value);
289     }
290
291     @Override
292     public final boolean equals(final @Nullable Object obj) {
293         return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
294     }
295
296     @Override
297     public final String toString() {
298         return toCanonicalString();
299     }
300
301     private Object readResolve() {
302         return instanceFor(value);
303     }
304 }