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.annotations.VisibleForTesting;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import org.opendaylight.yangtools.concepts.Immutable;
20 * Dedicated type for YANG's 'type uint16' type.
22 * @author Robert Varga
25 public final class Uint16 extends Number implements Comparable<Uint16>, Immutable {
26 private static final long serialVersionUID = 1L;
27 private static final int MIN_VALUE = 0;
28 private static final int MAX_VALUE = 65535;
31 * Cache of first 256 values.
33 private static final Uint16[] CACHE = new Uint16[Uint8.MAX_VALUE];
36 * Commonly encountered values.
38 private static final Uint16[] COMMON = new Uint16[] {
39 new Uint16(Short.MAX_VALUE),
40 new Uint16((short)32768),
41 new Uint16((short)65535),
45 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
46 * changed via {@value #LRU_SIZE_PROPERTY} system property.
48 private static final int DEFAULT_LRU_SIZE = 1024;
49 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
50 private static final int MAX_LRU_SIZE = MAX_VALUE + 1;
51 private static final int LRU_SIZE;
54 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
55 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
58 private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
59 .build(new CacheLoader<Short, Uint16>() {
61 public Uint16 load(final Short key) {
62 return new Uint16(key);
66 private final short value;
69 Uint16(final short value) {
73 private static Uint16 instanceFor(final short value) {
74 final int slot = Short.toUnsignedInt(value);
75 if (slot >= CACHE.length) {
76 for (Uint16 c : COMMON) {
77 if (c.value == value) {
82 return LRU.getUnchecked(value);
85 Uint16 ret = CACHE[slot];
87 synchronized (CACHE) {
90 ret = new Uint16(value);
99 public static Uint16 fromShortBits(final short bits) {
100 return instanceFor(bits);
103 public static Uint16 valueOf(final byte byteVal) {
104 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
105 return instanceFor(byteVal);
108 public static Uint16 valueOf(final short shortVal) {
109 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
110 return instanceFor(shortVal);
113 public static Uint16 valueOf(final int intVal) {
114 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
115 return instanceFor((short)(intVal & 0xffff));
118 public static Uint16 valueOf(final long longVal) {
119 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
120 return instanceFor((short)(longVal & 0xffff));
123 public static Uint16 valueOf(final Uint8 uint) {
124 return instanceFor(uint.shortValue());
127 public static Uint16 valueOf(final Uint32 uint) {
128 return valueOf(uint.longValue());
131 public static Uint16 valueOf(final Uint64 uint) {
132 return valueOf(uint.longValue());
135 public static Uint16 valueOf(final String string) {
136 return valueOf(string, 10);
139 public static Uint16 valueOf(final String string, final int radix) {
140 return valueOf(Integer.parseInt(string, radix));
144 public short shortValue() {
149 public int intValue() {
150 return Short.toUnsignedInt(value);
154 public long longValue() {
155 return Short.toUnsignedLong(value);
159 public float floatValue() {
164 public double doubleValue() {
169 @SuppressWarnings("checkstyle:parameterName")
170 public int compareTo(final Uint16 o) {
171 return Integer.compare(intValue(), o.intValue());
175 public int hashCode() {
176 return Short.hashCode(value);
180 public boolean equals(final Object obj) {
185 return obj instanceof Uint16 && value == ((Uint16)obj).value;
189 public String toString() {
190 return String.valueOf(intValue());
193 private Object readResolve() {
194 return instanceFor(value);