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 com.google.common.base.Preconditions.checkArgument;
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;
24 * Dedicated type for YANG's 'type uint64' type.
26 * @author Robert Varga
30 public class Uint64 extends Number implements CanonicalValue<Uint64> {
31 @MetaInfServices(value = CanonicalValueSupport.class)
32 public static final class Support extends AbstractCanonicalValueSupport<Uint64> {
38 public Variant<Uint64, CanonicalValueViolation> fromString(final String str) {
40 return Variant.ofFirst(Uint64.valueOf(str));
41 } catch (IllegalArgumentException e) {
42 return CanonicalValueViolation.variantOf(e);
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;
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;
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.
58 private static final long CACHE_SIZE;
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;
65 private static final @NonNull Uint64[] CACHE;
68 final Uint64[] c = new Uint64[(int) CACHE_SIZE];
69 for (int i = 0; i < c.length; ++i) {
75 private static final Interner<Uint64> INTERNER = Interners.newWeakInterner();
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();
81 private final long value;
83 Uint64(final long value) {
87 protected Uint64(final Uint64 other) {
88 this.value = other.value;
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);
97 * Returns an {@code Uint64} corresponding to a given bit representation. The argument is interpreted as an
98 * unsigned 64-bit value.
100 * @param bits unsigned bit representation
101 * @return A Uint64 instance
103 public static Uint64 fromLongBits(final long bits) {
104 return instanceFor(bits);
108 * Returns an {@code Uint64} corresponding to a given {@code byteVal}. The inverse operation is
109 * {@link #byteValue()}.
111 * @param byteVal byte value
112 * @return A Uint64 instance
113 * @throws IllegalArgumentException if byteVal is less than zero
115 public static Uint64 valueOf(final byte byteVal) {
116 checkArgument(byteVal >= MIN_VALUE_LONG, "Negative values are not allowed");
117 return instanceFor(byteVal);
121 * Returns an {@code Uint64} corresponding to a given {@code shortVal}. The inverse operation is
122 * {@link #shortValue()}.
124 * @param shortVal short value
125 * @return A Uint64 instance
126 * @throws IllegalArgumentException if shortVal is less than zero
128 public static Uint64 valueOf(final short shortVal) {
129 checkArgument(shortVal >= MIN_VALUE_LONG, "Negative values are not allowed");
130 return instanceFor(shortVal);
134 * Returns an {@code Uint64} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
136 * @param intVal int value
137 * @return A Uint64 instance
138 * @throws IllegalArgumentException if intVal is less than zero
140 public static Uint64 valueOf(final int intVal) {
141 checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
142 return instanceFor(intVal);
146 * Returns an {@code Uint64} corresponding to a given {@code longVal}. The inverse operation is
147 * {@link #fromLongBits(long)}.
149 * @param longVal long value
150 * @return A Uint8 instance
151 * @throws IllegalArgumentException if longVal is less than zero
153 public static Uint64 valueOf(final long longVal) {
154 checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
155 return instanceFor(longVal);
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 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());
217 * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
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.
226 public static Uint64 valueOf(final String string) {
227 return valueOf(string, 10);
231 * Returns an {@code Uint64} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
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.
242 public static Uint64 valueOf(final String string, final int radix) {
243 return instanceFor(Long.parseUnsignedLong(string, radix));
247 public final int intValue() {
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}.
259 public final long longValue() {
264 public final float floatValue() {
266 return UnsignedLong.fromLongBits(value).floatValue();
270 public final double doubleValue() {
272 return UnsignedLong.fromLongBits(value).doubleValue();
276 @SuppressWarnings("checkstyle:parameterName")
277 public final int compareTo(final Uint64 o) {
278 return Long.compareUnsigned(value, o.value);
282 public final String toCanonicalString() {
283 return Long.toUnsignedString(value);
287 public final CanonicalValueSupport<Uint64> support() {
292 * Return an interned (shared) instance equivalent to this object. This may return the same object.
294 * @return A shared instance.
296 public final Uint64 intern() {
297 return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
301 * Convert this value to a {@link BigInteger}.
303 * @return A BigInteger instance
305 public final BigInteger toJava() {
306 // FIXME: ditch the Guava transition
307 return toGuava().bigIntegerValue();
311 * Convert this value to an {@link UnsignedLong}.
313 * @return An UnsignedLong instance
315 public final UnsignedLong toGuava() {
316 return UnsignedLong.fromLongBits(value);
320 public final int hashCode() {
321 return Long.hashCode(value);
325 public final boolean equals(final @Nullable Object obj) {
326 return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
330 public final String toString() {
331 return toCanonicalString();
334 private Object readResolve() {
335 return instanceFor(value);