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 com.google.common.primitives.UnsignedInteger;
15 import java.io.Serial;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.yangtools.concepts.Either;
22 * Dedicated type for YANG's {@code type uint32} type.
25 public class Uint32 extends Number implements CanonicalValue<Uint32> {
26 public static final class Support extends AbstractCanonicalValueSupport<Uint32> {
32 public Either<Uint32, CanonicalValueViolation> fromString(final String str) {
34 return Either.ofFirst(Uint32.valueOf(str));
35 } catch (IllegalArgumentException e) {
36 return CanonicalValueViolation.variantOf(e);
41 private static final CanonicalValueSupport<Uint32> SUPPORT = new Support();
43 private static final long serialVersionUID = 1L;
44 private static final long MAX_VALUE_LONG = 4294967295L;
45 private static final String MAX_VALUE_STR = "4294967295";
47 private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.cache.size";
48 private static final int DEFAULT_CACHE_SIZE = 256;
51 * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
52 * changed via {@value #CACHE_SIZE_PROPERTY} system property.
54 private static final int CACHE_SIZE;
57 final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
58 CACHE_SIZE = p >= 0 ? Math.min(p, Integer.MAX_VALUE) : DEFAULT_CACHE_SIZE;
61 private static final @NonNull Uint32[] CACHE;
64 final Uint32[] c = new Uint32[CACHE_SIZE];
65 for (int i = 0; i < c.length; ++i) {
71 private static final Interner<Uint32> INTERNER = Interners.newWeakInterner();
76 public static final Uint32 ZERO = valueOf(0).intern();
80 public static final Uint32 ONE = valueOf(1).intern();
84 public static final Uint32 TWO = valueOf(2).intern();
86 * Value of {@code 10}.
88 public static final Uint32 TEN = valueOf(10).intern();
90 * Value of {@code 4294967295}.
92 public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG).intern();
94 private final int value;
96 private Uint32(final int value) {
100 protected Uint32(final Uint32 other) {
104 private static Uint32 instanceFor(final int value) {
105 final long longSlot = Integer.toUnsignedLong(value);
106 return longSlot < CACHE.length ? CACHE[(int)longSlot] : new Uint32(value);
110 * Returns an {@code Uint32} corresponding to a given bit representation. The argument is interpreted as an
111 * unsigned 32-bit value.
113 * @param bits unsigned bit representation
114 * @return A Uint32 instance
116 public static Uint32 fromIntBits(final int bits) {
117 return instanceFor(bits);
121 * Returns an {@code Uint32} corresponding to a given {@code byteVal}. The inverse operation is
122 * {@link #byteValue()}.
124 * @param byteVal byte value
125 * @return A Uint32 instance
126 * @throws IllegalArgumentException if byteVal is less than zero
128 public static Uint32 valueOf(final byte byteVal) {
129 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
130 return instanceFor(byteVal);
134 * Returns an {@code Uint32} corresponding to a given {@code shortVal}. The inverse operation is
135 * {@link #shortValue()}.
137 * @param shortVal short value
138 * @return A Uint32 instance
139 * @throws IllegalArgumentException if shortVal is less than zero
141 public static Uint32 valueOf(final short shortVal) {
142 UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
143 return instanceFor(shortVal);
147 * Returns an {@code Uint32} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
149 * @param intVal int value
150 * @return A Uint32 instance
151 * @throws IllegalArgumentException if intVal is less than zero
153 public static Uint32 valueOf(final int intVal) {
154 UintConversions.checkNonNegative(intVal, MAX_VALUE_STR);
155 return instanceFor(intVal);
159 * Returns an {@code Uint32} corresponding to a given {@code longVal}. The inverse operation is
160 * {@link #longValue()}.
162 * @param longVal long value
163 * @return A Uint8 instance
164 * @throws IllegalArgumentException if longVal is less than zero or greater than 4294967295
166 public static Uint32 valueOf(final long longVal) {
167 UintConversions.checkRange(longVal, MAX_VALUE_LONG);
168 return instanceFor((int)longVal);
172 * Returns an {@code Uint32} corresponding to a given {@code uint}.
174 * @param uint Uint8 value
175 * @return A Uint32 instance
176 * @throws NullPointerException if uint is null
178 public static Uint32 valueOf(final Uint8 uint) {
179 return instanceFor(uint.shortValue());
183 * Returns an {@code Uint32} corresponding to a given {@code uint}.
185 * @param uint Uint16 value
186 * @return A Uint32 instance
187 * @throws NullPointerException if uint is null
189 public static Uint32 valueOf(final Uint16 uint) {
190 return instanceFor(uint.intValue());
194 * Returns an {@code Uint32} corresponding to a given {@code uint}.
196 * @param uint Uint64 value
197 * @return A Uint32 instance
198 * @throws NullPointerException if uint is null
199 * @throws IllegalArgumentException if uint is greater than 4294967295
201 public static Uint32 valueOf(final Uint64 uint) {
202 return valueOf(uint.longValue());
206 * Returns an {@code Uint32} corresponding to a given {@code uint}.
208 * @param uint UnsignedInteger value
209 * @return A Uint32 instance
210 * @throws NullPointerException if uint is null
212 public static Uint32 valueOf(final UnsignedInteger uint) {
213 return instanceFor(uint.intValue());
217 * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
220 * @param string String to parse
221 * @return A Uint32 instance
222 * @throws NullPointerException if string is null
223 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
224 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
226 public static Uint32 valueOf(final String string) {
227 return valueOf(string, 10);
231 * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
234 * @param string String to parse
235 * @param radix Radix to use
236 * @return A Uint32 instance
237 * @throws NullPointerException if string is null
238 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
239 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
240 * {@code radix} is outside of allowed range.
242 public static Uint32 valueOf(final String string, final int radix) {
243 return instanceFor(Integer.parseUnsignedInt(requireNonNull(string), radix));
247 * Returns an {@code Uint32} corresponding to a given {@code byteVal} if it is representable. If the value is
248 * negative {@link #ZERO} will be returned.
250 * @param byteVal byte value
251 * @return A Uint32 instance
253 public static Uint32 saturatedOf(final byte byteVal) {
254 return byteVal <= 0 ? Uint32.ZERO : instanceFor(byteVal);
258 * Returns an {@code Uint32} corresponding to a given {@code shortVal} if it is representable. If the value is
259 * negative {@link #ZERO} will be returned.
261 * @param shortVal short value
262 * @return A Uint32 instance
264 public static Uint32 saturatedOf(final short shortVal) {
265 return shortVal <= 0 ? Uint32.ZERO : instanceFor(shortVal);
269 * Returns an {@code Uint32} corresponding to a given {@code intVal} if it is representable. If the value is
270 * negative {@link #ZERO} will be returned.
272 * @param intVal int value
273 * @return A Uint32 instance
275 public static Uint32 saturatedOf(final int intVal) {
276 return intVal <= 0 ? Uint32.ZERO : instanceFor(intVal);
280 * Returns an {@code Uint32} corresponding to a given {@code longVal} if it is representable. If the value is
281 * negative {@link #ZERO} will be returned. If the value is greater than 4294967295, {@link #MAX_VALUE} will be
284 * @param longVal long value
285 * @return A Uint32 instance
287 public static Uint32 saturatedOf(final long longVal) {
291 if (longVal >= MAX_VALUE_LONG) {
292 return Uint32.MAX_VALUE;
294 return instanceFor((int) longVal);
301 * The inverse operation is {@link #fromIntBits(int)}. In case this value is greater than {@link Integer#MAX_VALUE},
302 * the returned value will be equal to {@code this - 2^32}.
305 public final int intValue() {
310 public final long longValue() {
311 return Integer.toUnsignedLong(value);
315 public final float floatValue() {
320 public final double doubleValue() {
325 @SuppressWarnings("checkstyle:parameterName")
326 public final int compareTo(final Uint32 o) {
327 return Integer.compareUnsigned(value, o.value);
331 public final String toCanonicalString() {
332 return Integer.toUnsignedString(value);
336 public final CanonicalValueSupport<Uint32> support() {
341 * Return an interned (shared) instance equivalent to this object. This may return the same object.
343 * @return A shared instance.
345 public final Uint32 intern() {
346 return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
350 * Convert this value to a {@code long}.
354 public final long toJava() {
359 * Convert this value to an {@link UnsignedInteger}.
361 * @return An UnsignedInteger instance
363 public final UnsignedInteger toGuava() {
364 return UnsignedInteger.fromIntBits(value);
368 * Convert this value to a {@code Uint8}.
371 * @throws IllegalArgumentException if this value is greater than 255.
373 public final Uint8 toUint8() {
374 return Uint8.valueOf(toJava());
378 * Convert this value to a {@code Uint16}.
381 * @throws IllegalArgumentException if this value is greater than 65535.
383 public final Uint16 toUint16() {
384 return Uint16.valueOf(toJava());
388 * Convert this value to a {@code Uint64}.
392 public final Uint64 toUint64() {
393 return Uint64.fromLongBits(longValue());
396 public final Uint8 toSaturatedUint8() {
397 return Uint8.saturatedOf(toJava());
400 public final Uint16 toSaturatedUint16() {
401 return Uint16.saturatedOf(toJava());
404 // FIXME: more saturated conversions
407 public final int hashCode() {
408 return Integer.hashCode(value);
412 public final boolean equals(final @Nullable Object obj) {
413 return this == obj || obj instanceof Uint32 other && value == other.value;
417 * A slightly faster version of {@link #equals(Object)}.
419 * @param obj Uint32 object
420 * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
422 public final boolean equals(final @Nullable Uint32 obj) {
423 return this == obj || obj != null && value == obj.value;
427 public final String toString() {
428 return toCanonicalString();
432 private Object readResolve() {
433 return instanceFor(value);