--- /dev/null 2013-02-05 15:07:27.428842802 -0800 +++ new/src/share/classes/java/util/Comparators.java 2013-02-26 13:07:24.175275624 -0800 @@ -0,0 +1,279 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package java.util; + +import java.io.Serializable; +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.ToDoubleFunction; +import java.util.function.ToIntFunction; +import java.util.function.ToLongFunction; + +/** + * This class consists of {@code static} utility methods for comparators. Mostly + * factory method that returns a {@link Comparator}. + * + *

Unless otherwise noted, passing a {@code null} argument to a method in + * this class will cause a {@link NullPointerException} to be thrown. + * + * @see Comparator + * @since 1.8 + */ +public class Comparators { + private Comparators() { + throw new AssertionError("no instances"); + } + + /** + * Compares {@link Comparable} objects in natural order. + * + * @see Comparable + */ + private enum NaturalOrderComparator implements Comparator> { + INSTANCE; + + @Override + public int compare(Comparable c1, Comparable c2) { + return c1.compareTo(c2); + } + } + + /** + * Returns a comparator that imposes the reverse of the natural + * ordering. + * + *

The returned comparator is serializable. + * + * @param {@link Comparable} type + * + * @return A comparator that imposes the reverse of the natural + * ordering on a collection of objects that implement + * the {@link Comparable} interface. + * @see Comparable + */ + public static > Comparator reverseOrder() { + return Collections.reverseOrder(); + } + + /** + * Returns a comparator that imposes the reverse ordering of the specified + * {@link Comparator}. + * + *

The returned comparator is serializable (assuming the specified + * comparator is also serializable). + * + * @param the element type to be compared + * @param cmp a comparator whose ordering is to be reversed by the returned + * comparator + * @return A comparator that imposes the reverse ordering of the + * specified comparator. + */ + public static Comparator reverseOrder(Comparator cmp) { + Objects.requireNonNull(cmp); + return Collections.reverseOrder(cmp); + } + + /** + * Gets a comparator compares {@link Comparable} type in natural order. + * + * @param {@link Comparable} type + */ + public static > Comparator naturalOrder() { + return (Comparator) NaturalOrderComparator.INSTANCE; + } + + /** + * Gets a comparator compares {@link Map.Entry} in natural order on key. + * + * @param {@link Comparable} key type + * @param value type + */ + public static , V> Comparator> naturalOrderKeys() { + return (Comparator> & Serializable) + (c1, c2) -> c1.getKey().compareTo(c2.getKey()); + } + + /** + * Gets a comparator compares {@link Map.Entry} in natural order on value. + * + * @param key type + * @param {@link Comparable} value type + */ + public static > Comparator> naturalOrderValues() { + return (Comparator> & Serializable) + (c1, c2) -> c1.getValue().compareTo(c2.getValue()); + } + + /** + * Gets a comparator compares {@link Map.Entry} by key using the given + * {@link Comparator}. + * + *

The returned comparator is serializable assuming the specified + * comparators are also serializable. + * + * @param key type + * @param value type + * @param cmp the key {@link Comparator} + */ + public static Comparator> byKey(Comparator cmp) { + Objects.requireNonNull(cmp); + return (Comparator> & Serializable) + (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey()); + } + + /** + * Gets a comparator compares {@link Map.Entry} by value using the given + * {@link Comparator}. + * + * @param key type + * @param value type + * @param cmp the value {@link Comparator} + */ + public static Comparator> byValue(Comparator cmp) { + Objects.requireNonNull(cmp); + return (Comparator> & Serializable) + (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue()); + } + + /** + * Accepts a function that extracts a {@link java.lang.Comparable + * Comparable} sort key from a type {@code T}, and returns a {@code + * Comparator} that compares by that sort key. For example, if a class + * {@code Person} has a {@code String}-valued getter {@code getLastName}, + * then {@code comparing(Person::getLastName)} would return a {@code + * Comparator} that compares {@code Person} objects by their last + * name. + * + * @param the original element type + * @param the {@link Comparable} type for comparison + * @param keyExtractor the function used to extract the {@link Comparable} sort key + */ + public static > Comparator comparing(Function keyExtractor) { + Objects.requireNonNull(keyExtractor); + return (Comparator & Serializable) + (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); + } + + /** + * Accepts a function that extracts an {@code int} value from a type {@code + * T}, and returns a {@code Comparator} that compares by that value. + * + *

The returned comparator is serializable assuming the specified + * function is also serializable. + * + * @see #comparing(Function) + * @param the original element type + * @param keyExtractor the function used to extract the integer value + */ + public static Comparator comparing(ToIntFunction keyExtractor) { + Objects.requireNonNull(keyExtractor); + return (Comparator & Serializable) + (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); + } + + /** + * Accepts a function that extracts a {@code long} value from a type {@code + * T}, and returns a {@code Comparator} that compares by that value. + * + *

The returned comparator is serializable assuming the specified + * function is also serializable. + * + * @see #comparing(Function) + * @param the original element type + * @param keyExtractor the function used to extract the long value + */ + public static Comparator comparing(ToLongFunction keyExtractor) { + Objects.requireNonNull(keyExtractor); + return (Comparator & Serializable) + (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); + } + + /** + * Accepts a function that extracts a {@code double} value from a type + * {@code T}, and returns a {@code Comparator} that compares by that + * value. + * + *

The returned comparator is serializable assuming the specified + * function is also serializable. + * + * @see #comparing(Function) + * @param the original element type + * @param keyExtractor the function used to extract the double value + */ + public static Comparator comparing(ToDoubleFunction keyExtractor) { + Objects.requireNonNull(keyExtractor); + return (Comparator & Serializable) + (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); + } + + /** + * Constructs a lexicographic order from two {@link Comparator}s. For + * example, if you have comparators {@code byLastName} and {@code + * byFirstName}, each of type {@code Comparator}, then {@code + * compose(byLastName, byFirstName)} creates a {@code Comparator} + * which sorts by last name, and for equal last names sorts by first name. + * + *

The returned comparator is serializable assuming the specified + * comparators are also serializable. + * + * @param the element type to be compared + * @param first the first comparator + * @param second the secondary comparator used when equals on the first + */ + public static Comparator compose(Comparator first, Comparator second) { + Objects.requireNonNull(first); + Objects.requireNonNull(second); + return (Comparator & Serializable) (c1, c2) -> { + int res = first.compare(c1, c2); + return (res != 0) ? res : second.compare(c1, c2); + }; + } + + /** + * Constructs a {@link BinaryOperator} which returns the lesser of two elements + * according to the specified {@code Comparator} + * + * @param comparator A {@code Comparator} for comparing the two values + * @param the type of the elements to be compared + * @return a {@code BinaryOperator} which returns the lesser of its operands, + * according to the supplied {@code Comparator} + */ + public static BinaryOperator lesserOf(Comparator comparator) { + return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; + } + + /** + * Constructs a {@link BinaryOperator} which returns the greater of two elements + * according to the specified {@code Comparator} + * + * @param comparator A {@code Comparator} for comparing the two values + * @param the type of the elements to be compared + * @return a {@code BinaryOperator} which returns the greater of its operands, + * according to the supplied {@code Comparator} + */ + public static BinaryOperator greaterOf(Comparator comparator) { + return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; + } +}