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 return slot >= 0 && slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
105 private static Uint64 fromCommon(final long value) {
106 for (Uint64 c : COMMON) {
107 if (c.value == value) {
111 return LRU.getUnchecked(value);
114 private static Uint64 fromCache(final int slot, final long value) {
115 // FIXME: 4.0.0: use VarHandles here
116 Uint64 ret = CACHE[slot];
118 synchronized (CACHE) {
121 ret = new Uint64(value);
129 public static Uint64 fromLongBits(final long bits) {
130 return instanceFor(bits);
133 public static Uint64 fromUnsignedLong(final UnsignedLong ulong) {
134 return instanceFor(ulong.longValue());
137 public static Uint64 valueOf(final byte byteVal) {
138 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
139 return instanceFor(byteVal);
142 public static Uint64 valueOf(final short shortVal) {
143 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
144 return instanceFor(shortVal);
147 public static Uint64 valueOf(final int intVal) {
148 checkArgument(intVal >= MIN_VALUE, "Value %s is outside of allowed range", intVal);
149 return instanceFor(intVal);
152 public static Uint64 valueOf(final long longVal) {
153 checkArgument(longVal >= MIN_VALUE, "Value %s is outside of allowed range", longVal);
154 return instanceFor(longVal);
157 public static Uint64 valueOf(final Uint8 uint) {
158 return instanceFor(uint.shortValue());
161 public static Uint64 valueOf(final Uint16 uint) {
162 return instanceFor(uint.intValue());
165 public static Uint64 valueOf(final Uint32 uint) {
166 return instanceFor(uint.longValue());
169 public static Uint64 valueOf(final String string) {
170 return valueOf(string, 10);
173 public static Uint64 valueOf(final String string, final int radix) {
174 return instanceFor(Long.parseUnsignedLong(string, radix));
177 public static Uint64 valueOf(final BigInteger bigInt) {
178 checkArgument(bigInt.signum() >= 0, "Negative values not allowed");
179 checkArgument(bigInt.bitLength() <= Long.SIZE, "Value %s is outside of allowed range", bigInt);
181 return instanceFor(bigInt.longValue());
185 public final int intValue() {
190 public final long longValue() {
195 public final float floatValue() {
197 return UnsignedLong.fromLongBits(value).floatValue();
201 public final double doubleValue() {
203 return UnsignedLong.fromLongBits(value).doubleValue();
206 public final UnsignedLong toUnsignedLong() {
207 return UnsignedLong.fromLongBits(value);
211 @SuppressWarnings("checkstyle:parameterName")
212 public final int compareTo(final Uint64 o) {
213 return Long.compareUnsigned(value, o.value);
217 public final String toCanonicalString() {
218 return Long.toUnsignedString(value);
222 public final CanonicalValueSupport<Uint64> support() {
227 public final int hashCode() {
228 return Long.hashCode(value);
232 public final boolean equals(final @Nullable Object obj) {
233 return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
237 public final String toString() {
238 return toCanonicalString();
241 private Object readResolve() {
242 return instanceFor(value);