*/
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.collect.Interner;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
-import org.kohsuke.MetaInfServices;
import org.opendaylight.yangtools.concepts.Variant;
/**
@Beta
@NonNullByDefault
public class Uint64 extends Number implements CanonicalValue<Uint64> {
- @MetaInfServices(value = CanonicalValueSupport.class)
public static final class Support extends AbstractCanonicalValueSupport<Uint64> {
public Support() {
super(Uint64.class);
private static final CanonicalValueSupport<Uint64> SUPPORT = new Support();
private static final long serialVersionUID = 1L;
- private static final long MIN_VALUE_LONG = 0;
+ private static final String MAX_VALUE_STR = Long.toUnsignedString(-1);
private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint64.cache.size";
private static final int DEFAULT_CACHE_SIZE = 256;
private static final Interner<Uint64> INTERNER = Interners.newWeakInterner();
+ /**
+ * Value of {@code 0}.
+ */
public static final Uint64 ZERO = valueOf(0).intern();
+ /**
+ * Value of {@code 1}.
+ */
public static final Uint64 ONE = valueOf(1).intern();
+ /**
+ * Value of {@code 2}.
+ */
+ public static final Uint64 TWO = valueOf(2).intern();
+ /**
+ * Value of {@code 10}.
+ */
+ public static final Uint64 TEN = valueOf(10).intern();
+ /**
+ * Value of {@code 18446744073709551615}.
+ */
public static final Uint64 MAX_VALUE = fromLongBits(-1).intern();
private final long value;
- Uint64(final long value) {
+ private Uint64(final long value) {
this.value = value;
}
protected Uint64(final Uint64 other) {
- this.value = other.value;
+ this(other.value);
}
private static Uint64 instanceFor(final long value) {
- final int slot = (int)value;
- return slot >= 0 && slot < CACHE.length ? CACHE[slot] : new Uint64(value);
+ return value >= 0 && value < CACHE.length ? CACHE[(int) value] : new Uint64(value);
}
/**
* @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");
+ UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
return instanceFor(byteVal);
}
* @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");
+ UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
return instanceFor(shortVal);
}
* @throws IllegalArgumentException if intVal is less than zero
*/
public static Uint64 valueOf(final int intVal) {
- checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
+ UintConversions.checkNonNegative(intVal, MAX_VALUE_STR);
return instanceFor(intVal);
}
/**
- * Returns an {@code Uint64} corresponding to a given {@code longVal}. The inverse operation is
- * {@link #fromLongBits(long)}.
+ * Returns an {@code Uint64} corresponding to a given {@code longVal}, which is checked for range.
+ * See also {@link #longValue()} and {@link #fromLongBits(long)}.
*
* @param longVal long value
* @return A Uint8 instance
* @throws IllegalArgumentException if longVal is less than zero
*/
public static Uint64 valueOf(final long longVal) {
- checkArgument(longVal >= MIN_VALUE_LONG, "Negative values are not allowed");
- return instanceFor(longVal);
+ if (longVal >= 0) {
+ return instanceFor(longVal);
+ }
+ throw new IllegalArgumentException("Invalid range: " + longVal + ", expected: [[0..18446744073709551615]].");
}
/**
* @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());
+ if (bigInt.signum() >= 0 && bigInt.bitLength() <= Long.SIZE) {
+ return instanceFor(bigInt.longValue());
+ }
+ throw new IllegalArgumentException("Invalid range: " + bigInt + ", expected: [[0..18446744073709551615]].");
}
/**
* {@code radix} is outside of allowed range.
*/
public static Uint64 valueOf(final String string, final int radix) {
- return instanceFor(Long.parseUnsignedLong(string, radix));
+ return instanceFor(Long.parseUnsignedLong(requireNonNull(string), radix));
+ }
+
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code byteVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param byteVal byte value
+ * @return A Uint64 instance
+ */
+ public static Uint64 saturatedOf(final byte byteVal) {
+ return byteVal < 0 ? Uint64.ZERO : instanceFor(byteVal);
+ }
+
+ /**
+ * Returns an {@code Uint32} corresponding to a given {@code shortVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param shortVal short value
+ * @return A Uint32 instance
+ */
+ public static Uint64 saturatedOf(final short shortVal) {
+ return shortVal < 0 ? Uint64.ZERO : instanceFor(shortVal);
+ }
+
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code intVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param intVal int value
+ * @return A Uint64 instance
+ */
+ public static Uint64 saturatedOf(final int intVal) {
+ return intVal < 0 ? Uint64.ZERO : instanceFor(intVal);
+ }
+
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code longVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned.
+ *
+ * @param longVal long value
+ * @return A Uint64 instance
+ */
+ public static Uint64 saturatedOf(final long longVal) {
+ return longVal < 0 ? Uint64.ZERO : instanceFor(longVal);
+ }
+
+ /**
+ * Returns an {@code Uint64} corresponding to a given {@code longVal} if it is representable. If the value is
+ * negative {@link #ZERO} will be returned. If the value is greater than 18446744073709551615, {@link #MAX_VALUE}
+ * will be returned.
+ *
+ * @param bigInt BigInteger value
+ * @return A Uint64 instance
+ * @throws NullPointerException if bigInt is null
+ */
+ public static Uint64 saturatedOf(final BigInteger bigInt) {
+ if (bigInt.signum() < 0) {
+ return Uint64.ZERO;
+ }
+ return bigInt.bitLength() > Long.SIZE ? Uint64.MAX_VALUE : instanceFor(bigInt.longValue());
}
@Override
return UnsignedLong.fromLongBits(value);
}
+ /**
+ * Convert this value to a {@code Uint8}.
+ *
+ * @return A Uint8
+ * @throws IllegalArgumentException if this value is greater than 255.
+ */
+ public final Uint8 toUint8() {
+ if ((value & 0xFFFFFFFFFFFFFF00L) != 0) {
+ UintConversions.throwIAE(toString(), 255);
+ }
+ return Uint8.fromByteBits((byte) value);
+ }
+
+ /**
+ * Convert this value to a {@code Uint16}.
+ *
+ * @return A Uint16
+ * @throws IllegalArgumentException if this value is greater than 65535.
+ */
+ public final Uint16 toUint16() {
+ if ((value & 0xFFFFFFFFFFFF0000L) != 0) {
+ UintConversions.throwIAE(toString(), 65535);
+ }
+ return Uint16.fromShortBits((short) value);
+ }
+
+ /**
+ * Convert this value to a {@code Uint64}.
+ *
+ * @return A Uint32
+ * @throws IllegalArgumentException if this value is greater than 4294967295.
+ */
+ public final Uint32 toUint32() {
+ if ((value & 0xFFFFFFFF00000000L) != 0) {
+ UintConversions.throwIAE(toString(), 4294967295L);
+ }
+ return Uint32.fromIntBits((int) value);
+ }
+
@Override
public final int hashCode() {
return Long.hashCode(value);
return this == obj || obj instanceof Uint64 && value == ((Uint64)obj).value;
}
+ /**
+ * A slightly faster version of {@link #equals(Object)}.
+ *
+ * @param obj Uint64 object
+ * @return {@code true} if this object is the same as the obj argument; {@code false} otherwise.
+ */
+ public final boolean equals(final @Nullable Uint64 obj) {
+ return this == obj || obj != null && value == obj.value;
+ }
+
@Override
public final String toString() {
return toCanonicalString();