Refine search
@Override public int compareTo(Timestamp<GTID> o) { return UnsignedBytes.lexicographicalComparator().compare(this.gtid, ((GTID) o).gtid); }
/** * Returns a string representation of {@code x} for the given radix, where {@code x} is treated as * unsigned. * * @param x the value to convert to a string. * @param radix the radix to use while working with {@code x} * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX} * and {@link Character#MAX_RADIX}. * @since 13.0 */ @Beta public static String toString(byte x, int radix) { checkArgument( radix >= Character.MIN_RADIX && radix <= Character.MAX_RADIX, "radix (%s) must be between Character.MIN_RADIX and Character.MAX_RADIX", radix); // Benchmarks indicate this is probably not worth optimizing. return Integer.toString(toInt(x), radix); }
/** * Returns a string containing the supplied {@code byte} values separated by {@code separator}. * For example, {@code join(":", (byte) 1, (byte) 2, (byte) 255)} returns the string {@code * "1:2:255"}. * * @param separator the text that should appear between consecutive values in the resulting string * (but not at the start or end) * @param array an array of {@code byte} values, possibly empty */ public static String join(String separator, byte... array) { checkNotNull(separator); if (array.length == 0) { return ""; } // For pre-sizing a builder, just get the right order of magnitude StringBuilder builder = new StringBuilder(array.length * (3 + separator.length())); builder.append(toInt(array[0])); for (int i = 1; i < array.length; i++) { builder.append(separator).append(toString(array[i])); } return builder.toString(); }
public void testSaturatedCast() { for (byte value : VALUES) { assertEquals(value, UnsignedBytes.saturatedCast(UnsignedBytes.toInt(value))); } assertEquals(GREATEST, UnsignedBytes.saturatedCast(256L)); assertEquals(LEAST, UnsignedBytes.saturatedCast(-1L)); assertEquals(GREATEST, UnsignedBytes.saturatedCast(Long.MAX_VALUE)); assertEquals(LEAST, UnsignedBytes.saturatedCast(Long.MIN_VALUE)); }
@SuppressWarnings("unchecked") public void testLexicographicalComparatorLongInputs() { Random rnd = new Random(); for (Comparator<byte[]> comparator : Arrays.asList( UnsignedBytes.lexicographicalComparator(), UnsignedBytes.lexicographicalComparatorJavaImpl())) { for (int trials = 10; trials-- > 0; ) { byte[] left = new byte[1 + rnd.nextInt(32)]; rnd.nextBytes(left); byte[] right = left.clone(); assertTrue(comparator.compare(left, right) == 0); int i = rnd.nextInt(left.length); left[i] ^= (byte) (1 + rnd.nextInt(255)); assertTrue(comparator.compare(left, right) != 0); assertEquals( comparator.compare(left, right) > 0, UnsignedBytes.compare(left[i], right[i]) > 0); } } }
/** * Returns the unsigned {@code byte} value represented by the given decimal string. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Byte#parseByte(String)}) * @since 13.0 */ @Beta @CanIgnoreReturnValue public static byte parseUnsignedByte(String string) { return parseUnsignedByte(string, 10); }
private RowResult(byte[] row, SortedMap<byte[], T> columns) { Preconditions.checkArgument(Cell.isNameValid(row)); Preconditions.checkArgument(UnsignedBytes.lexicographicalComparator().equals(columns.comparator()), "comparator for the map must be the bytes comparator"); for (byte[] colName : columns.keySet()) { Preconditions.checkArgument(Cell.isNameValid(colName)); } this.row = row.clone(); this.columns = ImmutableSortedMap.copyOf(columns, UnsignedBytes.lexicographicalComparator()); }
/** * Intended to extract this to separate method since only pendingPrepare uses ConcurrentNavigableMap. */ private ConcurrentNavigableMap<byte[], byte[]> createPendingPrepareMap() { return new ConcurrentSkipListMap<>(UnsignedBytes.lexicographicalComparator()); } }
/** * Compares the two specified {@code byte} values, treating them as unsigned values between 0 and * 255 inclusive. For example, {@code (byte) -127} is considered greater than {@code (byte) 127} * because it is seen as having the value of positive {@code 129}. * * @param a the first {@code byte} to compare * @param b the second {@code byte} to compare * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is * greater than {@code b}; or zero if they are equal */ public static int compare(byte a, byte b) { return toInt(a) - toInt(b); }
public void testLexicographicalComparatorChoice() throws Exception { Comparator<byte[]> defaultComparator = UnsignedBytes.lexicographicalComparator(); assertNotNull(defaultComparator); assertSame(defaultComparator, UnsignedBytes.lexicographicalComparator()); if (unsafeComparatorAvailable()) { assertSame(defaultComparator.getClass(), Class.forName(unsafeComparatorClassName())); } else { assertSame(defaultComparator, UnsignedBytes.lexicographicalComparatorJavaImpl()); } }
@Override public int compare(byte[] left, byte[] right) { int minLength = Math.min(left.length, right.length); for (int i = 0; i < minLength; i++) { int result = UnsignedBytes.compare(left[i], right[i]); if (result != 0) { return result; } } return left.length - right.length; }
DataType(int code) { this.code = UnsignedBytes.checkedCast(code); }
public void testCheckedCast() { for (byte value : VALUES) { assertEquals(value, UnsignedBytes.checkedCast(UnsignedBytes.toInt(value))); } assertCastFails(256L); assertCastFails(-1L); assertCastFails(Long.MAX_VALUE); assertCastFails(Long.MIN_VALUE); }
private static void assertParseFails(String value, int radix) { try { UnsignedBytes.parseUnsignedByte(value, radix); fail(); } catch (NumberFormatException expected) { } }
public void testToString() { // We can easily afford to test this exhaustively. for (int i = 0; i <= 0xff; i++) { assertEquals(Integer.toString(i), UnsignedBytes.toString((byte) i)); } }
/** * Returns the Unsafe-using Comparator, or falls back to the pure-Java implementation if unable * to do so. */ static Comparator<byte[]> getBestComparator() { try { Class<?> theClass = Class.forName(UNSAFE_COMPARATOR_NAME); // yes, UnsafeComparator does implement Comparator<byte[]> @SuppressWarnings("unchecked") Comparator<byte[]> comparator = (Comparator<byte[]>) theClass.getEnumConstants()[0]; return comparator; } catch (Throwable t) { // ensure we really catch *everything* return lexicographicalComparatorJavaImpl(); } } }
/** * Returns the unsigned {@code byte} value represented by the given decimal string. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code byte} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Byte#parseByte(String)}) * @since 13.0 */ @Beta @CanIgnoreReturnValue public static byte parseUnsignedByte(String string) { return parseUnsignedByte(string, 10); }