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 com.google.common.primitives.UnsignedInteger;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.kohsuke.MetaInfServices;
20 import org.opendaylight.yangtools.concepts.Variant;
23 * Dedicated type for YANG's 'type uint32' type.
25 * @author Robert Varga
29 public class Uint32 extends Number implements CanonicalValue<Uint32> {
30 @MetaInfServices(value = CanonicalValueSupport.class)
31 public static final class Support extends AbstractCanonicalValueSupport<Uint32> {
37 public Variant<Uint32, CanonicalValueViolation> fromString(final String str) {
39 return Variant.ofFirst(Uint32.valueOf(str));
40 } catch (IllegalArgumentException e) {
41 return CanonicalValueViolation.variantOf(e);
46 private static final CanonicalValueSupport<Uint32> SUPPORT = new Support();
47 private static final long serialVersionUID = 1L;
48 private static final long MAX_VALUE_LONG = 4294967295L;
49 private static final String MAX_VALUE_STR = "4294967295";
51 private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.cache.size";
52 private static final int DEFAULT_CACHE_SIZE = 256;
55 * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
56 * changed via {@value #CACHE_SIZE_PROPERTY} system property.
58 private static final int CACHE_SIZE;
61 final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
62 CACHE_SIZE = p >= 0 ? Math.min(p, Integer.MAX_VALUE) : DEFAULT_CACHE_SIZE;
65 private static final @NonNull Uint32[] CACHE;
68 final Uint32[] c = new Uint32[CACHE_SIZE];
69 for (int i = 0; i < c.length; ++i) {
75 private static final Interner<Uint32> INTERNER = Interners.newWeakInterner();
77 public static final Uint32 ZERO = valueOf(0).intern();
78 public static final Uint32 ONE = valueOf(1).intern();
79 public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG).intern();
81 private final int value;
83 private Uint32(final int value) {
87 protected Uint32(final Uint32 other) {
91 private static Uint32 instanceFor(final int value) {
92 final long longSlot = Integer.toUnsignedLong(value);
93 return longSlot < CACHE.length ? CACHE[(int)longSlot] : new Uint32(value);
97 * Returns an {@code Uint32} corresponding to a given bit representation. The argument is interpreted as an
98 * unsigned 32-bit value.
100 * @param bits unsigned bit representation
101 * @return A Uint32 instance
103 public static Uint32 fromIntBits(final int bits) {
104 return instanceFor(bits);
108 * Returns an {@code Uint32} corresponding to a given {@code byteVal}. The inverse operation is
109 * {@link #byteValue()}.
111 * @param byteVal byte value
112 * @return A Uint32 instance
113 * @throws IllegalArgumentException if byteVal is less than zero
115 public static Uint32 valueOf(final byte byteVal) {
116 UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
117 return instanceFor(byteVal);
121 * Returns an {@code Uint32} corresponding to a given {@code shortVal}. The inverse operation is
122 * {@link #shortValue()}.
124 * @param shortVal short value
125 * @return A Uint32 instance
126 * @throws IllegalArgumentException if shortVal is less than zero
128 public static Uint32 valueOf(final short shortVal) {
129 UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
130 return instanceFor(shortVal);
134 * Returns an {@code Uint32} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
136 * @param intVal int value
137 * @return A Uint32 instance
138 * @throws IllegalArgumentException if intVal is less than zero
140 public static Uint32 valueOf(final int intVal) {
141 UintConversions.checkNonNegative(intVal, MAX_VALUE_STR);
142 return instanceFor(intVal);
146 * Returns an {@code Uint32} corresponding to a given {@code longVal}. The inverse operation is
147 * {@link #longValue()}.
149 * @param longVal long value
150 * @return A Uint8 instance
151 * @throws IllegalArgumentException if longVal is less than zero or greater than 4294967295
153 public static Uint32 valueOf(final long longVal) {
154 UintConversions.checkRange(longVal, MAX_VALUE_LONG, MAX_VALUE_STR);
155 return instanceFor((int)longVal);
159 * Returns an {@code Uint32} corresponding to a given {@code uint}.
161 * @param uint Uint8 value
162 * @return A Uint32 instance
163 * @throws NullPointerException if uint is null
165 public static Uint32 valueOf(final Uint8 uint) {
166 return instanceFor(uint.shortValue());
170 * Returns an {@code Uint32} corresponding to a given {@code uint}.
172 * @param uint Uint16 value
173 * @return A Uint32 instance
174 * @throws NullPointerException if uint is null
176 public static Uint32 valueOf(final Uint16 uint) {
177 return instanceFor(uint.intValue());
181 * Returns an {@code Uint32} corresponding to a given {@code uint}.
183 * @param uint Uint64 value
184 * @return A Uint32 instance
185 * @throws NullPointerException if uint is null
186 * @throws IllegalArgumentException if uint is greater than 4294967295
188 public static Uint32 valueOf(final Uint64 uint) {
189 return valueOf(uint.longValue());
193 * Returns an {@code Uint32} corresponding to a given {@code uint}.
195 * @param uint UnsignedInteger value
196 * @return A Uint32 instance
197 * @throws NullPointerException if uint is null
199 public static Uint32 valueOf(final UnsignedInteger uint) {
200 return instanceFor(uint.intValue());
204 * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
207 * @param string String to parse
208 * @return A Uint32 instance
209 * @throws NullPointerException if string is null
210 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
211 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
213 public static Uint32 valueOf(final String string) {
214 return valueOf(string, 10);
218 * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
221 * @param string String to parse
222 * @param radix Radix to use
223 * @return A Uint32 instance
224 * @throws NullPointerException if string is null
225 * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
226 * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
227 * {@code radix} is outside of allowed range.
229 public static Uint32 valueOf(final String string, final int radix) {
230 return instanceFor(Integer.parseUnsignedInt(requireNonNull(string), radix));
237 * The inverse operation is {@link #fromIntBits(int)}. In case this value is greater than {@link Integer#MAX_VALUE},
238 * the returned value will be equal to {@code this - 2^32}.
241 public final int intValue() {
246 public final long longValue() {
247 return Integer.toUnsignedLong(value);
251 public final float floatValue() {
256 public final double doubleValue() {
261 @SuppressWarnings("checkstyle:parameterName")
262 public final int compareTo(final Uint32 o) {
263 return Integer.compareUnsigned(value, o.value);
267 public final String toCanonicalString() {
268 return Integer.toUnsignedString(value);
272 public final CanonicalValueSupport<Uint32> support() {
277 * Return an interned (shared) instance equivalent to this object. This may return the same object.
279 * @return A shared instance.
281 public final Uint32 intern() {
282 return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
286 * Convert this value to a {@code long}.
290 public final long toJava() {
295 * Convert this value to an {@link UnsignedInteger}.
297 * @return An UnsignedInteger instance
299 public final UnsignedInteger toGuava() {
300 return UnsignedInteger.fromIntBits(value);
304 public final int hashCode() {
305 return Integer.hashCode(value);
309 public final boolean equals(final @Nullable Object obj) {
310 return this == obj || obj instanceof Uint32 && value == ((Uint32)obj).value;
314 public final String toString() {
315 return toCanonicalString();
318 private Object readResolve() {
319 return instanceFor(value);