/** * <p> * Groups input splits by file path. Each inout split group is ordered by starting bytes * to ensure file parts in correct order. * </p> * <p> * Example: * <pre> * hdfs:///tmp/table/file_1.txt -> hdfs:///tmp/table/file_1.txt:0+10000 * hdfs:///tmp/table/file_1.txt:10001+20000 * hdfs:///tmp/table/file_2.txt -> hdfs:///tmp/table/file_2.txt:0+10000 * </pre> * </p> * @param inputSplits input splits * @return multimap where key is file path and value is group of ordered file splits */ private Multimap<Path, FileSplit> transformFileSplits(InputSplit[] inputSplits) { Multimap<Path, FileSplit> inputSplitGroups = TreeMultimap.create( Ordering.natural(), Comparator.comparingLong(FileSplit::getStart)); for (InputSplit inputSplit : inputSplits) { FileSplit fileSplit = (FileSplit) inputSplit; inputSplitGroups.put(fileSplit.getPath(), fileSplit); } return inputSplitGroups; }
@Override public int getValueCapacity() { if (size() == 0) { return 0; } final Ordering<ValueVector> natural = new Ordering<ValueVector>() { @Override public int compare(@Nullable ValueVector left, @Nullable ValueVector right) { return Ints.compare( Preconditions.checkNotNull(left).getValueCapacity(), Preconditions.checkNotNull(right).getValueCapacity() ); } }; return natural.min(getChildren()).getValueCapacity(); }
/** * Returns the minimal range that * {@linkplain Range#contains(Comparable) contains} all of the given values. * The returned range is {@linkplain BoundType#CLOSED closed} on both ends. * * @throws ClassCastException if the parameters are not <i>mutually * comparable</i> * @throws NoSuchElementException if {@code values} is empty * @throws NullPointerException if any of {@code values} is null * @since 14.0 */ public static <C extends Comparable<?>> Range<C> encloseAll(Iterable<C> values) { checkNotNull(values); if (values instanceof SortedSet) { SortedSet<? extends C> set = cast(values); Comparator<?> comparator = set.comparator(); if (Ordering.natural().equals(comparator) || comparator == null) { return closed(set.first(), set.last()); } } Iterator<C> valueIterator = values.iterator(); C min = checkNotNull(valueIterator.next()); C max = min; while (valueIterator.hasNext()) { C value = checkNotNull(valueIterator.next()); min = Ordering.natural().min(min, value); max = Ordering.natural().max(max, value); } return closed(min, max); }
@Override public int compare(T a, T b) { return forwardOrder.compare(b, a); }
/** * 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 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()); }
private static <K, V> ImmutableList<K> sortKeysByValue( final Map<K, V> map, final Comparator<? super V> valueComparator) { Ordering<K> keyOrdering = new Ordering<K>() { @Override public int compare(K left, K right) { return valueComparator.compare(map.get(left), map.get(right)); } }; return keyOrdering.immutableSortedCopy(map.keySet()); }
/** * 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); }
0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
@Override public <S extends T> Ordering<S> reverse() { // ordering.reverse() might be optimized, so let it do its thing return ordering.reverse().nullsFirst(); }
@Override public <E extends T> E min(E a, E b, E c, E... rest) { return forwardOrder.max(a, b, c, rest); }
@Override public <S extends Comparable> Ordering<S> nullsFirst() { Ordering<Comparable> result = nullsFirst; if (result == null) { result = nullsFirst = super.nullsFirst(); } return (Ordering<S>) result; }
@SuppressWarnings("unchecked") // safe "contravariant cast" private <T extends B> Ordering<T> ordering() { return Ordering.from((Comparator<T>) comparator); } }
return leastOf(iterable.iterator(), k);
/** * Returns an ordering that compares objects according to the order in which they are given to * this method. Only objects present in the argument list (according to {@link Object#equals}) may * be compared. This comparator imposes a "partial ordering" over the type {@code T}. Null values * in the argument list are not supported. * * <p>The returned comparator throws a {@link ClassCastException} when it receives an input * parameter that isn't among the provided values. * * <p>The generated comparator is serializable if all the provided values are serializable. * * @param leastValue the value which the returned comparator should consider the "least" of all * values * @param remainingValuesInOrder the rest of the values that the returned comparator will be able * to compare, in the order the comparator should follow * @return the comparator described above * @throws NullPointerException if any of the provided values is null * @throws IllegalArgumentException if any duplicate values (according to {@link * Object#equals(Object)}) are present among the method arguments */ // TODO(kevinb): provide replacement @GwtCompatible(serializable = true) public static <T> Ordering<T> explicit(T leastValue, T... remainingValuesInOrder) { return explicit(Lists.asList(leastValue, remainingValuesInOrder)); }
/** * 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()); }