2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static java.util.Objects.requireNonNull;
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;
23 * Dedicated type for YANG's 'type uint64' type.
25 * @author Robert Varga
29 public class Uint64 extends Number implements CanonicalValue<Uint64> {
30 public static final class Support extends AbstractCanonicalValueSupport<Uint64> {
36 public Variant<Uint64, CanonicalValueViolation> fromString(final String str) {
38 return Variant.ofFirst(Uint64.valueOf(str));
39 } catch (IllegalArgumentException e) {
40 return CanonicalValueViolation.variantOf(e);
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);
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;
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.
56 private static final long CACHE_SIZE;
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;
63 private static final @NonNull Uint64[] CACHE;
66 final Uint64[] c = new Uint64[(int) CACHE_SIZE];
67 for (int i = 0; i < c.length; ++i) {
73 private static final Interner<Uint64> INTERNER = Interners.newWeakInterner();
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();
79 private final long value;
81 private Uint64(final long value) {
85 protected Uint64(final Uint64 other) {
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);
95 * Returns an {@code Uint64} corresponding to a given bit representation. The argument is interpreted as an
96 * unsigned 64-bit value.
98 * @param bits unsigned bit representation
99 * @return A Uint64 instance
101 public static Uint64 fromLongBits(final long bits) {
102 return instanceFor(bits);
106 * Returns an {@code Uint64} corresponding to a given {@code byteVal}. The inverse operation is
107 * {@link #byteValue()}.
109 * @param byteVal byte value
110 * @return A Uint64 instance
111 * @throws IllegalArgumentException if byteVal is less than zero
113 public static Uint64 valueOf(final byte byteVal) {
114 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
115 return instanceFor(byteVal);
119 * Returns an {@code Uint64} corresponding to a given {@code shortVal}. The inverse operation is
120 * {@link #shortValue()}.
122 * @param shortVal short value
123 * @return A Uint64 instance
124 * @throws IllegalArgumentException if shortVal is less than zero
126 public static Uint64 valueOf(final short shortVal) {
127 UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
128 return instanceFor(shortVal);
132 * Returns an {@code Uint64} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
134 * @param intVal int value
135 * @return A Uint64 instance
136 * @throws IllegalArgumentException if intVal is less than zero
138 public static Uint64 valueOf(final int intVal) {
139 UintConversions.checkNonNegative(intVal, MAX_VALUE_STR);
140 return instanceFor(intVal);
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)}.
147 * @param longVal long value
148 * @return A Uint8 instance
149 * @throws IllegalArgumentException if longVal is less than zero
151 public static Uint64 valueOf(final long longVal) {
153 return instanceFor(longVal);
155 throw new IllegalArgumentException("Invalid range: " + longVal + ", expected: [[0..18446744073709551615]].");
159 * Returns an {@code Uint64} corresponding to a given {@code uint}.
161 * @param uint Uint8 value
162 * @return A Uint64 instance
163 * @throws NullPointerException if uint is null
165 public static Uint64 valueOf(final Uint8 uint) {
166 return instanceFor(uint.shortValue());
170 * Returns an {@code Uint64} corresponding to a given {@code uint}.
172 * @param uint Uint16 value
173 * @return A Uint64 instance
174 * @throws NullPointerException if uint is null
176 public static Uint64 valueOf(final Uint16 uint) {
177 return instanceFor(uint.intValue());
181 * Returns an {@code Uint64} corresponding to a given {@code uint}.
183 * @param uint Uint32 value
184 * @return A Uint64 instance
185 * @throws NullPointerException if uint is null
187 public static Uint64 valueOf(final Uint32 uint) {
188 return instanceFor(uint.longValue());
192 * Returns an {@code Uint64} corresponding to a given {@code ulong}.
194 * @param ulong UnsignedLong value
195 * @return A Uint64 instance
196 * @throws NullPointerException if ulong is null
198 public static Uint64 valueOf(final UnsignedLong ulong) {
199 return instanceFor(ulong.longValue());
203 * Returns an {@code Uint64} corresponding to a given {@code bigInt}.
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
210 public static Uint64 valueOf(final BigInteger bigInt) {
211 if (bigInt.signum() >= 0 && bigInt.bitLength() <= Long.SIZE) {
212 return instanceFor(bigInt.longValue());
214 throw new IllegalArgumentException("Invalid range: " + bigInt + ", expected: [[0..18446744073709551615]].");
218 * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
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.
227 public static Uint64 valueOf(final String string) {
228 return valueOf(string, 10);
232 * Returns an {@code Uint64} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
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.
243 public static Uint64 valueOf(final String string, final int radix) {
244 return instanceFor(Long.parseUnsignedLong(requireNonNull(string), radix));
248 public final int intValue() {
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}.
260 public final long longValue() {
265 public final float floatValue() {
267 return UnsignedLong.fromLongBits(value).floatValue();
271 public final double doubleValue() {
273 return UnsignedLong.fromLongBits(value).doubleValue();
277 @SuppressWarnings("checkstyle:parameterName")
278 public final int compareTo(final Uint64 o) {
279 return Long.compareUnsigned(value, o.value);
283 public final String toCanonicalString() {
284 return Long.toUnsignedString(value);
288 public final CanonicalValueSupport<Uint64> support() {
293 * Return an interned (shared) instance equivalent to this object. This may return the same object.
295 * @return A shared instance.
297 public final Uint64 intern() {
298 return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
302 * Convert this value to a {@link BigInteger}.
304 * @return A BigInteger instance
306 public final BigInteger toJava() {
307 // FIXME: ditch the Guava transition
308 return toGuava().bigIntegerValue();
312 * Convert this value to an {@link UnsignedLong}.
314 * @return An UnsignedLong instance
316 public final UnsignedLong toGuava() {
317 return UnsignedLong.fromLongBits(value);
321 public final int hashCode() {
322 return Long.hashCode(value);
326 public final boolean equals(final @Nullable Object obj) {
327 return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
331 public final String toString() {
332 return toCanonicalString();
335 private Object readResolve() {
336 return instanceFor(value);