/** * Invert the sort order of each sort definition contained by this compound * comparator. */ public void invertOrder() { for (InvertibleComparator comparator : this.comparators) { comparator.invertOrder(); } }
/** * Create a new {@link ConvertingComparator} instance. * @param converter the converter */ public ConvertingComparator(Converter<S, T> converter) { this(Comparators.comparable(), converter); }
/** * Replace the Comparator at the given index using the given sort order. * @param index the index of the Comparator to replace * @param comparator the Comparator to place at the given index * @param ascending the sort order: ascending (true) or descending (false) */ public void setComparator(int index, Comparator<T> comparator, boolean ascending) { this.comparators.set(index, new InvertibleComparator<>(comparator, ascending)); }
/** * Return a decorator for the given comparator which accepts * null values and sorts them lower than non-null values. * @see NullSafeComparator#NullSafeComparator(boolean) */ public static <T> Comparator<T> nullsLow(Comparator<T> comparator) { return new NullSafeComparator<>(comparator, true); }
/** * Change the sort order at the given index to ascending. * @param index the index of the comparator to change */ public void setAscendingOrder(int index) { this.comparators.get(index).setAscending(true); }
@Override @SuppressWarnings("unchecked") public int compare(T o1, T o2) { Assert.state(!this.comparators.isEmpty(), "No sort definitions have been added to this CompoundComparator to compare"); for (InvertibleComparator comparator : this.comparators) { int result = comparator.compare(o1, o2); if (result != 0) { return result; } } return 0; }
@Override public int compare(Integer o1, Integer o2) { assertThat(o1, instanceOf(Integer.class)); assertThat(o2, instanceOf(Integer.class)); this.called = true; return super.compare(o1, o2); };
/** * Add a Comparator to the end of the chain using the provided sort order. * @param comparator the Comparator to add to the end of the chain * @param ascending the sort order: ascending (true) or descending (false) */ @SuppressWarnings("unchecked") public void addComparator(Comparator<? extends T> comparator, boolean ascending) { this.comparators.add(new InvertibleComparator(comparator, ascending)); }
/** * Invert the sort order of each sort definition contained by this compound * comparator. */ public void invertOrder() { for (InvertibleComparator comparator : this.comparators) { comparator.invertOrder(); } }
/** * Return a decorator for the given comparator which accepts * null values and sorts them higher than non-null values. * @see NullSafeComparator#NullSafeComparator(boolean) */ public static <T> Comparator<T> nullsHigh(Comparator<T> comparator) { return new NullSafeComparator<>(comparator, false); }
/** * Change the sort order at the given index to descending sort. * @param index the index of the comparator to change */ public void setDescendingOrder(int index) { this.comparators.get(index).setAscending(false); }
/** * Create a new {@link ConvertingComparator} instance. * @param converter the converter */ public ConvertingComparator(Converter<S, T> converter) { this(Comparators.comparable(), converter); }
/** * Add a Comparator to the end of the chain. * <p>The Comparator will default to ascending sort order, * unless it is a InvertibleComparator. * @param comparator the Comparator to add to the end of the chain * @see InvertibleComparator */ @SuppressWarnings("unchecked") public void addComparator(Comparator<? extends T> comparator) { if (comparator instanceof InvertibleComparator) { this.comparators.add((InvertibleComparator) comparator); } else { this.comparators.add(new InvertibleComparator(comparator)); } }
/** * Invert the sort order of the sort definition at the specified index. * @param index the index of the comparator to invert */ public void invertOrder(int index) { this.comparators.get(index).invertOrder(); }
/** * Return a decorator for the given comparator which accepts * null values and sorts them lower than non-null values. * @see NullSafeComparator#NullSafeComparator(boolean) */ public static <T> Comparator<T> nullsLow(Comparator<T> comparator) { return new NullSafeComparator<>(comparator, true); }
/** * Create an InvertibleComparator that sorts based on the provided order. * For the actual comparison, the specified Comparator will be used. * @param comparator the comparator to decorate * @param ascending the sort order: ascending (true) or descending (false) */ public InvertibleComparator(Comparator<T> comparator, boolean ascending) { Assert.notNull(comparator, "Comparator must not be null"); this.comparator = comparator; setAscending(ascending); }
/** * Replace the Comparator at the given index. * <p>The Comparator will default to ascending sort order, * unless it is a InvertibleComparator. * @param index the index of the Comparator to replace * @param comparator the Comparator to place at the given index * @see InvertibleComparator */ @SuppressWarnings("unchecked") public void setComparator(int index, Comparator<? extends T> comparator) { if (comparator instanceof InvertibleComparator) { this.comparators.set(index, (InvertibleComparator) comparator); } else { this.comparators.set(index, new InvertibleComparator(comparator)); } }
@Test(expected = IllegalArgumentException.class) public void shouldNeedComparatorWithAscending() throws Exception { new InvertibleComparator<>(null, true); }
@Test(expected = IllegalArgumentException.class) public void shouldNeedComparator() throws Exception { new InvertibleComparator<>(null); }