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();
77 public static final Uint16 ZERO = valueOf(0).intern();
81 public static final Uint16 ONE = valueOf(1).intern();
85 public static final Uint16 TWO = valueOf(2).intern();
87 * Value of {@code 10}.
89 public static final Uint16 TEN = valueOf(10).intern();
91 * Value of {@code 65535}.
93 public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
95 private final short value;
97 private Uint16(final short value) {
101 protected Uint16(final Uint16 other) {
105 private static Uint16 instanceFor(final short value) {
106 final int slot = Short.toUnsignedInt(value);
107 return slot < CACHE.length ? CACHE[slot] : new Uint16(value);
111 * Returns an {@code Uint16} corresponding to a given bit representation. The argument is interpreted as an
112 * unsigned 16-bit value.
114 * @param bits unsigned bit representation
115 * @return A Uint16 instance
117 public static Uint16 fromShortBits(final short bits) {
118 return instanceFor(bits);
122 * Returns an {@code Uint16} corresponding to a given {@code byteVal}. The inverse operation is
123 * {@link #byteValue()}.
125 * @param byteVal byte value
126 * @return A Uint16 instance
127 * @throws IllegalArgumentException if byteVal is less than zero
129 public static Uint16 valueOf(final byte byteVal) {
130 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
131 return instanceFor(byteVal);
135 * Returns an {@code Uint16} corresponding to a given {@code shortVal}. The inverse operation is
136 * {@link #shortValue()}.
138 * @param shortVal short value
139 * @return A Uint16 instance
140 * @throws IllegalArgumentException if shortVal is less than zero.
142 public static Uint16 valueOf(final short shortVal) {
143 UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
144 return instanceFor(shortVal);
148 * Returns an {@code Uint16} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
150 * @param intVal int value
151 * @return A Uint16 instance
152 * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
154 public static Uint16 valueOf(final int intVal) {
155 UintConversions.checkRange(intVal, MAX_VALUE_INT);
156 return instanceFor((short)intVal);
160 * Returns an {@code Uint16} corresponding to a given {@code longVal}. The inverse operation is
161 * {@link #longValue()}.
163 * @param longVal long value
164 * @return A Uint16 instance
165 * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
167 public static Uint16 valueOf(final long longVal) {
168 UintConversions.checkRange(longVal, MAX_VALUE_INT);
169 return instanceFor((short)longVal);
173 * Returns an {@code Uint16} corresponding to a given {@code uint}.
175 * @param uint Uint8 value
176 * @return A Uint16 instance
177 * @throws NullPointerException if uint is null
179 public static Uint16 valueOf(final Uint8 uint) {
180 return instanceFor(uint.shortValue());
184 * Returns an {@code Uint8} corresponding to a given {@code uint}.
186 * @param uint Uint32 value
187 * @return A Uint16 instance
188 * @throws NullPointerException if uint is null
189 * @throws IllegalArgumentException if uint is greater than 65535.
191 public static Uint16 valueOf(final Uint32 uint) {
192 return valueOf(uint.longValue());
196 * Returns an {@code Uint8} corresponding to a given {@code uint}.
198 * @param uint Uint64 value
199 * @return A Uint16 instance
200 * @throws NullPointerException if uint is null
201 * @throws IllegalArgumentException if uint is greater than 65535.
203 public static Uint16 valueOf(final Uint64 uint) {
204 return valueOf(uint.longValue());
208 * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
211 * @param string String to parse
212 * @return A Uint16 instance
213 * @throws NullPointerException if string is null
214 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
215 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value.
217 public static Uint16 valueOf(final String string) {
218 return valueOf(string, 10);
222 * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
225 * @param string String to parse
226 * @param radix Radix to use
227 * @return A Uint16 instance
228 * @throws NullPointerException if string is null
229 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
230 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value, or if the
231 * {@code radix} is outside of allowed range.
233 public static Uint16 valueOf(final String string, final int radix) {
234 return valueOf(Integer.parseInt(requireNonNull(string), radix));
238 * Returns an {@code Uint16} corresponding to a given {@code byteVal} if it is representable. If the value is
239 * negative {@link #ZERO} will be returned.
241 * @param byteVal byte value
242 * @return A Uint16 instance
244 public static Uint16 saturatedOf(final byte byteVal) {
245 return byteVal < 0 ? Uint16.ZERO : instanceFor(byteVal);
249 * Returns an {@code Uint16} corresponding to a given {@code shortVal} if it is representable. If the value is
250 * negative {@link #ZERO} will be returned.
252 * @param shortVal short value
253 * @return A Uint16 instance
255 public static Uint16 saturatedOf(final short shortVal) {
256 return shortVal < 0 ? Uint16.ZERO : instanceFor(shortVal);
260 * Returns an {@code Uint16} corresponding to a given {@code intVal} if it is representable. If the value is
261 * negative {@link #ZERO} will be returned. If the value is greater than 65535, {@link #MAX_VALUE} will be returned.
263 * @param intVal int value
264 * @return A Uint16 instance
266 public static Uint16 saturatedOf(final int intVal) {
270 if (intVal >= MAX_VALUE_INT) {
271 return Uint16.MAX_VALUE;
273 return instanceFor((short) intVal);
277 * Returns an {@code Uint16} corresponding to a given {@code longVal} if it is representable. If the value is
278 * negative {@link #ZERO} will be returned. If the value is greater than 65535, {@link #MAX_VALUE} will be returned.
280 * @param longVal long value
281 * @return A Uint16 instance
283 public static Uint16 saturatedOf(final long longVal) {
287 if (longVal >= MAX_VALUE_INT) {
288 return Uint16.MAX_VALUE;
290 return instanceFor((short) longVal);
297 * The inverse operation is {@link #fromShortBits(short)}. In case this value is greater than
298 * {@link Short#MAX_VALUE}, the returned value will be equal to {@code this - 2^16}.
301 public final short shortValue() {
306 public final int intValue() {
307 return Short.toUnsignedInt(value);
311 public final long longValue() {
312 return Short.toUnsignedLong(value);
316 public final float floatValue() {
321 public final double doubleValue() {
326 @SuppressWarnings("checkstyle:parameterName")
327 public final int compareTo(final Uint16 o) {
328 return Short.compareUnsigned(value, o.value);
332 public final String toCanonicalString() {
333 return Integer.toString(intValue());
337 public final CanonicalValueSupport<Uint16> support() {
342 * Return an interned (shared) instance equivalent to this object. This may return the same object.
344 * @return A shared instance.
346 public final Uint16 intern() {
347 return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
351 * Convert this value to an {@code int}.
355 public final int toJava() {
360 * Convert this value to a {@code Uint8}.
363 * @throws IllegalArgumentException if this value is greater than 255.
365 public final Uint8 toUint8() {
366 return Uint8.valueOf(toJava());
370 * Convert this value to a {@code Uint32}.
374 public final Uint32 toUint32() {
375 return Uint32.fromIntBits(intValue());
379 * Convert this value to a {@code Uint64}.
383 public final Uint64 toUint64() {
384 return Uint64.fromLongBits(longValue());
387 public final Uint8 toSaturatedUint8() {
388 return Uint8.saturatedOf(toJava());
392 public final int hashCode() {
393 return Short.hashCode(value);
397 public final boolean equals(final @Nullable Object obj) {
398 return this == obj || obj instanceof Uint16 && value == ((Uint16)obj).value;
402 * A slightly faster version of {@link #equals(Object)}.
404 * @param obj Uint16 object
405 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
407 public final boolean equals(final @Nullable Uint16 obj) {
408 return this == obj || obj != null && value == obj.value;
412 public final String toString() {
413 return toCanonicalString();
416 private Object readResolve() {
417 return instanceFor(value);