@Override public int compare(T a, T b) { return forwardOrder.compare(b, a); }
@Override public int compare(String s, String s2) { // Avoid conversion to bytes for equal references // Assuming we mostly compare different strings, checking s.equals(s2) will only make the comparison slower. //noinspection StringEquality if (s == s2) { return 0; } return ORDERING.compare(s, s2); }
@Override public int compare(F left, F right) { return ordering.compare(function.apply(left), function.apply(right)); }
/** * Returns {@code true} if each element in {@code iterable} after the first is greater than or * equal to the element that preceded it, according to this ordering. Note that this is always * true when the iterable has fewer than two elements. * * <p><b>Java 8 users:</b> Use the equivalent {@link Comparators#isInOrder(Iterable, Comparator)} * instead, since the rest of {@code Ordering} is mostly obsolete (as explained in the class * documentation). */ public boolean isOrdered(Iterable<? extends T> iterable) { Iterator<? extends T> it = iterable.iterator(); if (it.hasNext()) { T prev = it.next(); while (it.hasNext()) { T next = it.next(); if (compare(prev, next) > 0) { return false; } prev = next; } } return true; }
@Override public int compare(String o1, String o2) { return Comparators.<String>naturalNullsFirst().compare(o1, o2); } };
@Override public int compare(@Nullable T left, @Nullable T right) { if (left == right) { return 0; } if (left == null) { return RIGHT_IS_GREATER; } if (right == null) { return LEFT_IS_GREATER; } return ordering.compare(left, right); }
@Override public int compare(@Nullable T left, @Nullable T right) { if (left == right) { return 0; } if (left == null) { return LEFT_IS_GREATER; } if (right == null) { return RIGHT_IS_GREATER; } return ordering.compare(left, right); }
@Override protected ExprEval evalString(@Nullable String left, @Nullable String right) { return ExprEval.of(Comparators.<String>naturalNullsFirst().compare(left, right) > 0, ExprType.LONG); }
@Override public int compare(Invokable<?, ?> left, Invokable<?, ?> right) { return Ordering.usingToString().compare(left.getParameters(), right.getParameters()); } };
/** * Returns the lesser of the two values according to this ordering. If the values compare as 0, * the first is returned. * * <p><b>Implementation note:</b> this method is invoked by the default implementations of the * other {@code min} overloads, so overriding it will affect their behavior. * * <p><b>Java 8 users:</b> Use {@code Collections.min(Arrays.asList(a, b), thisComparator)} * instead (but note that it does not guarantee which tied minimum element is returned). * * @param a value to compare, returned if less than or equal to b. * @param b value to compare. * @throws ClassCastException if the parameters are not <i>mutually comparable</i> under this * ordering. */ @CanIgnoreReturnValue // TODO(kak): Consider removing this public <E extends T> E min(@Nullable E a, @Nullable E b) { return (compare(a, b) <= 0) ? a : b; }
/** * Returns the greater of the two values according to this ordering. If the values compare as 0, * the first is returned. * * <p><b>Implementation note:</b> this method is invoked by the default implementations of the * other {@code max} overloads, so overriding it will affect their behavior. * * <p><b>Java 8 users:</b> Use {@code Collections.max(Arrays.asList(a, b), thisComparator)} * instead (but note that it does not guarantee which tied maximum element is returned). * * @param a value to compare, returned if greater than or equal to b. * @param b value to compare. * @throws ClassCastException if the parameters are not <i>mutually comparable</i> under this * ordering. */ @CanIgnoreReturnValue // TODO(kak): Consider removing this public <E extends T> E max(@Nullable E a, @Nullable E b) { return (compare(a, b) >= 0) ? a : b; }
public void testFrom() { Ordering<String> caseInsensitiveOrdering = Ordering.from(String.CASE_INSENSITIVE_ORDER); assertEquals(0, caseInsensitiveOrdering.compare("A", "a")); assertTrue(caseInsensitiveOrdering.compare("a", "B") < 0); assertTrue(caseInsensitiveOrdering.compare("B", "a") > 0); @SuppressWarnings("deprecation") // test of deprecated method Ordering<String> orderingFromOrdering = Ordering.from(Ordering.<String>natural()); new EqualsTester() .addEqualityGroup(caseInsensitiveOrdering, Ordering.from(String.CASE_INSENSITIVE_ORDER)) .addEqualityGroup(orderingFromOrdering, Ordering.natural()) .testEquals(); }
public void testLowerRange() { for (BoundType lBoundType : BoundType.values()) { GeneralRange<Integer> range = GeneralRange.downTo(ORDERING, 3, lBoundType); for (Integer i : IN_ORDER_VALUES) { assertEquals( ORDERING.compare(i, 3) > 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), range.contains(i)); assertEquals( ORDERING.compare(i, 3) < 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), range.tooLow(i)); assertFalse(range.tooHigh(i)); } } }
public void testUpperRange() { for (BoundType lBoundType : BoundType.values()) { GeneralRange<Integer> range = GeneralRange.upTo(ORDERING, 3, lBoundType); for (Integer i : IN_ORDER_VALUES) { assertEquals( ORDERING.compare(i, 3) < 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == CLOSED), range.contains(i)); assertEquals( ORDERING.compare(i, 3) > 0 || (ORDERING.compare(i, 3) == 0 && lBoundType == OPEN), range.tooHigh(i)); assertFalse(range.tooLow(i)); } } }
public void testAllEqual() { Ordering<Object> comparator = Ordering.allEqual(); assertSame(comparator, comparator.reverse()); assertEquals(0, comparator.compare(null, null)); assertEquals(0, comparator.compare(new Object(), new Object())); assertEquals(0, comparator.compare("apples", "oranges")); assertSame(comparator, reserialize(comparator)); assertEquals("Ordering.allEqual()", comparator.toString()); List<String> strings = ImmutableList.of("b", "a", "d", "c"); assertEquals(strings, comparator.sortedCopy(strings)); assertEquals(strings, comparator.immutableSortedCopy(strings)); }
public void testNatural() { Ordering<Integer> comparator = Ordering.natural(); Helpers.testComparator(comparator, Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE); try { comparator.compare(1, null); fail(); } catch (NullPointerException expected) { } try { comparator.compare(null, 2); fail(); } catch (NullPointerException expected) { } try { comparator.compare(null, null); fail(); } catch (NullPointerException expected) { } assertSame(comparator, reserialize(comparator)); assertEquals("Ordering.natural()", comparator.toString()); }
public boolean replaces(LookupExtractorFactoryContainer other) { if (version == null && other.getVersion() == null) { return this.lookupExtractorFactory.replaces(other.getLookupExtractorFactory()); } return Comparators.<String>naturalNullsFirst().compare(version, other.getVersion()) > 0; }
public void testSingletonRange() { GeneralRange<Integer> range = GeneralRange.range(ORDERING, 3, CLOSED, 3, CLOSED); for (Integer i : IN_ORDER_VALUES) { assertEquals(ORDERING.compare(i, 3) == 0, range.contains(i)); } }
@Test public void testCompareDay() { Result<Object> res = new Result<Object>(time, null); Result<Object> same = new Result<Object>(time.plusHours(12), null); Result<Object> greater = new Result<Object>(time.plusHours(25), null); Result<Object> less = new Result<Object>(time.minusHours(1), null); Granularity day = Granularities.DAY; Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, same), 0); Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, greater), descending ? 1 : -1); Assert.assertEquals(ResultGranularTimestampComparator.create(day, descending).compare(res, less), descending ? -1 : 1); }
@Test public void testCompareAll() { Result<Object> r1 = new Result<Object>(time, null); Result<Object> r2 = new Result<Object>(time.plusYears(5), null); Assert.assertEquals(ResultGranularTimestampComparator.create(Granularities.ALL, descending).compare(r1, r2), 0); }