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 org.opendaylight.yangtools.concepts.Immutable;
19 * Dedicated type for YANG's 'type uint16' type.
21 * @author Robert Varga
24 public final class Uint16 extends Number implements Comparable<Uint16>, Immutable {
25 private static final long serialVersionUID = 1L;
26 private static final int MIN_VALUE = 0;
27 private static final int MAX_VALUE = 65535;
30 * Cache of first 256 values.
32 private static final Uint16[] CACHE = new Uint16[Uint8.MAX_VALUE];
35 * Commonly encountered values.
37 private static final Uint16[] COMMON = new Uint16[] {
38 new Uint16(Short.MAX_VALUE),
39 new Uint16((short)32768),
40 new Uint16((short)65535),
44 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
45 * changed via {@value #LRU_SIZE_PROPERTY} system property.
47 private static final int DEFAULT_LRU_SIZE = 1024;
48 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
49 private static final int MAX_LRU_SIZE = MAX_VALUE + 1;
50 private static final int LRU_SIZE;
53 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
54 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
57 private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
58 .build(new CacheLoader<Short, Uint16>() {
60 public Uint16 load(final Short key) {
61 return new Uint16(key);
65 private final short value;
67 private Uint16(final short value) {
71 private static Uint16 instanceFor(final short value) {
72 final int slot = Short.toUnsignedInt(value);
73 if (slot >= CACHE.length) {
74 for (Uint16 c : COMMON) {
75 if (c.value == value) {
80 return LRU.getUnchecked(value);
83 Uint16 ret = CACHE[slot];
85 synchronized (CACHE) {
88 ret = new Uint16(value);
97 public static Uint16 fromShortBits(final short bits) {
98 return instanceFor(bits);
101 public static Uint16 valueOf(final byte byteVal) {
102 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
103 return instanceFor(byteVal);
106 public static Uint16 valueOf(final short shortVal) {
107 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
108 return instanceFor(shortVal);
111 public static Uint16 valueOf(final int intVal) {
112 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
113 return instanceFor((short)(intVal & 0xffff));
116 public static Uint16 valueOf(final long longVal) {
117 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
118 return instanceFor((short)(longVal & 0xffff));
121 public static Uint16 valueOf(final Uint8 uint) {
122 return instanceFor(uint.shortValue());
125 public static Uint16 valueOf(final Uint32 uint) {
126 return valueOf(uint.longValue());
129 public static Uint16 valueOf(final String string) {
130 return valueOf(string, 10);
133 public static Uint16 valueOf(final String string, final int radix) {
134 // FIXME: implement this
135 throw new UnsupportedOperationException();
139 public short shortValue() {
144 public int intValue() {
145 return Short.toUnsignedInt(value);
149 public long longValue() {
150 return Short.toUnsignedLong(value);
154 public float floatValue() {
159 public double doubleValue() {
164 @SuppressWarnings("checkstyle:parameterName")
165 public int compareTo(final Uint16 o) {
166 return Integer.compare(intValue(), o.intValue());
170 public int hashCode() {
171 return Short.hashCode(value);
175 public boolean equals(final Object obj) {
180 return obj instanceof Uint16 && value == ((Uint16)obj).value;
184 public String toString() {
185 return String.valueOf(intValue());
188 private Object readResolve() {
189 return instanceFor(value);