return intValue();
}
+ /**
+ * Convert this value to a {@code Uint8}.
+ *
+ * @return A Uint8
+ * @throws IllegalArgumentException if this value is greater than 255.
+ */
+ public final Uint8 toUint8() {
+ return Uint8.valueOf(toJava());
+ }
+
+ /**
+ * Convert this value to a {@code Uint32}.
+ *
+ * @return A Uint32
+ */
+ public final Uint32 toUint32() {
+ return Uint32.fromIntBits(intValue());
+ }
+
+ /**
+ * Convert this value to a {@code Uint64}.
+ *
+ * @return A Uint64
+ */
+ public final Uint64 toUint64() {
+ return Uint64.fromLongBits(longValue());
+ }
+
@Override
public final int hashCode() {
return Short.hashCode(value);
return UnsignedInteger.fromIntBits(value);
}
+ /**
+ * Convert this value to a {@code Uint8}.
+ *
+ * @return A Uint8
+ * @throws IllegalArgumentException if this value is greater than 255.
+ */
+ public final Uint8 toUint8() {
+ return Uint8.valueOf(toJava());
+ }
+
+ /**
+ * Convert this value to a {@code Uint16}.
+ *
+ * @return A Uint16
+ * @throws IllegalArgumentException if this value is greater than 65535.
+ */
+ public final Uint16 toUint16() {
+ return Uint16.valueOf(toJava());
+ }
+
+ /**
+ * Convert this value to a {@code Uint64}.
+ *
+ * @return A Uint64
+ */
+ public final Uint64 toUint64() {
+ return Uint64.fromLongBits(longValue());
+ }
+
@Override
public final int hashCode() {
return Integer.hashCode(value);
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 shortValue();
}
+ /**
+ * Convert this value to a {@code Uint16}.
+ *
+ * @return A Uint16
+ */
+ public final Uint16 toUint16() {
+ return Uint16.fromShortBits(shortValue());
+ }
+
+ /**
+ * Convert this value to a {@code Uint32}.
+ *
+ * @return A Uint32
+ */
+ public final Uint32 toUint32() {
+ return Uint32.fromIntBits(intValue());
+ }
+
+ /**
+ * Convert this value to a {@code Uint64}.
+ *
+ * @return A Uint64
+ */
+ public final Uint64 toUint64() {
+ return Uint64.fromLongBits(longValue());
+ }
+
@Override
public final int hashCode() {
return Byte.hashCode(value);
}
}
+ static void throwIAE(final String value, final long max) {
+ // "Invalid range: 65536, expected: [[0..65535]]."
+ throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
+ }
+
private static void throwIAE(final int value, final int max) {
// "Invalid range: 65536, expected: [[0..65535]]."
throw new IllegalArgumentException("Invalid range: " + value + ", expected: [[0.." + max + "]].");
assertSame(Uint16.valueOf(5), Uint16.valueOf(Uint8.valueOf(5)));
assertSame(Uint16.valueOf(10), Uint16.valueOf(Uint32.valueOf(10)));
assertSame(Uint16.valueOf(20), Uint16.valueOf(Uint64.valueOf(20)));
+
+ assertEquals(Uint8.TEN, Uint16.TEN.toUint8());
+ assertEquals(Uint32.valueOf(65535), Uint16.MAX_VALUE.toUint32());
+ assertEquals(Uint64.valueOf(65535), Uint16.MAX_VALUE.toUint64());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testToUint8() {
+ Uint16.MAX_VALUE.toUint8();
}
@Test
assertSame(Uint32.valueOf(5), Uint32.valueOf(UnsignedInteger.fromIntBits(5)));
assertEquals(UnsignedInteger.fromIntBits(5), Uint32.valueOf(5).toGuava());
+
+ assertEquals(Uint8.TEN, Uint32.TEN.toUint8());
+ assertEquals(Uint16.TEN, Uint32.TEN.toUint16());
+ assertEquals(Uint64.valueOf(4294967295L), Uint32.MAX_VALUE.toUint64());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testToUint8() {
+ Uint32.MAX_VALUE.toUint8();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testToUint16() {
+ Uint32.MAX_VALUE.toUint16();
}
@Test
assertSame(Uint64.valueOf(5), Uint64.valueOf(UnsignedLong.fromLongBits(5)));
assertEquals(UnsignedLong.fromLongBits(5), Uint64.valueOf(5).toGuava());
+
+ assertEquals(Uint8.TEN, Uint64.TEN.toUint8());
+ assertEquals(Uint16.TEN, Uint64.TEN.toUint16());
+ assertEquals(Uint32.TEN, Uint64.TEN.toUint32());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testToUint8() {
+ Uint64.MAX_VALUE.toUint8();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testToUint16() {
+ Uint64.MAX_VALUE.toUint16();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testToUint32() {
+ Uint64.MAX_VALUE.toUint32();
}
@Test
assertSame(Uint8.valueOf(5), Uint8.valueOf(Uint16.valueOf(5)));
assertSame(Uint8.valueOf(10), Uint8.valueOf(Uint32.valueOf(10)));
assertSame(Uint8.valueOf(20), Uint8.valueOf(Uint64.valueOf(20)));
+
+ assertEquals(Uint16.valueOf(255), Uint8.MAX_VALUE.toUint16());
+ assertEquals(Uint32.valueOf(255), Uint8.MAX_VALUE.toUint32());
+ assertEquals(Uint64.valueOf(255), Uint8.MAX_VALUE.toUint64());
}
@Test