@SuppressWarnings("unchecked") // safe "contravariant cast" private <T extends B> Ordering<T> ordering() { return Ordering.from((Comparator<T>) comparator); } }
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 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); }
DescendingImmutableSortedSet(ImmutableSortedSet<E> forward) { super(Ordering.from(forward.comparator()).reverse()); this.forward = forward; }
/** * Returns an {@code ImmutableList} containing all of the elements from this {@code * FluentIterable} in the order specified by {@code comparator}. To produce an {@code * ImmutableList} sorted by its natural ordering, use {@code toSortedList(Ordering.natural())}. * * <p><b>{@code Stream} equivalent:</b> pass {@link ImmutableList#toImmutableList} to {@code * stream.sorted(comparator).collect()}. * * @param comparator the function by which to sort list elements * @throws NullPointerException if any element of this iterable is {@code null} * @since 14.0 (since 13.0 as {@code toSortedImmutableList()}). */ public final ImmutableList<E> toSortedList(Comparator<? super E> comparator) { return Ordering.from(comparator).immutableSortedCopy(getDelegate()); }
@Override public Comparator<? super E> comparator() { Comparator<? super E> result = comparator; if (result == null) { return comparator = Ordering.from(forwardMultiset().comparator()).<E>reverse(); } return result; }
0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
/** * Returns a newly-created immutable set multimap. */ @Override public ImmutableSetMultimap<K, V> build() { if (keyComparator != null) { Multimap<K, V> sortedCopy = MultimapBuilder.linkedHashKeys().linkedHashSetValues().<K, V>build(); List<Map.Entry<K, Collection<V>>> entries = Ordering.from(keyComparator) .<K>onKeys() .immutableSortedCopy(builderMultimap.asMap().entrySet()); for (Map.Entry<K, Collection<V>> entry : entries) { sortedCopy.putAll(entry.getKey(), entry.getValue()); } builderMultimap = sortedCopy; } return copyOf(builderMultimap, valueComparator); } }
/** * Returns a newly-created immutable multimap. */ public ImmutableMultimap<K, V> build() { if (valueComparator != null) { for (Collection<V> values : builderMultimap.asMap().values()) { List<V> list = (List<V>) values; Collections.sort(list, valueComparator); } } if (keyComparator != null) { Multimap<K, V> sortedCopy = MultimapBuilder.linkedHashKeys().arrayListValues().<K, V>build(); List<Map.Entry<K, Collection<V>>> entries = Ordering.from(keyComparator) .<K>onKeys() .immutableSortedCopy(builderMultimap.asMap().entrySet()); for (Map.Entry<K, Collection<V>> entry : entries) { sortedCopy.putAll(entry.getKey(), entry.getValue()); } builderMultimap = sortedCopy; } return copyOf(builderMultimap); } }
@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; }
/** * 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; }