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.collect.Interner;
13 import com.google.common.collect.Interners;
14 import java.io.Serial;
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.Either;
21 * Dedicated type for YANG's {@code type uint16} type.
24 public class Uint16 extends Number implements CanonicalValue<Uint16> {
25 public static final class Support extends AbstractCanonicalValueSupport<Uint16> {
31 public Either<Uint16, CanonicalValueViolation> fromString(final String str) {
33 return Either.ofFirst(Uint16.valueOf(str));
34 } catch (IllegalArgumentException e) {
35 return CanonicalValueViolation.variantOf(e);
40 private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
42 private static final long serialVersionUID = 1L;
43 private static final int MAX_VALUE_INT = 65535;
44 private static final String MAX_VALUE_STR = "65535";
46 private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.cache.size";
47 private static final int DEFAULT_CACHE_SIZE = 256;
50 * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
51 * changed via {@value #CACHE_SIZE_PROPERTY} system property.
53 private static final int CACHE_SIZE;
56 final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
57 CACHE_SIZE = p >= 0 ? Math.min(p, MAX_VALUE_INT + 1) : DEFAULT_CACHE_SIZE;
60 private static final @NonNull Uint16[] CACHE;
63 final Uint16[] c = new Uint16[CACHE_SIZE];
64 for (int i = 0; i < c.length; ++i) {
65 c[i] = new Uint16((short) i);
70 private static final Interner<Uint16> INTERNER = Interners.newWeakInterner();
75 public static final Uint16 ZERO = valueOf(0).intern();
79 public static final Uint16 ONE = valueOf(1).intern();
83 public static final Uint16 TWO = valueOf(2).intern();
85 * Value of {@code 10}.
87 public static final Uint16 TEN = valueOf(10).intern();
89 * Value of {@code 65535}.
91 public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
93 private final short value;
95 private Uint16(final short value) {
99 protected Uint16(final Uint16 other) {
103 private static Uint16 instanceFor(final short value) {
104 final int slot = Short.toUnsignedInt(value);
105 return slot < CACHE.length ? CACHE[slot] : new Uint16(value);
109 * Returns an {@code Uint16} corresponding to a given bit representation. The argument is interpreted as an
110 * unsigned 16-bit value.
112 * @param bits unsigned bit representation
113 * @return A Uint16 instance
115 public static Uint16 fromShortBits(final short bits) {
116 return instanceFor(bits);
120 * Returns an {@code Uint16} corresponding to a given {@code byteVal}. The inverse operation is
121 * {@link #byteValue()}.
123 * @param byteVal byte value
124 * @return A Uint16 instance
125 * @throws IllegalArgumentException if byteVal is less than zero
127 public static Uint16 valueOf(final byte byteVal) {
128 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
129 return instanceFor(byteVal);
133 * Returns an {@code Uint16} corresponding to a given {@code shortVal}. The inverse operation is
134 * {@link #shortValue()}.
136 * @param shortVal short value
137 * @return A Uint16 instance
138 * @throws IllegalArgumentException if shortVal is less than zero.
140 public static Uint16 valueOf(final short shortVal) {
141 UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
142 return instanceFor(shortVal);
146 * Returns an {@code Uint16} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
148 * @param intVal int value
149 * @return A Uint16 instance
150 * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
152 public static Uint16 valueOf(final int intVal) {
153 UintConversions.checkRange(intVal, MAX_VALUE_INT);
154 return instanceFor((short)intVal);
158 * Returns an {@code Uint16} corresponding to a given {@code longVal}. The inverse operation is
159 * {@link #longValue()}.
161 * @param longVal long value
162 * @return A Uint16 instance
163 * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
165 public static Uint16 valueOf(final long longVal) {
166 UintConversions.checkRange(longVal, MAX_VALUE_INT);
167 return instanceFor((short)longVal);
171 * Returns an {@code Uint16} corresponding to a given {@code uint}.
173 * @param uint Uint8 value
174 * @return A Uint16 instance
175 * @throws NullPointerException if uint is null
177 public static Uint16 valueOf(final Uint8 uint) {
178 return instanceFor(uint.shortValue());
182 * Returns an {@code Uint8} corresponding to a given {@code uint}.
184 * @param uint Uint32 value
185 * @return A Uint16 instance
186 * @throws NullPointerException if uint is null
187 * @throws IllegalArgumentException if uint is greater than 65535.
189 public static Uint16 valueOf(final Uint32 uint) {
190 return valueOf(uint.longValue());
194 * Returns an {@code Uint8} corresponding to a given {@code uint}.
196 * @param uint Uint64 value
197 * @return A Uint16 instance
198 * @throws NullPointerException if uint is null
199 * @throws IllegalArgumentException if uint is greater than 65535.
201 public static Uint16 valueOf(final Uint64 uint) {
202 return valueOf(uint.longValue());
206 * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
209 * @param string String to parse
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.
215 public static Uint16 valueOf(final String string) {
216 return valueOf(string, 10);
220 * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
223 * @param string String to parse
224 * @param radix Radix to use
225 * @return A Uint16 instance
226 * @throws NullPointerException if string is null
227 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
228 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value, or if the
229 * {@code radix} is outside of allowed range.
231 public static Uint16 valueOf(final String string, final int radix) {
232 return valueOf(Integer.parseInt(requireNonNull(string), radix));
236 * Returns an {@code Uint16} corresponding to a given {@code byteVal} if it is representable. If the value is
237 * negative {@link #ZERO} will be returned.
239 * @param byteVal byte value
240 * @return A Uint16 instance
242 public static Uint16 saturatedOf(final byte byteVal) {
243 return byteVal <= 0 ? Uint16.ZERO : instanceFor(byteVal);
247 * Returns an {@code Uint16} corresponding to a given {@code shortVal} if it is representable. If the value is
248 * negative {@link #ZERO} will be returned.
250 * @param shortVal short value
251 * @return A Uint16 instance
253 public static Uint16 saturatedOf(final short shortVal) {
254 return shortVal <= 0 ? Uint16.ZERO : instanceFor(shortVal);
258 * Returns an {@code Uint16} corresponding to a given {@code intVal} if it is representable. If the value is
259 * negative {@link #ZERO} will be returned. If the value is greater than 65535, {@link #MAX_VALUE} will be returned.
261 * @param intVal int value
262 * @return A Uint16 instance
264 public static Uint16 saturatedOf(final int intVal) {
268 if (intVal >= MAX_VALUE_INT) {
269 return Uint16.MAX_VALUE;
271 return instanceFor((short) intVal);
275 * Returns an {@code Uint16} corresponding to a given {@code longVal} if it is representable. If the value is
276 * negative {@link #ZERO} will be returned. If the value is greater than 65535, {@link #MAX_VALUE} will be returned.
278 * @param longVal long value
279 * @return A Uint16 instance
281 public static Uint16 saturatedOf(final long longVal) {
285 if (longVal >= MAX_VALUE_INT) {
286 return Uint16.MAX_VALUE;
288 return instanceFor((short) longVal);
295 * The inverse operation is {@link #fromShortBits(short)}. In case this value is greater than
296 * {@link Short#MAX_VALUE}, the returned value will be equal to {@code this - 2^16}.
299 public final short shortValue() {
304 public final int intValue() {
305 return Short.toUnsignedInt(value);
309 public final long longValue() {
310 return Short.toUnsignedLong(value);
314 public final float floatValue() {
319 public final double doubleValue() {
324 @SuppressWarnings("checkstyle:parameterName")
325 public final int compareTo(final Uint16 o) {
326 return Short.compareUnsigned(value, o.value);
330 public final String toCanonicalString() {
331 return Integer.toString(intValue());
335 public final CanonicalValueSupport<Uint16> support() {
340 * Return an interned (shared) instance equivalent to this object. This may return the same object.
342 * @return A shared instance.
344 public final Uint16 intern() {
345 return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
349 * Convert this value to an {@code int}.
353 public final int toJava() {
358 * Convert this value to a {@code Uint8}.
361 * @throws IllegalArgumentException if this value is greater than 255.
363 public final Uint8 toUint8() {
364 return Uint8.valueOf(toJava());
368 * Convert this value to a {@code Uint32}.
372 public final Uint32 toUint32() {
373 return Uint32.fromIntBits(intValue());
377 * Convert this value to a {@code Uint64}.
381 public final Uint64 toUint64() {
382 return Uint64.fromLongBits(longValue());
385 public final Uint8 toSaturatedUint8() {
386 return Uint8.saturatedOf(toJava());
390 public final int hashCode() {
391 return Short.hashCode(value);
395 public final boolean equals(final @Nullable Object obj) {
396 return this == obj || obj instanceof Uint16 other && value == other.value;
400 * A slightly faster version of {@link #equals(Object)}.
402 * @param obj Uint16 object
403 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
405 public final boolean equals(final @Nullable Uint16 obj) {
406 return this == obj || obj != null && value == obj.value;
410 public final String toString() {
411 return toCanonicalString();
415 private Object readResolve() {
416 return instanceFor(value);