package org.opendaylight.yangtools.yang.common;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.kohsuke.MetaInfServices;
private static final int MIN_VALUE_INT = 0;
private static final int MAX_VALUE_INT = 65535;
- /**
- * Cache of first 256 values.
- */
- private static final Uint16[] CACHE = new Uint16[256];
+ private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.cache.size";
+ private static final int DEFAULT_CACHE_SIZE = 256;
/**
- * Commonly encountered values.
+ * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
+ * changed via {@value #CACHE_SIZE_PROPERTY} system property.
*/
- private static final Uint16[] COMMON = new Uint16[] {
- new Uint16(Short.MAX_VALUE),
- new Uint16((short)32768),
- new Uint16((short)65535),
- };
+ private static final int CACHE_SIZE;
- public static final Uint16 MIN_VALUE = valueOf(MIN_VALUE_INT);
- public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT);
+ static {
+ final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
+ CACHE_SIZE = p >= 0 ? Math.min(p, MAX_VALUE_INT + 1) : DEFAULT_CACHE_SIZE;
+ }
- /**
- * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
- * changed via {@value #LRU_SIZE_PROPERTY} system property.
- */
- private static final int DEFAULT_LRU_SIZE = 1024;
- private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.LRU.size";
- private static final int MAX_LRU_SIZE = MAX_VALUE_INT + 1;
- private static final int LRU_SIZE;
+ private static final @NonNull Uint16[] CACHE;
static {
- final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
- LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
+ final Uint16[] c = new Uint16[CACHE_SIZE];
+ for (int i = MIN_VALUE_INT; i < c.length; ++i) {
+ c[i] = new Uint16((short) i);
+ }
+ CACHE = c;
}
- private static final LoadingCache<Short, Uint16> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
- .build(new CacheLoader<Short, Uint16>() {
- @Override
- public Uint16 load(final Short key) {
- return new Uint16(key);
- }
- });
+ private static final Interner<Uint16> INTERNER = Interners.newWeakInterner();
+
+ public static final Uint16 ZERO = valueOf(MIN_VALUE_INT).intern();
+ public static final Uint16 ONE = valueOf(1).intern();
+ public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
private final short value;
private static Uint16 instanceFor(final short value) {
final int slot = Short.toUnsignedInt(value);
- return slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
- }
-
- private static Uint16 fromCommon(final short value) {
- for (Uint16 c : COMMON) {
- if (c.value == value) {
- return c;
- }
- }
- return LRU.getUnchecked(value);
- }
-
- private static Uint16 fromCache(final int slot, final short value) {
- // FIXME: 4.0.0: use VarHandles here
- Uint16 ret = CACHE[slot];
- if (ret == null) {
- synchronized (CACHE) {
- ret = CACHE[slot];
- if (ret == null) {
- ret = new Uint16(value);
- CACHE[slot] = ret;
- }
- }
- }
- return ret;
+ return slot < CACHE.length ? CACHE[slot] : new Uint16(value);
}
+ /**
+ * Returns an {@code Uint16} corresponding to a given bit representation. The argument is interpreted as an
+ * unsigned 16-bit value.
+ *
+ * @param bits unsigned bit representation
+ * @return A Uint16 instance
+ */
public static Uint16 fromShortBits(final short bits) {
return instanceFor(bits);
}
+ /**
+ * Returns an {@code Uint16} corresponding to a given {@code byteVal}. The inverse operation is
+ * {@link #byteValue()}.
+ *
+ * @param byteVal byte value
+ * @return A Uint16 instance
+ * @throws IllegalArgumentException if byteVal is less than zero
+ */
public static Uint16 valueOf(final byte byteVal) {
checkArgument(byteVal >= MIN_VALUE_INT, "Negative values are not allowed");
return instanceFor(byteVal);
}
+ /**
+ * Returns an {@code Uint16} corresponding to a given {@code shortVal}. The inverse operation is
+ * {@link #shortValue()}.
+ *
+ * @param shortVal short value
+ * @return A Uint16 instance
+ * @throws IllegalArgumentException if shortVal is less than zero.
+ */
public static Uint16 valueOf(final short shortVal) {
checkArgument(shortVal >= MIN_VALUE_INT, "Negative values are not allowed");
return instanceFor(shortVal);
}
+ /**
+ * Returns an {@code Uint16} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+ *
+ * @param intVal int value
+ * @return A Uint16 instance
+ * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
+ */
public static Uint16 valueOf(final int intVal) {
checkArgument(intVal >= MIN_VALUE_INT && intVal <= MAX_VALUE_INT, "Value %s is outside of allowed range",
intVal);
return instanceFor((short)(intVal & 0xffff));
}
+ /**
+ * Returns an {@code Uint16} corresponding to a given {@code longVal}. The inverse operation is
+ * {@link #longValue()}.
+ *
+ * @param longVal long value
+ * @return A Uint16 instance
+ * @throws IllegalArgumentException if intVal is less than zero or greater than 65535.
+ */
public static Uint16 valueOf(final long longVal) {
checkArgument(longVal >= MIN_VALUE_INT && longVal <= MAX_VALUE_INT, "Value %s is outside of allowed range",
longVal);
return instanceFor((short)(longVal & 0xffff));
}
+ /**
+ * Returns an {@code Uint16} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint8 value
+ * @return A Uint16 instance
+ * @throws NullPointerException if uint is null
+ */
public static Uint16 valueOf(final Uint8 uint) {
return instanceFor(uint.shortValue());
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint32 value
+ * @return A Uint16 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if uint is greater than 65535.
+ */
public static Uint16 valueOf(final Uint32 uint) {
return valueOf(uint.longValue());
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint64 value
+ * @return A Uint16 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if uint is greater than 65535.
+ */
public static Uint16 valueOf(final Uint64 uint) {
return valueOf(uint.longValue());
}
+ /**
+ * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
+ * value.
+ *
+ * @param string String to parse
+ * @return A Uint16 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value.
+ */
public static Uint16 valueOf(final String string) {
return valueOf(string, 10);
}
+ /**
+ * Returns an {@code Uint16} holding the value of the specified {@code String}, parsed as an unsigned {@code int}
+ * value.
+ *
+ * @param string String to parse
+ * @param radix Radix to use
+ * @return A Uint16 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 65535
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} value, or if the
+ * {@code radix} is outside of allowed range.
+ */
public static Uint16 valueOf(final String string, final int radix) {
- return valueOf(Integer.parseInt(string, radix));
+ return valueOf(Integer.parseInt(requireNonNull(string), radix));
}
+ /**
+ * {@inheritDoc}
+ *
+ * <p>
+ * The inverse operation is {@link #fromShortBits(short)}. In case this value is greater than
+ * {@link Short#MAX_VALUE}, the returned value will be equal to {@code this - 2^16}.
+ */
@Override
public final short shortValue() {
return value;
return SUPPORT;
}
+ /**
+ * Return an interned (shared) instance equivalent to this object. This may return the same object.
+ *
+ * @return A shared instance.
+ */
+ public final Uint16 intern() {
+ return intValue() < CACHE_SIZE ? this : INTERNER.intern(this);
+ }
+
@Override
public final int hashCode() {
return Short.hashCode(value);
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.annotations.Beta;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
import com.google.common.primitives.UnsignedInteger;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.kohsuke.MetaInfServices;
private static final long MIN_VALUE_LONG = 0;
private static final long MAX_VALUE_LONG = 0xffffffffL;
- /**
- * Cache of first 256 values.
- */
- private static final Uint32[] CACHE = new Uint32[256];
- /**
- * Commonly encountered values.
- */
- private static final Uint32[] COMMON = {
- new Uint32(Short.MAX_VALUE),
- new Uint32(32768),
- new Uint32(65535),
- new Uint32(65536),
- new Uint32(Integer.MAX_VALUE),
- new Uint32(-1)
- };
-
- public static final Uint32 MIN_VALUE = valueOf(MIN_VALUE_LONG);
- public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG);
+ private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.cache.size";
+ private static final int DEFAULT_CACHE_SIZE = 256;
/**
- * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
- * changed via {@value #LRU_SIZE_PROPERTY} system property.
+ * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
+ * changed via {@value #CACHE_SIZE_PROPERTY} system property.
*/
- private static final int DEFAULT_LRU_SIZE = 1024;
- private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.LRU.size";
- private static final int MAX_LRU_SIZE = 0xffffff;
- private static final int LRU_SIZE;
+ private static final int CACHE_SIZE;
static {
- final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
- LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
+ final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
+ CACHE_SIZE = p >= 0 ? Math.min(p, Integer.MAX_VALUE) : DEFAULT_CACHE_SIZE;
}
- private static final LoadingCache<Integer, Uint32> LRU = CacheBuilder.newBuilder().weakValues()
- .maximumSize(LRU_SIZE).build(new CacheLoader<Integer, Uint32>() {
- @Override
- public Uint32 load(final Integer key) {
- return new Uint32(key);
- }
- });
+ private static final @NonNull Uint32[] CACHE;
+
+ static {
+ final Uint32[] c = new Uint32[CACHE_SIZE];
+ for (int i = 0; i < c.length; ++i) {
+ c[i] = new Uint32(i);
+ }
+ CACHE = c;
+ }
+
+ private static final Interner<Uint32> INTERNER = Interners.newWeakInterner();
+
+ public static final Uint32 ZERO = valueOf(0).intern();
+ public static final Uint32 ONE = valueOf(1).intern();
+ public static final Uint32 MAX_VALUE = valueOf(MAX_VALUE_LONG).intern();
private final int value;
private static Uint32 instanceFor(final int value) {
final long longSlot = Integer.toUnsignedLong(value);
- return longSlot < CACHE.length ? fromCache((int)longSlot, value) : fromCommon(value);
- }
-
- private static Uint32 fromCommon(final int value) {
- for (Uint32 c : COMMON) {
- if (c.value == value) {
- return c;
- }
- }
- return LRU.getUnchecked(value);
- }
-
- private static Uint32 fromCache(final int slot, final int value) {
- // FIXME: 4.0.0: use VarHandles here
- Uint32 ret = CACHE[slot];
- if (ret == null) {
- synchronized (CACHE) {
- ret = CACHE[slot];
- if (ret == null) {
- ret = new Uint32(value);
- CACHE[slot] = ret;
- }
- }
- }
- return ret;
+ return longSlot < CACHE.length ? CACHE[(int)longSlot] : new Uint32(value);
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given bit representation. The argument is interpreted as an
+ * unsigned 32-bit value.
+ *
+ * @param bits unsigned bit representation
+ * @return A Uint32 instance
+ */
public static Uint32 fromIntBits(final int bits) {
return instanceFor(bits);
}
- public static Uint32 fromUnsignedInteger(final UnsignedInteger uint) {
- return instanceFor(uint.intValue());
- }
-
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code byteVal}. The inverse operation is
+ * {@link #byteValue()}.
+ *
+ * @param byteVal byte value
+ * @return A Uint32 instance
+ * @throws IllegalArgumentException if byteVal is less than zero
+ */
public static Uint32 valueOf(final byte byteVal) {
checkArgument(byteVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(byteVal);
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code shortVal}. The inverse operation is
+ * {@link #shortValue()}.
+ *
+ * @param shortVal short value
+ * @return A Uint32 instance
+ * @throws IllegalArgumentException if shortVal is less than zero
+ */
public static Uint32 valueOf(final short shortVal) {
checkArgument(shortVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(shortVal);
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+ *
+ * @param intVal int value
+ * @return A Uint32 instance
+ * @throws IllegalArgumentException if intVal is less than zero
+ */
public static Uint32 valueOf(final int intVal) {
- checkArgument(intVal >= MIN_VALUE_LONG, "Value %s is outside of allowed range", intVal);
+ checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(intVal);
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code longVal}. The inverse operation is
+ * {@link #longValue()}.
+ *
+ * @param longVal long value
+ * @return A Uint8 instance
+ * @throws IllegalArgumentException if intVal is less than zero or greater than 4294967295
+ */
public static Uint32 valueOf(final long longVal) {
checkArgument(longVal >= MIN_VALUE_LONG && longVal <= MAX_VALUE_LONG, "Value %s is outside of allowed range",
longVal);
return instanceFor((int)longVal);
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint8 value
+ * @return A Uint32 instance
+ * @throws NullPointerException if uint is null
+ */
public static Uint32 valueOf(final Uint8 uint) {
return instanceFor(uint.shortValue());
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint16 value
+ * @return A Uint32 instance
+ * @throws NullPointerException if uint is null
+ */
public static Uint32 valueOf(final Uint16 uint) {
return instanceFor(uint.intValue());
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint64 value
+ * @return A Uint32 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if uint is greater than 4294967295
+ */
public static Uint32 valueOf(final Uint64 uint) {
return valueOf(uint.longValue());
}
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code uint}.
+ *
+ * @param uint UnsignedInteger value
+ * @return A Uint32 instance
+ * @throws NullPointerException if uint is null
+ */
+ public static Uint32 valueOf(final UnsignedInteger uint) {
+ return instanceFor(uint.intValue());
+ }
+
+ /**
+ * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+ * value.
+ *
+ * @param string String to parse
+ * @return A Uint32 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
+ */
public static Uint32 valueOf(final String string) {
return valueOf(string, 10);
}
+ /**
+ * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+ * value.
+ *
+ * @param string String to parse
+ * @param radix Radix to use
+ * @return A Uint32 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 4294967295
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
+ * {@code radix} is outside of allowed range.
+ */
public static Uint32 valueOf(final String string, final int radix) {
return instanceFor(Integer.parseUnsignedInt(string, radix));
}
+ /**
+ * {@inheritDoc}
+ *
+ * <p>
+ * The inverse operation is {@link #fromIntBits(int)}. In case this value is greater than {@link Integer#MAX_VALUE},
+ * the returned value will be equal to {@code this - 2^32}.
+ */
@Override
public final int intValue() {
return value;
return longValue();
}
+ /**
+ * Convert this value to an {@link UnsignedInteger}.
+ *
+ * @return An UnsignedInteger instance
+ */
public final UnsignedInteger toUnsignedInteger() {
return UnsignedInteger.fromIntBits(value);
}
return SUPPORT;
}
+ /**
+ * Return an interned (shared) instance equivalent to this object. This may return the same object.
+ *
+ * @return A shared instance.
+ */
+ public final Uint32 intern() {
+ return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
+ }
+
@Override
public final int hashCode() {
return Integer.hashCode(value);
import static com.google.common.base.Preconditions.checkArgument;
import com.google.common.annotations.Beta;
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
import com.google.common.primitives.UnsignedLong;
import java.math.BigInteger;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.kohsuke.MetaInfServices;
private static final long serialVersionUID = 1L;
private static final long MIN_VALUE_LONG = 0;
+ private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.cache.size";
+ private static final int DEFAULT_CACHE_SIZE = 256;
+
/**
- * Cache of first 256 values.
- */
- private static final Uint64[] CACHE = new Uint64[256];
- /**
- * Commonly encountered values.
+ * Tunable cache for values. By default it holds {@value #DEFAULT_CACHE_SIZE} entries. This can be
+ * changed via {@value #CACHE_SIZE_PROPERTY} system property.
*/
- private static final Uint64[] COMMON = {
- new Uint64(Short.MAX_VALUE + 1L),
- new Uint64(32768),
- new Uint64(65535),
- new Uint64(65536),
- new Uint64(Integer.MAX_VALUE),
- new Uint64(Integer.MAX_VALUE + 1L),
- new Uint64(Long.MAX_VALUE),
- new Uint64(-1L)
- };
+ private static final long CACHE_SIZE;
- public static final Uint64 MIN_VALUE = valueOf(MIN_VALUE_LONG);
- public static final Uint64 MAX_VALUE = fromLongBits(-1);
+ static {
+ final int p = Integer.getInteger(CACHE_SIZE_PROPERTY, DEFAULT_CACHE_SIZE);
+ CACHE_SIZE = p >= 0 ? Math.min(p, Integer.MAX_VALUE) : DEFAULT_CACHE_SIZE;
+ }
- /**
- * Tunable weak LRU cache for other values. By default it holds {@value #DEFAULT_LRU_SIZE} entries. This can be
- * changed via {@value #LRU_SIZE_PROPERTY} system property.
- */
- private static final int DEFAULT_LRU_SIZE = 1024;
- private static final String LRU_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.LRU.size";
- private static final int MAX_LRU_SIZE = 0xffffff;
- private static final int LRU_SIZE;
+ private static final @NonNull Uint64[] CACHE;
static {
- final int p = Integer.getInteger(LRU_SIZE_PROPERTY, DEFAULT_LRU_SIZE);
- LRU_SIZE = p >= 0 ? Math.min(p, MAX_LRU_SIZE) : DEFAULT_LRU_SIZE;
+ final Uint64[] c = new Uint64[(int) CACHE_SIZE];
+ for (int i = 0; i < c.length; ++i) {
+ c[i] = new Uint64(i);
+ }
+ CACHE = c;
}
- private static final LoadingCache<Long, Uint64> LRU = CacheBuilder.newBuilder().weakValues().maximumSize(LRU_SIZE)
- .build(new CacheLoader<Long, Uint64>() {
- @Override
- public Uint64 load(final Long key) {
- return new Uint64(key);
- }
- });
+ private static final Interner<Uint64> INTERNER = Interners.newWeakInterner();
+
+ public static final Uint64 ZERO = valueOf(0).intern();
+ public static final Uint64 ONE = valueOf(1).intern();
+ public static final Uint64 MAX_VALUE = fromLongBits(-1).intern();
private final long value;
private static Uint64 instanceFor(final long value) {
final int slot = (int)value;
- return slot >= 0 && slot < CACHE.length ? fromCache(slot, value) : fromCommon(value);
- }
-
- private static Uint64 fromCommon(final long value) {
- for (Uint64 c : COMMON) {
- if (c.value == value) {
- return c;
- }
- }
- return LRU.getUnchecked(value);
- }
-
- private static Uint64 fromCache(final int slot, final long value) {
- // FIXME: 4.0.0: use VarHandles here
- Uint64 ret = CACHE[slot];
- if (ret == null) {
- synchronized (CACHE) {
- ret = CACHE[slot];
- if (ret == null) {
- ret = new Uint64(value);
- CACHE[slot] = ret;
- }
- }
- }
- return ret;
+ return slot >= 0 && slot < CACHE.length ? CACHE[slot] : new Uint64(value);
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given bit representation. The argument is interpreted as an
+ * unsigned 64-bit value.
+ *
+ * @param bits unsigned bit representation
+ * @return A Uint64 instance
+ */
public static Uint64 fromLongBits(final long bits) {
return instanceFor(bits);
}
- public static Uint64 fromUnsignedLong(final UnsignedLong ulong) {
- return instanceFor(ulong.longValue());
- }
-
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code byteVal}. The inverse operation is
+ * {@link #byteValue()}.
+ *
+ * @param byteVal byte value
+ * @return A Uint64 instance
+ * @throws IllegalArgumentException if byteVal is less than zero
+ */
public static Uint64 valueOf(final byte byteVal) {
checkArgument(byteVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(byteVal);
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code shortVal}. The inverse operation is
+ * {@link #shortValue()}.
+ *
+ * @param shortVal short value
+ * @return A Uint64 instance
+ * @throws IllegalArgumentException if shortVal is less than zero
+ */
public static Uint64 valueOf(final short shortVal) {
checkArgument(shortVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(shortVal);
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+ *
+ * @param intVal int value
+ * @return A Uint64 instance
+ * @throws IllegalArgumentException if intVal is less than zero
+ */
public static Uint64 valueOf(final int intVal) {
- checkArgument(intVal >= MIN_VALUE_LONG, "Value %s is outside of allowed range", intVal);
+ checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(intVal);
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code longVal}. The inverse operation is
+ * {@link #fromLongBits(long)}.
+ *
+ * @param longVal long value
+ * @return A Uint8 instance
+ * @throws IllegalArgumentException if intVal is less than zero
+ */
public static Uint64 valueOf(final long longVal) {
- checkArgument(longVal >= MIN_VALUE_LONG, "Value %s is outside of allowed range", longVal);
+ checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
return instanceFor(longVal);
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint8 value
+ * @return A Uint64 instance
+ * @throws NullPointerException if uint is null
+ */
public static Uint64 valueOf(final Uint8 uint) {
return instanceFor(uint.shortValue());
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint16 value
+ * @return A Uint64 instance
+ * @throws NullPointerException if uint is null
+ */
public static Uint64 valueOf(final Uint16 uint) {
return instanceFor(uint.intValue());
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint32 value
+ * @return A Uint64 instance
+ * @throws NullPointerException if uint is null
+ */
public static Uint64 valueOf(final Uint32 uint) {
return instanceFor(uint.longValue());
}
- public static Uint64 valueOf(final String string) {
- return valueOf(string, 10);
- }
-
- public static Uint64 valueOf(final String string, final int radix) {
- return instanceFor(Long.parseUnsignedLong(string, radix));
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code ulong}.
+ *
+ * @param ulong UnsignedLong value
+ * @return A Uint64 instance
+ * @throws NullPointerException if uint is null
+ */
+ public static Uint64 valueOf(final UnsignedLong ulong) {
+ return instanceFor(ulong.longValue());
}
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code bigInt}.
+ *
+ * @param bigInt BigInteger value
+ * @return A Uint64 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if bigInt is less than zero or greater than 18446744073709551615
+ */
public static Uint64 valueOf(final BigInteger bigInt) {
checkArgument(bigInt.signum() >= 0, "Negative values not allowed");
checkArgument(bigInt.bitLength() <= Long.SIZE, "Value %s is outside of allowed range", bigInt);
-
return instanceFor(bigInt.longValue());
}
+ /**
+ * Returns an {@code Uint32} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+ * value.
+ *
+ * @param string String to parse
+ * @return A Uint64 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 18446744073709551615
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value.
+ */
+ public static Uint64 valueOf(final String string) {
+ return valueOf(string, 10);
+ }
+
+ /**
+ * Returns an {@code Uint64} holding the value of the specified {@code String}, parsed as an unsigned {@code long}
+ * value.
+ *
+ * @param string String to parse
+ * @param radix Radix to use
+ * @return A Uint64 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 18446744073709551615
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} value, or if the
+ * {@code radix} is outside of allowed range.
+ */
+ public static Uint64 valueOf(final String string, final int radix) {
+ return instanceFor(Long.parseUnsignedLong(string, radix));
+ }
+
@Override
public final int intValue() {
return (int)value;
}
+ /**
+ * {@inheritDoc}
+ *
+ * <p>
+ * The inverse operation is {@link #fromLongBits(long)}. In case this value is greater than {@link Long#MAX_VALUE},
+ * the returned value will be equal to {@code this - 2^64}.
+ */
@Override
public final long longValue() {
return value;
return UnsignedLong.fromLongBits(value).doubleValue();
}
+ /**
+ * Convert this value to an {@link UnsignedLong}.
+ *
+ * @return An UnsignedLong instance
+ */
public final UnsignedLong toUnsignedLong() {
return UnsignedLong.fromLongBits(value);
}
return SUPPORT;
}
+ /**
+ * Return an interned (shared) instance equivalent to this object. This may return the same object.
+ *
+ * @return A shared instance.
+ */
+ public final Uint64 intern() {
+ return value >= 0 && value < CACHE_SIZE ? this : INTERNER.intern(this);
+ }
+
+
@Override
public final int hashCode() {
return Long.hashCode(value);
package org.opendaylight.yangtools.yang.common;
import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.kohsuke.MetaInfServices;
private static final short MAX_VALUE_SHORT = 255;
private static final long serialVersionUID = 1L;
- private static final Uint8[] CACHE = new Uint8[MAX_VALUE_SHORT + 1];
- public static final Uint8 MIN_VALUE = valueOf(MIN_VALUE_SHORT);
+ private static final @NonNull Uint8[] CACHE;
+
+ static {
+ final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
+ for (int i = MIN_VALUE_SHORT; i <= MAX_VALUE_SHORT; ++i) {
+ c[i] = new Uint8((byte)i);
+ }
+ CACHE = c;
+ }
+
+ public static final Uint8 ZERO = valueOf(MIN_VALUE_SHORT);
+ public static final Uint8 ONE = valueOf(1);
public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
private final byte value;
}
private static Uint8 instanceFor(final byte value) {
- final int slot = Byte.toUnsignedInt(value);
-
- // FIXME: 4.0.0: use VarHandles here
- Uint8 ret = CACHE[slot];
- if (ret == null) {
- synchronized (CACHE) {
- ret = CACHE[slot];
- if (ret == null) {
- ret = new Uint8(value);
- CACHE[slot] = ret;
- }
- }
- }
-
- return ret;
+ return CACHE[Byte.toUnsignedInt(value)];
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given bit representation. The argument is interpreted as an
+ * unsigned 8-bit value.
+ *
+ * @param bits unsigned bit representation
+ * @return A Uint8 instance
+ */
public static Uint8 fromByteBits(final byte bits) {
return instanceFor(bits);
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code byteVal}. The inverse operation is {@link #byteValue()}.
+ *
+ * @param byteVal byte value
+ * @return A Uint8 instance
+ * @throws IllegalArgumentException if byteVal is less than zero
+ */
public static Uint8 valueOf(final byte byteVal) {
checkArgument(byteVal >= MIN_VALUE_SHORT, "Negative values are not allowed");
return instanceFor(byteVal);
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code shortVal}. The inverse operation is
+ * {@link #shortValue()}.
+ *
+ * @param shortVal short value
+ * @return A Uint8 instance
+ * @throws IllegalArgumentException if shortVal is less than zero or greater than 255.
+ */
public static Uint8 valueOf(final short shortVal) {
checkArgument(shortVal >= MIN_VALUE_SHORT && shortVal <= MAX_VALUE_SHORT,
"Value %s is outside of allowed range", shortVal);
return instanceFor((byte)(shortVal & 0xff));
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code intVal}. The inverse operation is {@link #intValue()}.
+ *
+ * @param intVal int value
+ * @return A Uint8 instance
+ * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
+ */
public static Uint8 valueOf(final int intVal) {
checkArgument(intVal >= MIN_VALUE_SHORT && intVal <= MAX_VALUE_SHORT,
"Value %s is outside of allowed range", intVal);
return instanceFor((byte)(intVal & 0xff));
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code longVal}. The inverse operation is
+ * {@link #longValue()}.
+ *
+ * @param longVal long value
+ * @return A Uint8 instance
+ * @throws IllegalArgumentException if intVal is less than zero or greater than 255.
+ */
public static Uint8 valueOf(final long longVal) {
checkArgument(longVal >= MIN_VALUE_SHORT && longVal <= MAX_VALUE_SHORT,
"Value %s is outside of allowed range", longVal);
return instanceFor((byte)(longVal & 0xff));
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint16 value
+ * @return A Uint8 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if uint is greater than 255.
+ */
public static Uint8 valueOf(final Uint16 uint) {
return valueOf(uint.intValue());
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint32 value
+ * @return A Uint8 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if uint is greater than 255.
+ */
public static Uint8 valueOf(final Uint32 uint) {
return valueOf(uint.longValue());
}
+ /**
+ * Returns an {@code Uint8} corresponding to a given {@code uint}.
+ *
+ * @param uint Uint64 value
+ * @return A Uint8 instance
+ * @throws NullPointerException if uint is null
+ * @throws IllegalArgumentException if uint is greater than 255.
+ */
public static Uint8 valueOf(final Uint64 uint) {
return valueOf(uint.longValue());
}
+ /**
+ * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
+ * value.
+ *
+ * @param string String to parse
+ * @return A Uint8 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value.
+ */
public static Uint8 valueOf(final String string) {
return valueOf(string, 10);
}
+ /**
+ * Returns an {@code Uint8} holding the value of the specified {@code String}, parsed as an unsigned {@code short}
+ * value.
+ *
+ * @param string String to parse
+ * @param radix Radix to use
+ * @return A Uint8 instance
+ * @throws NullPointerException if string is null
+ * @throws IllegalArgumentException if the parsed value is less than zero or greater than 255
+ * @throws NumberFormatException if the string does not contain a parsable unsigned {@code short} value, or if the
+ * {@code radix} is outside of allowed range.
+ */
public static Uint8 valueOf(final String string, final int radix) {
- return valueOf(Short.parseShort(string, radix));
+ return valueOf(Short.parseShort(requireNonNull(string), radix));
}
+ /**
+ * {@inheritDoc}
+ *
+ * <p>
+ * The inverse operation is {@link #fromByteBits(byte)}. In case this value is greater than {@link Byte#MAX_VALUE},
+ * the returned value will be equal to {@code this - 2^8}.
+ */
@Override
public final byte byteValue() {
return value;
assertSame(Uint32.valueOf(10), Uint32.valueOf(Uint16.valueOf(10)));
assertSame(Uint32.valueOf(20), Uint32.valueOf(Uint64.valueOf(20)));
- assertSame(Uint32.valueOf(5), Uint32.fromUnsignedInteger(UnsignedInteger.fromIntBits(5)));
+ assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toUnsignedInteger());
}
assertSame(Uint64.valueOf(20), Uint64.valueOf(Uint32.valueOf(20)));
assertEquals(Uint64.valueOf(30), Uint64.valueOf(new BigInteger("30")));
- assertSame(Uint64.valueOf(5), Uint64.fromUnsignedLong(UnsignedLong.fromLongBits(5)));
+ assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toUnsignedLong());
}