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.opendaylight.yangtools.concepts.Immutable;
21 * Dedicated type for YANG's 'type uint64' type.
23 * @author Robert Varga
26 public class Uint64 extends Number implements Comparable<Uint64>, Immutable {
27 private static final long serialVersionUID = 1L;
28 private static final long MIN_VALUE = 0;
31 * Cache of first 256 values.
33 private static final Uint64[] CACHE = new Uint64[Uint8.MAX_VALUE];
35 * Commonly encountered values.
37 private static final Uint64[] COMMON = {
38 new Uint64(Short.MAX_VALUE + 1L),
42 new Uint64(Integer.MAX_VALUE),
43 new Uint64(Integer.MAX_VALUE + 1L),
44 new Uint64(Long.MAX_VALUE),
48 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
49 * changed via {@value #LRU_SIZE_PROPERTY} system property.
51 private static final int DEFAULT_LRU_SIZE = 1024;
52 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.LRU.size";
53 private static final int MAX_LRU_SIZE = 0xffffff;
54 private static final int LRU_SIZE;
57 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
58 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
61 private static final LoadingCache<Long, Uint64> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
62 .build(new CacheLoader<Long, Uint64>() {
64 public Uint64 load(final Long key) {
65 return new Uint64(key);
69 private final long value;
71 Uint64(final long value) {
75 protected Uint64(final Uint64 other) {
76 this.value = other.value;
79 private static Uint64 instanceFor(final long value) {
80 final int slot = (int)value;
81 if (slot < 0 || slot >= CACHE.length) {
82 for (Uint64 c : COMMON) {
83 if (c.value == value) {
88 return LRU.getUnchecked(value);
91 Uint64 ret = CACHE[slot];
93 synchronized (CACHE) {
96 ret = new Uint64(value);
105 public static Uint64 fromLongBits(final long bits) {
106 return instanceFor(bits);
109 public static Uint64 fromUnsignedLong(final UnsignedLong ulong) {
110 return instanceFor(ulong.longValue());
113 public static Uint64 valueOf(final byte byteVal) {
114 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
115 return instanceFor(byteVal);
118 public static Uint64 valueOf(final short shortVal) {
119 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
120 return instanceFor(shortVal);
123 public static Uint64 valueOf(final int intVal) {
124 checkArgument(intVal >= MIN_VALUE, "Value %s is outside of allowed range", intVal);
125 return instanceFor(intVal);
128 public static Uint64 valueOf(final long longVal) {
129 checkArgument(longVal >= MIN_VALUE, "Value %s is outside of allowed range", longVal);
130 return instanceFor(longVal);
133 public static Uint64 valueOf(final Uint8 uint) {
134 return instanceFor(uint.shortValue());
137 public static Uint64 valueOf(final Uint16 uint) {
138 return instanceFor(uint.intValue());
141 public static Uint64 valueOf(final Uint32 uint) {
142 return instanceFor(uint.longValue());
145 public static Uint64 valueOf(final String string) {
146 return valueOf(string, 10);
149 public static Uint64 valueOf(final String string, final int radix) {
150 return instanceFor(Long.parseUnsignedLong(string, radix));
153 public static Uint64 valueOf(final BigInteger bigInt) {
154 checkArgument(bigInt.signum() >= 0, "Negative values not allowed");
155 checkArgument(bigInt.bitLength() <= Long.SIZE, "Value %s is outside of allowed range", bigInt);
157 return instanceFor(bigInt.longValue());
161 public final int intValue() {
166 public final long longValue() {
171 public final float floatValue() {
173 return UnsignedLong.fromLongBits(value).floatValue();
177 public final double doubleValue() {
179 return UnsignedLong.fromLongBits(value).doubleValue();
182 public final UnsignedLong toUnsignedLong() {
183 return UnsignedLong.fromLongBits(value);
187 @SuppressWarnings("checkstyle:parameterName")
188 public final int compareTo(final Uint64 o) {
189 return Long.compareUnsigned(value, o.value);
193 public final int hashCode() {
194 return Long.hashCode(value);
198 public final boolean equals(final Object obj) {
199 return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
203 public final String toString() {
204 return Long.toUnsignedString(value);
207 private Object readResolve() {
208 return instanceFor(value);