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;
18 import org.kohsuke.MetaInfServices;
19 import org.opendaylight.yangtools.concepts.Variant;
22 * Dedicated type for YANG's 'type uint16' type.
24 * @author Robert Varga
28 public class Uint16 extends Number implements CanonicalValue<Uint16> {
29 @MetaInfServices(value = CanonicalValueSupport.class)
30 public static final class Support extends AbstractCanonicalValueSupport<Uint16> {
36 public Variant<Uint16, CanonicalValueViolation> fromString(final String str) {
38 return Variant.ofFirst(Uint16.valueOf(str));
39 } catch (IllegalArgumentException e) {
40 return CanonicalValueViolation.variantOf(e);
45 private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
46 private static final long serialVersionUID = 1L;
47 private static final int MIN_VALUE = 0;
48 private static final int MAX_VALUE = 65535;
51 * Cache of first 256 values.
53 private static final Uint16[] CACHE = new Uint16[Uint8.MAX_VALUE];
56 * Commonly encountered values.
58 private static final Uint16[] COMMON = new Uint16[] {
59 new Uint16(Short.MAX_VALUE),
60 new Uint16((short)32768),
61 new Uint16((short)65535),
65 * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
66 * changed via {@value #LRU_SIZE_PROPERTY} system property.
68 private static final int DEFAULT_LRU_SIZE = 1024;
69 private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
70 private static final int MAX_LRU_SIZE = MAX_VALUE + 1;
71 private static final int LRU_SIZE;
74 final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
75 LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
78 private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
79 .build(new CacheLoader<Short, Uint16>() {
81 public Uint16 load(final Short key) {
82 return new Uint16(key);
86 private final short value;
88 Uint16(final short value) {
92 protected Uint16(final Uint16 other) {
93 this.value = other.value;
96 private static Uint16 instanceFor(final short value) {
97 final int slot = Short.toUnsignedInt(value);
98 if (slot >= CACHE.length) {
99 for (Uint16 c : COMMON) {
100 if (c.value == value) {
105 return LRU.getUnchecked(value);
108 Uint16 ret = CACHE[slot];
110 synchronized (CACHE) {
113 ret = new Uint16(value);
122 public static Uint16 fromShortBits(final short bits) {
123 return instanceFor(bits);
126 public static Uint16 valueOf(final byte byteVal) {
127 checkArgument(byteVal >= MIN_VALUE, "Negative values are not allowed");
128 return instanceFor(byteVal);
131 public static Uint16 valueOf(final short shortVal) {
132 checkArgument(shortVal >= MIN_VALUE, "Negative values are not allowed");
133 return instanceFor(shortVal);
136 public static Uint16 valueOf(final int intVal) {
137 checkArgument(intVal >= MIN_VALUE && intVal <= MAX_VALUE, "Value %s is outside of allowed range", intVal);
138 return instanceFor((short)(intVal & 0xffff));
141 public static Uint16 valueOf(final long longVal) {
142 checkArgument(longVal >= MIN_VALUE && longVal <= MAX_VALUE, "Value %s is outside of allowed range", longVal);
143 return instanceFor((short)(longVal & 0xffff));
146 public static Uint16 valueOf(final Uint8 uint) {
147 return instanceFor(uint.shortValue());
150 public static Uint16 valueOf(final Uint32 uint) {
151 return valueOf(uint.longValue());
154 public static Uint16 valueOf(final Uint64 uint) {
155 return valueOf(uint.longValue());
158 public static Uint16 valueOf(final String string) {
159 return valueOf(string, 10);
162 public static Uint16 valueOf(final String string, final int radix) {
163 return valueOf(Integer.parseInt(string, radix));
167 public final short shortValue() {
172 public final int intValue() {
173 return Short.toUnsignedInt(value);
177 public final long longValue() {
178 return Short.toUnsignedLong(value);
182 public final float floatValue() {
187 public final double doubleValue() {
192 @SuppressWarnings("checkstyle:parameterName")
193 public final int compareTo(final Uint16 o) {
194 return Integer.compare(intValue(), o.intValue());
198 public final String toCanonicalString() {
199 return String.valueOf(intValue());
203 public final CanonicalValueSupport<Uint16> support() {
208 public final int hashCode() {
209 return Short.hashCode(value);
213 public final boolean equals(final @Nullable Object obj) {
214 return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
218 public final String toString() {
219 return toCanonicalString();
222 private Object readResolve() {
223 return instanceFor(value);