*/
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;
private static final CanonicalValueSupport<Uint16> SUPPORT = new Support();
private static final long serialVersionUID = 1L;
- private static final int MIN_VALUE_INT = 0;
private static final int MAX_VALUE_INT = 65535;
+ private static final String MAX_VALUE_STR = "65535";
private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint16.cache.size";
private static final int DEFAULT_CACHE_SIZE = 256;
static {
final Uint16[] c = new Uint16[CACHE_SIZE];
- for (int i = MIN_VALUE_INT; i < c.length; ++i) {
+ for (int i = 0; i < c.length; ++i) {
c[i] = new Uint16((short) i);
}
CACHE = c;
private static final Interner<Uint16> INTERNER = Interners.newWeakInterner();
- public static final Uint16 ZERO = valueOf(MIN_VALUE_INT).intern();
+ public static final Uint16 ZERO = valueOf(0).intern();
public static final Uint16 ONE = valueOf(1).intern();
public static final Uint16 MAX_VALUE = valueOf(MAX_VALUE_INT).intern();
* @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");
+ UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
return instanceFor(byteVal);
}
* @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");
+ UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
return instanceFor(shortVal);
}
* @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);
+ UintConversions.checkRange(intVal, MAX_VALUE_INT, MAX_VALUE_STR);
return instanceFor((short)(intVal & 0xffff));
}
* @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);
+ UintConversions.checkRange(longVal, MAX_VALUE_INT, MAX_VALUE_STR);
return instanceFor((short)(longVal & 0xffff));
}
*/
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;
private static final CanonicalValueSupport<Uint32> SUPPORT = new Support();
private static final long serialVersionUID = 1L;
- private static final long MIN_VALUE_LONG = 0;
- private static final long MAX_VALUE_LONG = 0xffffffffL;
+ private static final long MAX_VALUE_LONG = 4294967295L;
+ private static final String MAX_VALUE_STR = "4294967295";
private static final String CACHE_SIZE_PROPERTY = "org.opendaylight.yangtools.yang.common.Uint32.cache.size";
private static final int DEFAULT_CACHE_SIZE = 256;
* @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");
+ UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
return instanceFor(byteVal);
}
* @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");
+ UintConversions.checkNonNegative(shortVal, MAX_VALUE_STR);
return instanceFor(shortVal);
}
* @throws IllegalArgumentException if intVal is less than zero
*/
public static Uint32 valueOf(final int intVal) {
- checkArgument(intVal >= MIN_VALUE_LONG, "Negative values are not allowed");
+ UintConversions.checkNonNegative(intVal, MAX_VALUE_STR);
return instanceFor(intVal);
}
* @throws IllegalArgumentException if longVal 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);
+ UintConversions.checkRange(longVal, MAX_VALUE_LONG, MAX_VALUE_STR);
return instanceFor((int)longVal);
}
* {@code radix} is outside of allowed range.
*/
public static Uint32 valueOf(final String string, final int radix) {
- return instanceFor(Integer.parseUnsignedInt(string, radix));
+ return instanceFor(Integer.parseUnsignedInt(requireNonNull(string), radix));
}
/**
*/
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;
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;
* @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));
}
@Override
*/
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;
private static final CanonicalValueSupport<Uint8> SUPPORT = new Support();
- private static final short MIN_VALUE_SHORT = 0;
private static final short MAX_VALUE_SHORT = 255;
+ private static final String MAX_VALUE_STR = "255";
private static final long serialVersionUID = 1L;
static {
final Uint8[] c = new Uint8[MAX_VALUE_SHORT + 1];
- for (int i = MIN_VALUE_SHORT; i <= MAX_VALUE_SHORT; ++i) {
+ for (int i = 0; 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 ZERO = valueOf(0);
public static final Uint8 ONE = valueOf(1);
public static final Uint8 MAX_VALUE = valueOf(MAX_VALUE_SHORT);
* @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");
+ UintConversions.checkNonNegative(byteVal, MAX_VALUE_STR);
return instanceFor(byteVal);
}
* @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);
+ UintConversions.checkRange(shortVal, MAX_VALUE_SHORT, MAX_VALUE_STR);
return instanceFor((byte)(shortVal & 0xff));
}
* @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);
+ UintConversions.checkRange(intVal, MAX_VALUE_SHORT, MAX_VALUE_STR);
return instanceFor((byte)(intVal & 0xff));
}
* @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);
+ UintConversions.checkRange(longVal, MAX_VALUE_SHORT, MAX_VALUE_STR);
return instanceFor((byte)(longVal & 0xff));
}
public static Uint64 fromGuava(final UnsignedLong value) {
return Uint64.valueOf(value);
}
+
+ static void checkNonNegative(final byte value, final String maxValue) {
+ if (value < 0) {
+ throwIAE(value, maxValue);
+ }
+ }
+
+ static void checkNonNegative(final short value, final String maxStr) {
+ if (value < 0) {
+ throwIAE(value, maxStr);
+ }
+ }
+
+ static void checkNonNegative(final int value, final String maxStr) {
+ if (value < 0) {
+ throwIAE(value, maxStr);
+ }
+ }
+
+ static void checkRange(final short value, final short max, final String maxStr) {
+ if (value < 0 || value > max) {
+ throwIAE(value, maxStr);
+ }
+ }
+
+ static void checkRange(final int value, final int max, final String maxStr) {
+ if (value < 0 || value > max) {
+ throwIAE(value, maxStr);
+ }
+ }
+
+ static void checkRange(final long value, final long max, final String maxStr) {
+ if (value < 0 || value > max) {
+ throwIAE(value, maxStr);
+ }
+ }
+
+ private static void throwIAE(final int value, final String max) {
+ // "Invalid range: 65536, expected: [[0..65535]]."
+ throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
+ }
+
+ private static void throwIAE(final long value, final String max) {
+ // "Invalid range: 65536, expected: [[0..65535]]."
+ throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
+ }
}
public void testBigLong() {
Uint16.valueOf(65536L);
}
+
+ @Test(expected = NullPointerException.class)
+ public void testNullValueOfString() {
+ Uint16.valueOf((String) null);
+ }
}
public void testBigLong() {
Uint32.valueOf(4294967296L);
}
+
+ @Test(expected = NullPointerException.class)
+ public void testNullValueOfString() {
+ Uint32.valueOf((String) null);
+ }
}
public void testBigBigInteger() {
Uint64.valueOf(new BigInteger("0x10000000000000000"));
}
+
+ @Test(expected = NullPointerException.class)
+ public void testNullValueOfString() {
+ Uint64.valueOf((String) null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testNullValueOfBigInteger() {
+ Uint64.valueOf((BigInteger) null);
+ }
}
public void testBigLong() {
Uint8.valueOf(256L);
}
+
+ @Test(expected = NullPointerException.class)
+ public void testNullValueOfString() {
+ Uint8.valueOf((String) null);
+ }
}