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 java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.concepts.Variant;
21 * Dedicated type for YANG's 'type uint16' type.
23 * @author Robert Varga
27 public class Uint16 extends Number implements CanonicalValue<Uint16> {
28 public static final class Support extends AbstractCanonicalValueSupport<Uint16> {
34 public Variant<Uint16, CanonicalValueViolation> fromString(final String str) {
36 return Variant.ofFirst(Uint16.valueOf(str));
37 } catch (IllegalArgumentException e) {
38 return CanonicalValueViolation.variantOf(e);
43 private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
44 private static final long serialVersionUID = 1L;
45 private static final int MAX_VALUE_INT = 65535;
46 private static final String MAX_VALUE_STR = "65535";
48 private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.cache.size";
49 private static final int DEFAULT_CACHE_SIZE = 256;
52 * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
53 * changed via {@value #CACHE_SIZE_PROPERTY} system property.
55 private static final int CACHE_SIZE;
58 final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
59 CACHE_SIZE = p >= 0 ? Math.min(p, MAX_VALUE_INT + 1) : DEFAULT_CACHE_SIZE;
62 private static final @NonNull Uint16[] CACHE;
65 final Uint16[] c = new Uint16[CACHE_SIZE];
66 for (int i = 0; i < c.length; ++i) {
67 c[i] = new Uint16((short) i);
72 private static final Interner<Uint16> INTERNER = Interners.newWeakInterner();
74 public static final Uint16 ZERO = valueOf(0).intern();
75 public static final Uint16 ONE = valueOf(1).intern();
76 public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
78 private final short value;
80 private Uint16(final short value) {
84 protected Uint16(final Uint16 other) {
88 private static Uint16 instanceFor(final short value) {
89 final int slot = Short.toUnsignedInt(value);
90 return slot < CACHE.length ? CACHE[slot] : new Uint16(value);
94 * Returns an {@code Uint16} corresponding to a given bit representation. The argument is interpreted as an
95 * unsigned 16-bit value.
97 * @param bits unsigned bit representation
98 * @return A Uint16 instance
100 public static Uint16 fromShortBits(final short bits) {
101 return instanceFor(bits);
105 * Returns an {@code Uint16} corresponding to a given {@code byteVal}. The inverse operation is
106 * {@link #byteValue()}.
108 * @param byteVal byte value
109 * @return A Uint16 instance
110 * @throws IllegalArgumentException if byteVal is less than zero
112 public static Uint16 valueOf(final byte byteVal) {
113 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
114 return instanceFor(byteVal);
118 * Returns an {@code Uint16} corresponding to a given {@code shortVal}. The inverse operation is
119 * {@link #shortValue()}.
121 * @param shortVal short value
122 * @return A Uint16 instance
123 * @throws IllegalArgumentException if shortVal is less than zero.
125 public static Uint16 valueOf(final short shortVal) {
126 UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
127 return instanceFor(shortVal);
131 * Returns an {@code Uint16} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
133 * @param intVal int value
134 * @return A Uint16 instance
135 * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
137 public static Uint16 valueOf(final int intVal) {
138 UintConversions.checkRange(intVal, MAX_VALUE_INT);
139 return instanceFor((short)intVal);
143 * Returns an {@code Uint16} corresponding to a given {@code longVal}. The inverse operation is
144 * {@link #longValue()}.
146 * @param longVal long value
147 * @return A Uint16 instance
148 * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
150 public static Uint16 valueOf(final long longVal) {
151 UintConversions.checkRange(longVal, MAX_VALUE_INT);
152 return instanceFor((short)longVal);
156 * Returns an {@code Uint16} corresponding to a given {@code uint}.
158 * @param uint Uint8 value
159 * @return A Uint16 instance
160 * @throws NullPointerException if uint is null
162 public static Uint16 valueOf(final Uint8 uint) {
163 return instanceFor(uint.shortValue());
167 * Returns an {@code Uint8} corresponding to a given {@code uint}.
169 * @param uint Uint32 value
170 * @return A Uint16 instance
171 * @throws NullPointerException if uint is null
172 * @throws IllegalArgumentException if uint is greater than 65535.
174 public static Uint16 valueOf(final Uint32 uint) {
175 return valueOf(uint.longValue());
179 * Returns an {@code Uint8} corresponding to a given {@code uint}.
181 * @param uint Uint64 value
182 * @return A Uint16 instance
183 * @throws NullPointerException if uint is null
184 * @throws IllegalArgumentException if uint is greater than 65535.
186 public static Uint16 valueOf(final Uint64 uint) {
187 return valueOf(uint.longValue());
191 * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
194 * @param string String to parse
195 * @return A Uint16 instance
196 * @throws NullPointerException if string is null
197 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
198 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value.
200 public static Uint16 valueOf(final String string) {
201 return valueOf(string, 10);
205 * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
208 * @param string String to parse
209 * @param radix Radix to use
210 * @return A Uint16 instance
211 * @throws NullPointerException if string is null
212 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
213 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value, or if the
214 * {@code radix} is outside of allowed range.
216 public static Uint16 valueOf(final String string, final int radix) {
217 return valueOf(Integer.parseInt(requireNonNull(string), radix));
224 * The inverse operation is {@link #fromShortBits(short)}. In case this value is greater than
225 * {@link Short#MAX_VALUE}, the returned value will be equal to {@code this - 2^16}.
228 public final short shortValue() {
233 public final int intValue() {
234 return Short.toUnsignedInt(value);
238 public final long longValue() {
239 return Short.toUnsignedLong(value);
243 public final float floatValue() {
248 public final double doubleValue() {
253 @SuppressWarnings("checkstyle:parameterName")
254 public final int compareTo(final Uint16 o) {
255 return Short.compareUnsigned(value, o.value);
259 public final String toCanonicalString() {
260 return Integer.toString(intValue());
264 public final CanonicalValueSupport<Uint16> support() {
269 * Return an interned (shared) instance equivalent to this object. This may return the same object.
271 * @return A shared instance.
273 public final Uint16 intern() {
274 return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
278 * Convert this value to an {@code int}.
282 public final int toJava() {
287 public final int hashCode() {
288 return Short.hashCode(value);
292 public final boolean equals(final @Nullable Object obj) {
293 return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
297 public final String toString() {
298 return toCanonicalString();
301 private Object readResolve() {
302 return instanceFor(value);