@Override public <S extends T> Ordering<S> reverse() { // ordering.reverse() might be optimized, so let it do its thing return ordering.reverse().nullsLast(); }
@Override public <S extends T> Ordering<S> reverse() { // ordering.reverse() might be optimized, so let it do its thing return ordering.reverse().nullsFirst(); }
private static <T> Ordering<T> reverse(Comparator<T> forward) { return Ordering.from(forward).reverse(); }
private static <T> Ordering<T> reverse(Comparator<T> forward) { return Ordering.from(forward).reverse(); }
/** * Returns the {@code k} greatest elements of the given iterable according to this ordering, in * order from greatest to least. If there are fewer than {@code k} elements present, all will be * included. * * <p>The implementation does not necessarily use a <i>stable</i> sorting algorithm; when multiple * elements are equivalent, it is undefined which will come first. * * <p><b>Java 8 users:</b> Use {@code Streams.stream(iterable).collect(Comparators.greatest(k, * thisComparator))} instead. * * @return an immutable {@code RandomAccess} list of the {@code k} greatest elements in * <i>descending order</i> * @throws IllegalArgumentException if {@code k} is negative * @since 8.0 */ public <E extends T> List<E> greatestOf(Iterable<E> iterable, int k) { // TODO(kevinb): see if delegation is hurting performance noticeably // TODO(kevinb): if we change this implementation, add full unit tests. return reverse().leastOf(iterable, k); }
/** * Returns the {@code k} greatest elements from the given iterator according to this ordering, in * order from greatest to least. If there are fewer than {@code k} elements present, all will be * included. * * <p>The implementation does not necessarily use a <i>stable</i> sorting algorithm; when multiple * elements are equivalent, it is undefined which will come first. * * <p><b>Java 8 users:</b> Continue to use this method for now. After the next release of Guava, * use {@code Streams.stream(iterator).collect(Comparators.greatest(k, thisComparator))} instead. * * @return an immutable {@code RandomAccess} list of the {@code k} greatest elements in * <i>descending order</i> * @throws IllegalArgumentException if {@code k} is negative * @since 14.0 */ public <E extends T> List<E> greatestOf(Iterator<E> iterator, int k) { return reverse().leastOf(iterator, k); }
/** * Returns a {@code TopKSelector} that collects the greatest {@code k} elements added to it, * relative to the specified comparator, and returns them via {@link #topK} in descending order. * * @throws IllegalArgumentException if {@code k < 0} */ public static <T> TopKSelector<T> greatest(int k, Comparator<? super T> comparator) { return new TopKSelector<T>(Ordering.from(comparator).reverse(), k); }
/** * Returns a builder that creates immutable sorted maps whose keys are * ordered by the reverse of their natural ordering. */ public static <K extends Comparable<?>, V> Builder<K, V> reverseOrder() { return new Builder<K, V>(Ordering.natural().reverse()); }
private MinMaxPriorityQueue(Builder<? super E> builder, int queueSize) { Ordering<E> ordering = builder.ordering(); this.minHeap = new Heap(ordering); this.maxHeap = new Heap(ordering.reverse()); minHeap.otherHeap = maxHeap; maxHeap.otherHeap = minHeap; this.maximumSize = builder.maximumSize; // TODO(kevinb): pad? this.queue = new Object[queueSize]; }
/** * Returns a builder that creates immutable sorted multisets whose elements are ordered by the * reverse of their natural ordering. * * <p>Note: the type parameter {@code E} extends {@code Comparable<?>} rather than {@code * Comparable<? super E>} as a workaround for javac <a * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468354">bug 6468354</a>. */ public static <E extends Comparable<?>> Builder<E> reverseOrder() { return new Builder<E>(Ordering.natural().reverse()); }
DescendingImmutableSortedSet(ImmutableSortedSet<E> forward) { super(Ordering.from(forward.comparator()).reverse()); this.forward = forward; }
@Override public Comparator<? super E> comparator() { Comparator<? super E> result = comparator; if (result == null) { return comparator = Ordering.from(forwardMultiset().comparator()).<E>reverse(); } return result; }
@SuppressWarnings("unchecked") @Override public Comparator<? super E> comparator() { Comparator<? super E> forwardComparator = forward.comparator(); if (forwardComparator == null) { return (Comparator) Ordering.natural().reverse(); } else { return reverse(forwardComparator); } }
@GwtIncompatible // NavigableSet ImmutableSortedSet<C> createDescendingSet() { return ImmutableSortedSet.emptySet(Ordering.natural().reverse()); } }
ImmutableList<K> collectTypes(Iterable<? extends K> types) { // type -> order number. 1 for Object, 2 for anything directly below, so on so forth. Map<K, Integer> map = Maps.newHashMap(); for (K type : types) { collectTypes(type, map); } return sortKeysByValue(map, Ordering.natural().reverse()); }
@Override public ImmutableSet<Range<C>> asDescendingSetOfRanges() { if (ranges.isEmpty()) { return ImmutableSet.of(); } return new RegularImmutableSortedSet<Range<C>>( ranges.reverse(), Range.<C>rangeLexOrdering().reverse()); }
@Override public ImmutableSortedMultiset<E> descendingMultiset() { ImmutableSortedMultiset<E> result = descendingMultiset; if (result == null) { return descendingMultiset = this.isEmpty() ? emptyMultiset(Ordering.from(comparator()).reverse()) : new DescendingImmutableSortedMultiset<E>(this); } return result; }
@Override public ImmutableMap<Range<K>, V> asDescendingMapOfRanges() { if (ranges.isEmpty()) { return ImmutableMap.of(); } RegularImmutableSortedSet<Range<K>> rangeSet = new RegularImmutableSortedSet<Range<K>>( ranges.reverse(), Range.<K>rangeLexOrdering().reverse()); return new ImmutableSortedMap<Range<K>, V>(rangeSet, values.reverse()); }
/** * Returns the same range relative to the reversed comparator. */ GeneralRange<T> reverse() { GeneralRange<T> result = reverse; if (result == null) { result = new GeneralRange<T>( Ordering.from(comparator).reverse(), hasUpperBound, getUpperEndpoint(), getUpperBoundType(), hasLowerBound, getLowerEndpoint(), getLowerBoundType()); result.reverse = this; return this.reverse = result; } return result; }
@Override public ImmutableSortedMap<K, V> descendingMap() { // TODO(kevinb): the descendingMap is never actually cached at all. Either it should be or the // code below simplified. ImmutableSortedMap<K, V> result = descendingMap; if (result == null) { if (isEmpty()) { return result = emptyMap(Ordering.from(comparator()).reverse()); } else { return result = new ImmutableSortedMap<K, V>( (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this); } } return result; }