Add UintConversions
[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 com.google.common.base.Preconditions.checkArgument;
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.kohsuke.MetaInfServices;
21 import org.opendaylight.yangtools.concepts.Variant;
22
23 /**
24  * Dedicated type for YANG's 'type uint64' type.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 @NonNullByDefault
30 public class Uint64 extends Number implements CanonicalValue<Uint64> {
31     @MetaInfServices(value = CanonicalValueSupport.class)
32     public static final class Support extends AbstractCanonicalValueSupport<Uint64> {
33         public Support() {
34             super(Uint64.class);
35         }
36
37         @Override
38         public Variant<Uint64, CanonicalValueViolation> fromString(final String str) {
39             try {
40                 return Variant.ofFirst(Uint64.valueOf(str));
41             } catch (IllegalArgumentException e) {
42                 return CanonicalValueViolation.variantOf(e);
43             }
44         }
45     }
46
47     private static final CanonicalValueSupport<Uint64> SUPPORT = new Support();
48     private static final long serialVersionUID = 1L;
49     private static final long MIN_VALUE_LONG = 0;
50
51     private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.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 long 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 Uint64[] CACHE;
66
67     static {
68         final Uint64[] c = new Uint64[(int) CACHE_SIZE];
69         for (int i = 0; i < c.length; ++i) {
70             c[i] = new Uint64(i);
71         }
72         CACHE = c;
73     }
74
75     private static final Interner<Uint64> INTERNER = Interners.newWeakInterner();
76
77     public static final Uint64 ZERO = valueOf(0).intern();
78     public static final Uint64 ONE = valueOf(1).intern();
79     public static final Uint64 MAX_VALUE = fromLongBits(-1).intern();
80
81     private final long value;
82
83     Uint64(final long value) {
84         this.value = value;
85     }
86
87     protected Uint64(final Uint64 other) {
88         this.value = other.value;
89     }
90
91     private static Uint64 instanceFor(final long value) {
92         final int slot = (int)value;
93         return slot >= 0 && slot < CACHE.length ? CACHE[slot] : new Uint64(value);
94     }
95
96     /**
97      * Returns an {@code Uint64} corresponding to a given bit representation. The argument is interpreted as an
98      * unsigned 64-bit value.
99      *
100      * @param bits unsigned bit representation
101      * @return A Uint64 instance
102      */
103     public static Uint64 fromLongBits(final long bits) {
104         return instanceFor(bits);
105     }
106
107     /**
108      * Returns an {@code Uint64} corresponding to a given {@code byteVal}. The inverse operation is
109      * {@link #byteValue()}.
110      *
111      * @param byteVal byte value
112      * @return A Uint64 instance
113      * @throws IllegalArgumentException if byteVal is less than zero
114      */
115     public static Uint64 valueOf(final byte byteVal) {
116         checkArgument(byteVal >= MIN_VALUE_LONG, "Negative values are not allowed");
117         return instanceFor(byteVal);
118     }
119
120     /**
121      * Returns an {@code Uint64} corresponding to a given {@code shortVal}. The inverse operation is
122      * {@link #shortValue()}.
123      *
124      * @param shortVal short value
125      * @return A Uint64 instance
126      * @throws IllegalArgumentException if shortVal is less than zero
127      */
128     public static Uint64 valueOf(final short shortVal) {
129         checkArgument(shortVal >= MIN_VALUE_LONG, "Negative values are not allowed");
130         return instanceFor(shortVal);
131     }
132
133     /**
134      * Returns an {@code Uint64} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
135      *
136      * @param intVal int value
137      * @return A Uint64 instance
138      * @throws IllegalArgumentException if intVal is less than zero
139      */
140     public static Uint64 valueOf(final int intVal) {
141         checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
142         return instanceFor(intVal);
143     }
144
145     /**
146      * Returns an {@code Uint64} corresponding to a given {@code longVal}. The inverse operation is
147      * {@link #fromLongBits(long)}.
148      *
149      * @param longVal long value
150      * @return A Uint8 instance
151      * @throws IllegalArgumentException if longVal is less than zero
152      */
153     public static Uint64 valueOf(final long longVal) {
154         checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
155         return instanceFor(longVal);
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         checkArgument(bigInt.signum() >= 0, "Negative values not allowed");
212         checkArgument(bigInt.bitLength() <= Long.SIZE, "Value %s is outside of allowed range", bigInt);
213         return instanceFor(bigInt.longValue());
214     }
215
216     /**
217      * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
218      * value.
219      *
220      * @param string String to parse
221      * @return A Uint64 instance
222      * @throws NullPointerException if string is null
223      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 18446744073709551615
224      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
225      */
226     public static Uint64 valueOf(final String string) {
227         return valueOf(string, 10);
228     }
229
230     /**
231      * Returns an {@code Uint64} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
232      * value.
233      *
234      * @param string String to parse
235      * @param radix Radix to use
236      * @return A Uint64 instance
237      * @throws NullPointerException if string is null
238      * @throws IllegalArgumentException if the parsed value is less than zero or greater than 18446744073709551615
239      * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
240      *                               {@code radix} is outside of allowed range.
241      */
242     public static Uint64 valueOf(final String string, final int radix) {
243         return instanceFor(Long.parseUnsignedLong(string, radix));
244     }
245
246     @Override
247     public final int intValue() {
248         return (int)value;
249     }
250
251     /**
252      * {@inheritDoc}
253      *
254      * <p>
255      * The inverse operation is {@link #fromLongBits(long)}. In case this value is greater than {@link Long#MAX_VALUE},
256      * the returned value will be equal to {@code this - 2^64}.
257      */
258     @Override
259     public final long longValue() {
260         return value;
261     }
262
263     @Override
264     public final float floatValue() {
265         // TODO: ditch Guava
266         return UnsignedLong.fromLongBits(value).floatValue();
267     }
268
269     @Override
270     public final double doubleValue() {
271         // TODO: ditch Guava
272         return UnsignedLong.fromLongBits(value).doubleValue();
273     }
274
275     @Override
276     @SuppressWarnings("checkstyle:parameterName")
277     public final int compareTo(final Uint64 o) {
278         return Long.compareUnsigned(value, o.value);
279     }
280
281     @Override
282     public final String toCanonicalString() {
283         return Long.toUnsignedString(value);
284     }
285
286     @Override
287     public final CanonicalValueSupport<Uint64> support() {
288         return SUPPORT;
289     }
290
291     /**
292      * Return an interned (shared) instance equivalent to this object. This may return the same object.
293      *
294      * @return A shared instance.
295      */
296     public final Uint64 intern() {
297         return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
298     }
299
300     /**
301      * Convert this value to a {@link BigInteger}.
302      *
303      * @return A BigInteger instance
304      */
305     public final BigInteger toJava() {
306         // FIXME: ditch the Guava transition
307         return toGuava().bigIntegerValue();
308     }
309
310     /**
311      * Convert this value to an {@link UnsignedLong}.
312      *
313      * @return An UnsignedLong instance
314      */
315     public final UnsignedLong toGuava() {
316         return UnsignedLong.fromLongBits(value);
317     }
318
319     @Override
320     public final int hashCode() {
321         return Long.hashCode(value);
322     }
323
324     @Override
325     public final boolean equals(final @Nullable Object obj) {
326         return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
327     }
328
329     @Override
330     public final String toString() {
331         return toCanonicalString();
332     }
333
334     private Object readResolve() {
335         return instanceFor(value);
336     }
337 }