/** * Returns a comparator which tries two comparators in order until a non-zero * result is found, returning that result, and returning zero only if both * comparators return zero. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @see #compound(Iterable) */ @SuppressWarnings("unchecked") public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second) { Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; return compound(Arrays.asList(firstT, secondT)); }
/** * Returns a comparator which tries three comparators in order until a * non-zero result is found, returning that result, and returning zero only if * all comparators return zero. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @param third the third comparator to invoke * @see #compound(Iterable) */ @SuppressWarnings("unchecked") public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third) { Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; Comparator<T> thirdT = (Comparator<T>) third; return compound(Arrays.asList(firstT, secondT, thirdT)); }
/** * Returns a comparator which tries four comparators in order until a non-zero * result is found, returning that result, and returning zero only if all * comparators return zero. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @param third the third comparator to invoke * @param forth the fourth comparator to invoke * @see #compound(Iterable) */ @SuppressWarnings("unchecked") public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third, Comparator<? super T> forth) { Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; Comparator<T> thirdT = (Comparator<T>) third; Comparator<T> forthT = (Comparator<T>) forth; return compound(Arrays.asList(firstT, secondT, thirdT, forthT)); }
/** * Returns a comparator which tries each given comparator in order until a * non-zero result is found, returning that result, and returning zero only if * all comparators return zero. * * <p>Subsequent changes to the {@code rest} array do not affect the behavior * of the returned comparator. * * @param first the first comparator to invoke * @param second the second comparator to invoke * @param third the third comparator to invoke * @param forth the fourth comparator to invoke * @param rest additional comparators to invoke as necessary * @see #compound(Iterable) */ @SuppressWarnings("unchecked") // TODO: check that this is right public static <T> Ordering<T> compound(Comparator<? super T> first, Comparator<? super T> second, Comparator<? super T> third, Comparator<? super T> forth, Comparator<? super T>... rest) { // TODO: is this really the best way? if so, explain why. Comparator<T> firstT = (Comparator<T>) first; Comparator<T> secondT = (Comparator<T>) second; Comparator<T> thirdT = (Comparator<T>) third; Comparator<T> forthT = (Comparator<T>) forth; List<Comparator<T>> list = Lists.newArrayList( firstT, secondT, thirdT, forthT); list.addAll(Arrays.asList((Comparator<T>[]) rest)); return compound(list); }