/** * Gets a comparator that compares using a collection of {@link Comparator}s, * applied in (default iterator) sequence until one returns not equal or the * collection is exhausted. * * @param <E> the object type to compare * @param comparators the comparators to use, not null or empty or containing nulls * @return a {@link ComparatorChain} formed from the input comparators * @throws NullPointerException if comparators collection is null or contains a null * @throws ClassCastException if the comparators collection contains the wrong object type * @see ComparatorChain */ @SuppressWarnings("unchecked") public static <E> Comparator<E> chainedComparator(final Collection<Comparator<E>> comparators) { return chainedComparator( (Comparator<E>[]) comparators.toArray(new Comparator[comparators.size()]) ); }
/** * Merges two sorted Collections, a and b, into a single, sorted List * such that the natural ordering of the elements is retained. * <p> * Uses the standard O(n) merge algorithm for combining two sorted lists. * * @param <O> the element type * @param a the first collection, must not be null * @param b the second collection, must not be null * @return a new sorted List, containing the elements of Collection a and b * @throws NullPointerException if either collection is null * @since 4.0 */ public static <O extends Comparable<? super O>> List<O> collate(Iterable<? extends O> a, Iterable<? extends O> b) { return collate(a, b, ComparatorUtils.<O>naturalComparator(), true); }
/** * Merges two sorted Collections, a and b, into a single, sorted List * such that the natural ordering of the elements is retained. * <p> * Uses the standard O(n) merge algorithm for combining two sorted lists. * * @param <O> the element type * @param a the first collection, must not be null * @param b the second collection, must not be null * @param includeDuplicates if {@code true} duplicate elements will be retained, otherwise * they will be removed in the output collection * @return a new sorted List, containing the elements of Collection a and b * @throws NullPointerException if either collection is null * @since 4.0 */ public static <O extends Comparable<? super O>> List<O> collate(final Iterable<? extends O> a, final Iterable<? extends O> b, final boolean includeDuplicates) { return collate(a, b, ComparatorUtils.<O>naturalComparator(), includeDuplicates); }