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.UnsignedInteger;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
21 * Dedicated type for YANG's 'type uint32' type.
23 * @author Robert Varga
27 public class Uint32 extends Number implements CanonicalValue<Uint32> {
28 private static final class Support extends AbstractCanonicalValueSupport<Uint32> {
34 public Uint32 fromString(final String str) {
35 return Uint32.valueOf(str);
39 private static final CanonicalValueSupport<Uint32> SUPPORT = new Support();
40 private static final long serialVersionUID = 1L;
41 private static final long MIN_VALUE = 0;
42 private static final long MAX_VALUE = 0xffffffffL;
45 * Cache of first 256 values.
47 private static final Uint32[] CACHE = new Uint32[Uint8.MAX_VALUE];
49 * Commonly encountered values.
51 private static final Uint32[] COMMON = {
52 new Uint32(Short.MAX_VALUE),
56 new Uint32(Integer.MAX_VALUE),
60 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
61 * changed via {@value #LRU_SIZE_PROPERTY} system property.
63 private static final int DEFAULT_LRU_SIZE = 1024;
64 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.LRU.size";
65 private static final int MAX_LRU_SIZE = 0xffffff;
66 private static final int LRU_SIZE;
69 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
70 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
73 private static final LoadingCache<Integer, Uint32> LRU = CacheBuilder.newBuilder().weakValues()
74 .maximumSize(LRU_SIZE).build(new CacheLoader<Integer, Uint32>() {
76 public Uint32 load(final Integer key) {
77 return new Uint32(key);
81 private final int value;
83 Uint32(final int value) {
87 protected Uint32(final Uint32 other) {
88 this.value = other.value;
91 private static Uint32 instanceFor(final int value) {
92 final long longSlot = Integer.toUnsignedLong(value);
93 if (longSlot >= CACHE.length) {
94 for (Uint32 c : COMMON) {
95 if (c.value == value) {
100 return LRU.getUnchecked(value);
103 final int slot = (int)longSlot;
104 Uint32 ret = CACHE[slot];
106 synchronized (CACHE) {
109 ret = new Uint32(value);
118 public static Uint32 fromIntBits(final int bits) {
119 return instanceFor(bits);
122 public static Uint32 fromUnsignedInteger(final UnsignedInteger uint) {
123 return instanceFor(uint.intValue());
126 public static Uint32 valueOf(final byte byteVal) {
127 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
128 return instanceFor(byteVal);
131 public static Uint32 valueOf(final short shortVal) {
132 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
133 return instanceFor(shortVal);
136 public static Uint32 valueOf(final int intVal) {
137 checkArgument(intVal >= MIN_VALUE, "Value %s is outside of allowed range", intVal);
138 return instanceFor(intVal);
141 public static Uint32 valueOf(final long longVal) {
142 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
143 return instanceFor((int)longVal);
146 public static Uint32 valueOf(final Uint8 uint) {
147 return instanceFor(uint.shortValue());
150 public static Uint32 valueOf(final Uint16 uint) {
151 return instanceFor(uint.intValue());
154 public static Uint32 valueOf(final Uint64 uint) {
155 return valueOf(uint.longValue());
158 public static Uint32 valueOf(final String string) {
159 return valueOf(string, 10);
162 public static Uint32 valueOf(final String string, final int radix) {
163 return instanceFor(Integer.parseUnsignedInt(string, radix));
167 public final int intValue() {
172 public final long longValue() {
173 return Integer.toUnsignedLong(value);
177 public final float floatValue() {
182 public final double doubleValue() {
186 public final UnsignedInteger toUnsignedInteger() {
187 return UnsignedInteger.fromIntBits(value);
191 @SuppressWarnings("checkstyle:parameterName")
192 public final int compareTo(final Uint32 o) {
193 return Integer.compareUnsigned(value, o.value);
197 public final String toCanonicalString() {
198 return Integer.toUnsignedString(value);
202 public final CanonicalValueSupport<Uint32> support() {
207 public final int hashCode() {
208 return Integer.hashCode(value);
212 public final boolean equals(final @Nullable Object obj) {
213 return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
217 public final String toString() {
218 return toCanonicalString();
221 private Object readResolve() {
222 return instanceFor(value);