Refine search
/** Returns the value of this {@code UnsignedInteger} as a {@code long}. */ @Override public long longValue() { return toLong(value); }
@Override public int compare(int[] left, int[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { if (left[i] != right[i]) { return UnsignedInts.compare(left[i], right[i]); } } return left.length - right.length; }
/** * Returns a string representation of the {@code UnsignedInteger} value, in base {@code radix}. If * {@code radix < Character.MIN_RADIX} or {@code radix > Character.MAX_RADIX}, the radix {@code * 10} is used. */ public String toString(int radix) { return UnsignedInts.toString(value, radix); } }
/** * Returns this mod {@code val}. * * @throws ArithmeticException if {@code val} is zero * @since 14.0 */ public UnsignedInteger mod(UnsignedInteger val) { return fromIntBits(UnsignedInts.remainder(value, checkNotNull(val).value)); }
/** * Returns the result of dividing this by {@code val}. * * @throws ArithmeticException if {@code val} is zero * @since 14.0 */ public UnsignedInteger dividedBy(UnsignedInteger val) { return fromIntBits(UnsignedInts.divide(value, checkNotNull(val).value)); }
public void testFromIntBitsLongValue() { for (int value : TEST_INTS) { long expected = value & 0xffffffffL; assertEquals( UnsignedInts.toString(value), expected, UnsignedInteger.fromIntBits(value).longValue()); } }
public void testFromIntBitsAndIntValueAreInverses() { for (int value : TEST_INTS) { assertEquals( UnsignedInts.toString(value), value, UnsignedInteger.fromIntBits(value).intValue()); } }
/** * Returns an {@code UnsignedInteger} holding the value of the specified {@code String}, parsed as * an unsigned {@code int} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} * value */ public static UnsignedInteger valueOf(String string, int radix) { return fromIntBits(UnsignedInts.parseUnsignedInt(string, radix)); }
/** * Returns the unsigned {@code int} value represented by the given decimal string. * * <p><b>Java 8 users:</b> use {@link Integer#parseUnsignedInt(String)} instead. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code int} value * @throws NullPointerException if {@code s} is null (in contrast to {@link * Integer#parseInt(String)}) */ @CanIgnoreReturnValue public static int parseUnsignedInt(String s) { return parseUnsignedInt(s, 10); }
/** * Returns the greatest value present in {@code array}, treating values as unsigned. * * @param array a <i>nonempty</i> array of unsigned {@code int} values * @return the value present in {@code array} that is greater than or equal to every other value * in the array according to {@link #compare} * @throws IllegalArgumentException if {@code array} is empty */ public static int max(int... array) { checkArgument(array.length > 0); int max = flip(array[0]); for (int i = 1; i < array.length; i++) { int next = flip(array[i]); if (next > max) { max = next; } } return flip(max); }
public void testSaturatedCast() { for (long value : UNSIGNED_INTS) { assertEquals(value, UnsignedInts.toLong(UnsignedInts.saturatedCast(value))); } assertEquals(GREATEST, UnsignedInts.saturatedCast(1L << 32)); assertEquals(LEAST, UnsignedInts.saturatedCast(-1L)); assertEquals(GREATEST, UnsignedInts.saturatedCast(Long.MAX_VALUE)); assertEquals(LEAST, UnsignedInts.saturatedCast(Long.MIN_VALUE)); }
public void testCheckedCast() { for (long value : UNSIGNED_INTS) { assertEquals(value, UnsignedInts.toLong(UnsignedInts.checkedCast(value))); } assertCastFails(1L << 32); assertCastFails(-1L); assertCastFails(Long.MAX_VALUE); assertCastFails(Long.MIN_VALUE); }
@GwtIncompatible // Too slow in GWT (~3min fully optimized) public void testDivideRemainderEuclideanProperty() { // Use a seed so that the test is deterministic: Random r = new Random(0L); for (int i = 0; i < 1000000; i++) { int dividend = r.nextInt(); int divisor = r.nextInt(); // Test that the Euclidean property is preserved: assertTrue( dividend - (divisor * UnsignedInts.divide(dividend, divisor) + UnsignedInts.remainder(dividend, divisor)) == 0); } }
public void testRemainder() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { try { assertEquals((int) (a % b), UnsignedInts.remainder((int) a, (int) b)); assertFalse(b == 0); } catch (ArithmeticException e) { assertEquals(0, b); } } } }
public void testDivide() { for (long a : UNSIGNED_INTS) { for (long b : UNSIGNED_INTS) { try { assertEquals((int) (a / b), UnsignedInts.divide((int) a, (int) b)); assertFalse(b == 0); } catch (ArithmeticException e) { assertEquals(0, b); } } } }
/** * Returns the result of dividing this by {@code val}. */ public UnsignedInteger divide(UnsignedInteger val) { checkNotNull(val); return asUnsigned(UnsignedInts.divide(value, val.value)); }
/** * Returns the remainder of dividing this by {@code val}. */ public UnsignedInteger remainder(UnsignedInteger val) { checkNotNull(val); return asUnsigned(UnsignedInts.remainder(value, val.value)); }
/** * Returns this mod {@code val}. * * @throws ArithmeticException if {@code val} is zero * @since 14.0 */ public UnsignedInteger mod(UnsignedInteger val) { return fromIntBits(UnsignedInts.remainder(value, checkNotNull(val).value)); }
/** * Returns the result of dividing this by {@code val}. * * @throws ArithmeticException if {@code val} is zero * @since 14.0 */ public UnsignedInteger dividedBy(UnsignedInteger val) { return fromIntBits(UnsignedInts.divide(value, checkNotNull(val).value)); }
/** * Returns an {@code UnsignedInteger} holding the value of the specified {@code String}, parsed as * an unsigned {@code int} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code int} * value */ public static UnsignedInteger valueOf(String string, int radix) { return fromIntBits(UnsignedInts.parseUnsignedInt(string, radix)); }