/** * 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 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 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(); } } }
@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); } } }
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()); } }
public void testLexicographicalComparator() { List<byte[]> ordered = Arrays.asList( new byte[] {}, new byte[] {LEAST}, new byte[] {LEAST, LEAST}, new byte[] {LEAST, (byte) 1}, new byte[] {(byte) 1}, new byte[] {(byte) 1, LEAST}, new byte[] {GREATEST, GREATEST - (byte) 1}, new byte[] {GREATEST, GREATEST}, new byte[] {GREATEST, GREATEST, GREATEST}); // The Unsafe implementation if it's available. Otherwise, the Java implementation. Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); assertSame(comparator, SerializableTester.reserialize(comparator)); // The Java implementation. Comparator<byte[]> javaImpl = UnsignedBytes.lexicographicalComparatorJavaImpl(); Helpers.testComparator(javaImpl, ordered); assertSame(javaImpl, SerializableTester.reserialize(javaImpl)); }
/** * 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 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 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 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 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 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 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 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 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 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 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 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(); } } }
@BeforeExperiment void setUp() throws Exception { Random r = new Random(); ba1 = new byte[length]; r.nextBytes(ba1); ba2 = Arrays.copyOf(ba1, ba1.length); // Differ at the last element ba3 = Arrays.copyOf(ba1, ba1.length); ba4 = Arrays.copyOf(ba1, ba1.length); ba3[ba1.length - 1] = (byte) 43; ba4[ba1.length - 1] = (byte) 42; javaImpl = UnsignedBytes.lexicographicalComparatorJavaImpl(); unsafeImpl = UnsignedBytes.LexicographicalComparatorHolder.UnsafeComparator.INSTANCE; }
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()); } }