private void readPartitionFile(FileSystem fs, Configuration conf, Path path) throws IOException { @SuppressWarnings("deprecation") SequenceFile.Reader reader = new SequenceFile.Reader(fs, path, conf); ImmutableBytesWritable key = new ImmutableBytesWritable(); partitions = new ArrayList<>(); while (reader.next(key)) { partitions.add(new ImmutableBytesWritable(key.copyBytes())); } reader.close(); if (!Ordering.natural().isOrdered(partitions)) { throw new IOException("Partitions are not ordered!"); } }
public synchronized ImmutableList<RegionInfo> getRegionsFromMeta() { // lazy loaded, synchronized to ensure a single load if (regionsFromMeta == null) { List<RegionInfo> regions = new ArrayList<>(); for (HbckInfo h : HBaseFsck.this.regionInfoMap.values()) { if (tableName.equals(h.getTableName())) { if (h.metaEntry != null) { regions.add(h.metaEntry); } } } regionsFromMeta = Ordering.from(RegionInfo.COMPARATOR).immutableSortedCopy(regions); } return regionsFromMeta; }
@Override public Comparator<? super Cut<C>> comparator() { return Ordering.<Cut<C>>natural(); }
/** * 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 ContiguousSet) { return ((ContiguousSet<C>) values).range(); } 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); }
/** * 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 map. * * @throws IllegalArgumentException if duplicate keys were added */ public ImmutableMap<K, V> build() { /* * If entries is full, then this implementation may end up using the entries array * directly and writing over the entry objects with non-terminal entries, but this is * safe; if this Builder is used further, it will grow the entries array (so it can't * affect the original array), and future build() calls will always copy any entry * objects that cannot be safely reused. */ if (valueComparator != null) { if (entriesUsed) { entries = Arrays.copyOf(entries, size); } Arrays.sort( entries, 0, size, Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction())); } entriesUsed = size == entries.length; return RegularImmutableMap.fromEntryArray(size, entries); } }
@Override public int compare(T a, T b) { return forwardOrder.compare(b, a); }
/** * 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); }
@SuppressWarnings("unchecked") // safe "contravariant cast" private <T extends B> Ordering<T> ordering() { return Ordering.from((Comparator<T>) comparator); } }
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()); }
@Override public <E extends T> E min(Iterable<E> iterable) { return forwardOrder.max(iterable); }
@Override public <E extends T> E max(Iterator<E> iterator) { return forwardOrder.min(iterator); }
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)); }
@Override public <S extends Comparable> Ordering<S> reverse() { return Ordering.natural(); }
/** * 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()); }