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.cache.CacheBuilder;
14 import com.google.common.cache.CacheLoader;
15 import com.google.common.cache.LoadingCache;
16 import com.google.common.primitives.UnsignedLong;
17 import java.math.BigInteger;
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 = 0;
52 * Cache of first 256 values.
54 private static final Uint64[] CACHE = new Uint64[Uint8.MAX_VALUE];
56 * Commonly encountered values.
58 private static final Uint64[] COMMON = {
59 new Uint64(Short.MAX_VALUE + 1L),
63 new Uint64(Integer.MAX_VALUE),
64 new Uint64(Integer.MAX_VALUE + 1L),
65 new Uint64(Long.MAX_VALUE),
69 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
70 * changed via {@value #LRU_SIZE_PROPERTY} system property.
72 private static final int DEFAULT_LRU_SIZE = 1024;
73 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.LRU.size";
74 private static final int MAX_LRU_SIZE = 0xffffff;
75 private static final int LRU_SIZE;
78 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
79 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
82 private static final LoadingCache<Long, Uint64> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
83 .build(new CacheLoader<Long, Uint64>() {
85 public Uint64 load(final Long key) {
86 return new Uint64(key);
90 private final long value;
92 Uint64(final long value) {
96 protected Uint64(final Uint64 other) {
97 this.value = other.value;
100 private static Uint64 instanceFor(final long value) {
101 final int slot = (int)value;
102 if (slot < 0 || slot >= CACHE.length) {
103 for (Uint64 c : COMMON) {
104 if (c.value == value) {
109 return LRU.getUnchecked(value);
112 Uint64 ret = CACHE[slot];
114 synchronized (CACHE) {
117 ret = new Uint64(value);
126 public static Uint64 fromLongBits(final long bits) {
127 return instanceFor(bits);
130 public static Uint64 fromUnsignedLong(final UnsignedLong ulong) {
131 return instanceFor(ulong.longValue());
134 public static Uint64 valueOf(final byte byteVal) {
135 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
136 return instanceFor(byteVal);
139 public static Uint64 valueOf(final short shortVal) {
140 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
141 return instanceFor(shortVal);
144 public static Uint64 valueOf(final int intVal) {
145 checkArgument(intVal >= MIN_VALUE, "Value %s is outside of allowed range", intVal);
146 return instanceFor(intVal);
149 public static Uint64 valueOf(final long longVal) {
150 checkArgument(longVal >= MIN_VALUE, "Value %s is outside of allowed range", longVal);
151 return instanceFor(longVal);
154 public static Uint64 valueOf(final Uint8 uint) {
155 return instanceFor(uint.shortValue());
158 public static Uint64 valueOf(final Uint16 uint) {
159 return instanceFor(uint.intValue());
162 public static Uint64 valueOf(final Uint32 uint) {
163 return instanceFor(uint.longValue());
166 public static Uint64 valueOf(final String string) {
167 return valueOf(string, 10);
170 public static Uint64 valueOf(final String string, final int radix) {
171 return instanceFor(Long.parseUnsignedLong(string, radix));
174 public static Uint64 valueOf(final BigInteger bigInt) {
175 checkArgument(bigInt.signum() >= 0, "Negative values not allowed");
176 checkArgument(bigInt.bitLength() <= Long.SIZE, "Value %s is outside of allowed range", bigInt);
178 return instanceFor(bigInt.longValue());
182 public final int intValue() {
187 public final long longValue() {
192 public final float floatValue() {
194 return UnsignedLong.fromLongBits(value).floatValue();
198 public final double doubleValue() {
200 return UnsignedLong.fromLongBits(value).doubleValue();
203 public final UnsignedLong toUnsignedLong() {
204 return UnsignedLong.fromLongBits(value);
208 @SuppressWarnings("checkstyle:parameterName")
209 public final int compareTo(final Uint64 o) {
210 return Long.compareUnsigned(value, o.value);
214 public final String toCanonicalString() {
215 return Long.toUnsignedString(value);
219 public final CanonicalValueSupport<Uint64> support() {
224 public final int hashCode() {
225 return Long.hashCode(value);
229 public final boolean equals(final @Nullable Object obj) {
230 return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
234 public final String toString() {
235 return toCanonicalString();
238 private Object readResolve() {
239 return instanceFor(value);