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