001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4;
018
019import java.util.Collection;
020import java.util.Comparator;
021
022import org.apache.commons.collections4.comparators.BooleanComparator;
023import org.apache.commons.collections4.comparators.ComparableComparator;
024import org.apache.commons.collections4.comparators.ComparatorChain;
025import org.apache.commons.collections4.comparators.NullComparator;
026import org.apache.commons.collections4.comparators.ReverseComparator;
027import org.apache.commons.collections4.comparators.TransformingComparator;
028
029/**
030 * Provides convenient static utility methods for <Code>Comparator</Code>
031 * objects.
032 * <p>
033 * Most of the functionality in this class can also be found in the
034 * <code>comparators</code> package. This class merely provides a
035 * convenient central place if you have use for more than one class
036 * in the <code>comparators</code> subpackage.
037 *
038 * @since 2.1
039 * @version $Id: ComparatorUtils.html 972421 2015-11-14 20:00:04Z tn $
040 */
041public class ComparatorUtils {
042
043    /**
044     * ComparatorUtils should not normally be instantiated.
045     */
046    private ComparatorUtils() {}
047
048    /**
049     * Comparator for natural sort order.
050     *
051     * @see ComparableComparator#comparableComparator()
052     */
053    @SuppressWarnings({ "rawtypes", "unchecked" }) // explicit type needed for Java 1.5 compilation
054    public static final Comparator NATURAL_COMPARATOR = ComparableComparator.<Comparable>comparableComparator();
055
056    /**
057     * Gets a comparator that uses the natural order of the objects.
058     *
059     * @param <E>  the object type to compare
060     * @return  a comparator which uses natural order
061     */
062    @SuppressWarnings("unchecked")
063    public static <E extends Comparable<? super E>> Comparator<E> naturalComparator() {
064        return NATURAL_COMPARATOR;
065    }
066
067    /**
068     * Gets a comparator that compares using an array of {@link Comparator}s, applied
069     * in sequence until one returns not equal or the array is exhausted.
070     *
071     * @param <E>  the object type to compare
072     * @param comparators  the comparators to use, not null or empty or containing nulls
073     * @return a {@link ComparatorChain} formed from the input comparators
074     * @throws NullPointerException if comparators array is null or contains a null
075     * @see ComparatorChain
076     */
077    public static <E extends Comparable<? super E>> Comparator<E> chainedComparator(
078            final Comparator<E>... comparators) {
079
080        final ComparatorChain<E> chain = new ComparatorChain<E>();
081        for (final Comparator<E> comparator : comparators) {
082            if (comparator == null) {
083                throw new NullPointerException("Comparator cannot be null");
084            }
085            chain.addComparator(comparator);
086        }
087        return chain;
088    }
089
090    /**
091     * Gets a comparator that compares using a collection of {@link Comparator}s,
092     * applied in (default iterator) sequence until one returns not equal or the
093     * collection is exhausted.
094     *
095     * @param <E>  the object type to compare
096     * @param comparators  the comparators to use, not null or empty or containing nulls
097     * @return a {@link ComparatorChain} formed from the input comparators
098     * @throws NullPointerException if comparators collection is null or contains a null
099     * @throws ClassCastException if the comparators collection contains the wrong object type
100     * @see ComparatorChain
101     */
102    @SuppressWarnings("unchecked")
103    public static <E extends Comparable<? super E>> Comparator<E> chainedComparator(
104            final Collection<Comparator<E>> comparators) {
105
106        return chainedComparator(
107            (Comparator<E>[]) comparators.toArray(new Comparator[comparators.size()])
108        );
109    }
110
111    /**
112     * Gets a comparator that reverses the order of the given comparator.
113     *
114     * @param <E>  the object type to compare
115     * @param comparator  the comparator to reverse
116     * @return  a comparator that reverses the order of the input comparator
117     * @see ReverseComparator
118     */
119    public static <E> Comparator<E> reversedComparator(final Comparator<E> comparator) {
120        return new ReverseComparator<E>(comparator);
121    }
122
123    /**
124     * Gets a Comparator that can sort Boolean objects.
125     * <p>
126     * The parameter specifies whether true or false is sorted first.
127     * <p>
128     * The comparator throws NullPointerException if a null value is compared.
129     *
130     * @param trueFirst  when <code>true</code>, sort
131     *        <code>true</code> {@link Boolean}s before
132     *        <code>false</code> {@link Boolean}s.
133     * @return  a comparator that sorts booleans
134     */
135    public static Comparator<Boolean> booleanComparator(final boolean trueFirst) {
136        return BooleanComparator.booleanComparator(trueFirst);
137    }
138
139    /**
140     * Gets a Comparator that controls the comparison of <code>null</code> values.
141     * <p>
142     * The returned comparator will consider a null value to be less than
143     * any nonnull value, and equal to any other null value.  Two nonnull
144     * values will be evaluated with the given comparator.
145     *
146     * @param <E>  the object type to compare
147     * @param comparator the comparator that wants to allow nulls
148     * @return  a version of that comparator that allows nulls
149     * @see NullComparator
150     */
151    @SuppressWarnings("unchecked")
152    public static <E> Comparator<E> nullLowComparator(Comparator<E> comparator) {
153        if (comparator == null) {
154            comparator = NATURAL_COMPARATOR;
155        }
156        return new NullComparator<E>(comparator, false);
157    }
158
159    /**
160     * Gets a Comparator that controls the comparison of <code>null</code> values.
161     * <p>
162     * The returned comparator will consider a null value to be greater than
163     * any nonnull value, and equal to any other null value.  Two nonnull
164     * values will be evaluated with the given comparator.
165     *
166     * @param <E>  the object type to compare
167     * @param comparator the comparator that wants to allow nulls
168     * @return  a version of that comparator that allows nulls
169     * @see NullComparator
170     */
171    @SuppressWarnings("unchecked")
172    public static <E> Comparator<E> nullHighComparator(Comparator<E> comparator) {
173        if (comparator == null) {
174            comparator = NATURAL_COMPARATOR;
175        }
176        return new NullComparator<E>(comparator, true);
177    }
178
179    /**
180     * Gets a Comparator that passes transformed objects to the given comparator.
181     * <p>
182     * Objects passed to the returned comparator will first be transformed
183     * by the given transformer before they are compared by the given
184     * comparator.
185     *
186     * @param <I>  the input object type of the transformed comparator
187     * @param <O>  the object type of the decorated comparator
188     * @param comparator  the sort order to use
189     * @param transformer  the transformer to use
190     * @return  a comparator that transforms its input objects before comparing them
191     * @see  TransformingComparator
192     */
193    @SuppressWarnings("unchecked")
194    public static <I, O> Comparator<I> transformedComparator(Comparator<O> comparator,
195            final Transformer<? super I, ? extends O> transformer) {
196
197        if (comparator == null) {
198            comparator = NATURAL_COMPARATOR;
199        }
200        return new TransformingComparator<I, O>(transformer, comparator);
201    }
202
203    /**
204     * Returns the smaller of the given objects according to the given
205     * comparator, returning the second object if the comparator
206     * returns equal.
207     *
208     * @param <E>  the object type to compare
209     * @param o1  the first object to compare
210     * @param o2  the second object to compare
211     * @param comparator  the sort order to use
212     * @return  the smaller of the two objects
213     */
214    @SuppressWarnings("unchecked")
215    public static <E> E min(final E o1, final E o2, Comparator<E> comparator) {
216        if (comparator == null) {
217            comparator = NATURAL_COMPARATOR;
218        }
219        final int c = comparator.compare(o1, o2);
220        return c < 0 ? o1 : o2;
221    }
222
223    /**
224     * Returns the larger of the given objects according to the given
225     * comparator, returning the second object if the comparator
226     * returns equal.
227     *
228     * @param <E>  the object type to compare
229     * @param o1  the first object to compare
230     * @param o2  the second object to compare
231     * @param comparator  the sort order to use
232     * @return  the larger of the two objects
233     */
234    @SuppressWarnings("unchecked")
235    public static <E> E max(final E o1, final E o2, Comparator<E> comparator) {
236        if (comparator == null) {
237            comparator = NATURAL_COMPARATOR;
238        }
239        final int c = comparator.compare(o1, o2);
240        return c > 0 ? o1 : o2;
241    }
242
243}