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.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
20 * Dedicated type for YANG's 'type uint16' type.
22 * @author Robert Varga
26 public class Uint16 extends Number implements CanonicalValue<Uint16> {
27 private static final class Support extends AbstractCanonicalValueSupport<Uint16> {
33 public Uint16 fromString(final String str) {
34 return Uint16.valueOf(str);
38 private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
39 private static final long serialVersionUID = 1L;
40 private static final int MIN_VALUE = 0;
41 private static final int MAX_VALUE = 65535;
44 * Cache of first 256 values.
46 private static final Uint16[] CACHE = new Uint16[Uint8.MAX_VALUE];
49 * Commonly encountered values.
51 private static final Uint16[] COMMON = new Uint16[] {
52 new Uint16(Short.MAX_VALUE),
53 new Uint16((short)32768),
54 new Uint16((short)65535),
58 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
59 * changed via {@value #LRU_SIZE_PROPERTY} system property.
61 private static final int DEFAULT_LRU_SIZE = 1024;
62 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
63 private static final int MAX_LRU_SIZE = MAX_VALUE + 1;
64 private static final int LRU_SIZE;
67 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
68 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
71 private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
72 .build(new CacheLoader<Short, Uint16>() {
74 public Uint16 load(final Short key) {
75 return new Uint16(key);
79 private final short value;
81 Uint16(final short value) {
85 protected Uint16(final Uint16 other) {
86 this.value = other.value;
89 private static Uint16 instanceFor(final short value) {
90 final int slot = Short.toUnsignedInt(value);
91 if (slot >= CACHE.length) {
92 for (Uint16 c : COMMON) {
93 if (c.value == value) {
98 return LRU.getUnchecked(value);
101 Uint16 ret = CACHE[slot];
103 synchronized (CACHE) {
106 ret = new Uint16(value);
115 public static Uint16 fromShortBits(final short bits) {
116 return instanceFor(bits);
119 public static Uint16 valueOf(final byte byteVal) {
120 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
121 return instanceFor(byteVal);
124 public static Uint16 valueOf(final short shortVal) {
125 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
126 return instanceFor(shortVal);
129 public static Uint16 valueOf(final int intVal) {
130 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
131 return instanceFor((short)(intVal & 0xffff));
134 public static Uint16 valueOf(final long longVal) {
135 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
136 return instanceFor((short)(longVal & 0xffff));
139 public static Uint16 valueOf(final Uint8 uint) {
140 return instanceFor(uint.shortValue());
143 public static Uint16 valueOf(final Uint32 uint) {
144 return valueOf(uint.longValue());
147 public static Uint16 valueOf(final Uint64 uint) {
148 return valueOf(uint.longValue());
151 public static Uint16 valueOf(final String string) {
152 return valueOf(string, 10);
155 public static Uint16 valueOf(final String string, final int radix) {
156 return valueOf(Integer.parseInt(string, radix));
160 public final short shortValue() {
165 public final int intValue() {
166 return Short.toUnsignedInt(value);
170 public final long longValue() {
171 return Short.toUnsignedLong(value);
175 public final float floatValue() {
180 public final double doubleValue() {
185 @SuppressWarnings("checkstyle:parameterName")
186 public final int compareTo(final Uint16 o) {
187 return Integer.compare(intValue(), o.intValue());
191 public final String toCanonicalString() {
192 return String.valueOf(intValue());
196 public final CanonicalValueSupport<Uint16> support() {
201 public final int hashCode() {
202 return Short.hashCode(value);
206 public final boolean equals(final @Nullable Object obj) {
207 return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
211 public final String toString() {
212 return toCanonicalString();
215 private Object readResolve() {
216 return instanceFor(value);