/** * Returns a {@code Collector} that returns the {@code k} greatest (relative to the specified * {@code Comparator}) input elements, in descending order, as an unmodifiable {@code List}. Ties * are broken arbitrarily. * * <p>For example: * * <pre>{@code * Stream.of("foo", "quux", "banana", "elephant") * .collect(greatest(2, comparingInt(String::length))) * // returns {"elephant", "banana"} * }</pre> * * <p>This {@code Collector} uses O(k) memory and takes expected time O(n) (worst-case O(n log * k)), as opposed to e.g. {@code Stream.sorted(comparator.reversed()).limit(k)}, which currently * takes O(n log n) time and O(n) space. * * @throws IllegalArgumentException if {@code k < 0} * @since 22.0 */ public static <T> Collector<T, ?, List<T>> greatest(int k, Comparator<? super T> comparator) { return least(k, comparator.reversed()); }
@Override public int compareTo(DocumentPath that) { return Comparators.lexicographical(Comparator.<String>naturalOrder()) .compare(this.pathElements, that.pathElements); } }
public void testIsInStrictOrder() { assertFalse(Comparators.isInStrictOrder(asList(5, 3, 0, 9), Ordering.natural())); assertFalse(Comparators.isInStrictOrder(asList(0, 5, 3, 9), Ordering.natural())); assertTrue(Comparators.isInStrictOrder(asList(0, 3, 5, 9), Ordering.natural())); assertFalse(Comparators.isInStrictOrder(asList(0, 0, 3, 3), Ordering.natural())); assertTrue(Comparators.isInStrictOrder(asList(0, 3), Ordering.natural())); assertTrue(Comparators.isInStrictOrder(Collections.singleton(1), Ordering.natural())); assertTrue(Comparators.isInStrictOrder(Collections.<Integer>emptyList(), Ordering.natural())); }
/** * Returns a comparator which tries two comparators in order until a non-zero * result is found, returning that result, and returning zero only if both * comparators return zero. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @see #compound(Iterable) */ @SuppressWarnings("unchecked") public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second) { Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; return compound(Arrays.asList(firstT, secondT)); }
public void testEmptiesFirst() { Optional<String> empty = Optional.empty(); Optional<String> abc = Optional.of("abc"); Optional<String> z = Optional.of("z"); Comparator<Optional<String>> comparator = Comparators.emptiesFirst(comparing(String::length)); Helpers.testComparator(comparator, empty, z, abc); // Just demonstrate that no explicit type parameter is required comparator = Comparators.emptiesFirst(naturalOrder()); }
public int compare(T left, T right) { int leftCount = multiset.count(left); int rightCount = multiset.count(right); return Comparators.compare(leftCount, rightCount); }
/** * Returns a comparator which tries three comparators in order until a * non-zero result is found, returning that result, and returning zero only if * all comparators return zero. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @param third the third comparator to invoke * @see #compound(Iterable) */ @SuppressWarnings("unchecked") public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third) { Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; Comparator<T> thirdT = (Comparator<T>) third; return compound(Arrays.asList(firstT, secondT, thirdT)); }
public void testEmptiesFirst() { Optional<String> empty = Optional.empty(); Optional<String> abc = Optional.of("abc"); Optional<String> z = Optional.of("z"); Comparator<Optional<String>> comparator = Comparators.emptiesFirst(comparing(String::length)); Helpers.testComparator(comparator, empty, z, abc); } }
@SuppressWarnings("unchecked") // dang varargs public void testLexicographical() { Comparator<String> comparator = Ordering.natural(); Comparator<Iterable<String>> lexy = Comparators.lexicographical(comparator); ImmutableList<String> empty = ImmutableList.of(); ImmutableList<String> a = ImmutableList.of("a"); ImmutableList<String> aa = ImmutableList.of("a", "a"); ImmutableList<String> ab = ImmutableList.of("a", "b"); ImmutableList<String> b = ImmutableList.of("b"); Helpers.testComparator(lexy, empty, a, aa, ab, b); new EqualsTester() .addEqualityGroup(lexy, Comparators.lexicographical(comparator)) .addEqualityGroup(Comparators.lexicographical(String.CASE_INSENSITIVE_ORDER)) .addEqualityGroup(Ordering.natural()) .testEquals(); }
/** * Returns a {@code Collector} that returns the {@code k} greatest (relative to the specified * {@code Comparator}) input elements, in descending order, as an unmodifiable {@code List}. Ties * are broken arbitrarily. * * <p>For example: * * <pre>{@code * Stream.of("foo", "quux", "banana", "elephant") * .collect(greatest(2, comparingInt(String::length))) * // returns {"elephant", "banana"} * }</pre> * * <p>This {@code Collector} uses O(k) memory and takes expected time O(n) (worst-case O(n log * k)), as opposed to e.g. {@code Stream.sorted(comparator.reversed()).limit(k)}, which currently * takes O(n log n) time and O(n) space. * * @throws IllegalArgumentException if {@code k < 0} * @since 22.0 */ public static <T> Collector<T, ?, List<T>> greatest(int k, Comparator<? super T> comparator) { return least(k, comparator.reversed()); }
@Test public void testMultiAbiComparator_exhaustiveTargetingList() { ImmutableList<VariantTargeting> exhaustiveTargetingVariants = EXHAUSTIVE_TARGETING_LIST.stream() .map(TargetingComparatorsTest::fromAbiSet) .collect(toImmutableList()); assertThat( Comparators.isInStrictOrder( exhaustiveTargetingVariants, TargetingComparators.MULTI_ABI_COMPARATOR)) .isTrue(); }
/** * Returns a comparator which tries four comparators in order until a non-zero * result is found, returning that result, and returning zero only if all * comparators return zero. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @param third the third comparator to invoke * @param forth the fourth comparator to invoke * @see #compound(Iterable) */ @SuppressWarnings("unchecked") public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third, Comparator<? super T> forth) { Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; Comparator<T> thirdT = (Comparator<T>) third; Comparator<T> forthT = (Comparator<T>) forth; return compound(Arrays.asList(firstT, secondT, thirdT, forthT)); }
@Override public final int compareTo(Path other) { return Comparators.lexicographical(Comparator.<String>naturalOrder()) .compare(getNames(), ((ZipPath) other).getNames()); }
public void testLeastCollector() { CollectorTester.of(Comparators.<Integer>least(2, Comparator.naturalOrder())) .expectCollects(Arrays.asList(1, 2), 1, 2, 3, 4, 5, 6) .expectCollects(Arrays.asList(1), 1) .expectCollects(Collections.emptyList()); }
@Test public void testMultiAbiAliasComparator_realisticTargetingList() { assertThat( Comparators.isInStrictOrder( REALISTIC_TARGETING_LIST, TargetingComparators.MULTI_ABI_ALIAS_COMPARATOR)) .isTrue(); }
/** * Returns a comparator which tries each given comparator in order until a * non-zero result is found, returning that result, and returning zero only if * all comparators return zero. * * <p>Subsequent changes to the {@code rest} array do not affect the behavior * of the returned comparator. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @param third the third comparator to invoke * @param forth the fourth comparator to invoke * @param rest additional comparators to invoke as necessary * @see #compound(Iterable) */ @SuppressWarnings("unchecked") // TODO: check that this is right public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third, Comparator<? super T> forth, Comparator<? super T>... rest) { // TODO: is this really the best way? if so, explain why. Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; Comparator<T> thirdT = (Comparator<T>) third; Comparator<T> forthT = (Comparator<T>) forth; List<Comparator<T>> list = Lists.newArrayList( firstT, secondT, thirdT, forthT); list.addAll(Arrays.asList((Comparator<T>[]) rest)); return compound(list); }
public static <C extends Comparable<? super C>> int compareIterable( Iterable<C> lhs, Iterable<C> rhs) { return Comparators.lexicographical(Ordering.<C>natural()).compare(lhs, rhs); }
/** * Returns a {@code Collector} that returns the {@code k} greatest (relative to the specified * {@code Comparator}) input elements, in descending order, as an unmodifiable {@code List}. Ties * are broken arbitrarily. * * <p>For example: * * <pre>{@code * Stream.of("foo", "quux", "banana", "elephant") * .collect(greatest(2, comparingInt(String::length))) * // returns {"elephant", "banana"} * }</pre> * * <p>This {@code Collector} uses O(k) memory and takes expected time O(n) (worst-case O(n log * k)), as opposed to e.g. {@code Stream.sorted(comparator.reversed()).limit(k)}, which currently * takes O(n log n) time and O(n) space. * * @throws IllegalArgumentException if {@code k < 0} * @since 22.0 */ public static <T> Collector<T, ?, List<T>> greatest(int k, Comparator<? super T> comparator) { return least(k, comparator.reversed()); }
@Test public void testMultiAbiAliasComparator_exhaustiveTargetingList() { assertThat( Comparators.isInStrictOrder( EXHAUSTIVE_TARGETING_LIST, TargetingComparators.MULTI_ABI_ALIAS_COMPARATOR)) .isTrue(); }
@Override public int compareTo(AsPath rhs) { return Comparators.lexicographical(Ordering.<AsSet>natural()).compare(_asSets, rhs._asSets); }