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