/** * 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)); }
@Test public void shouldInvert() throws Exception { InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator); assertThat(invertibleComparator.isAscending(), is(true)); assertThat(invertibleComparator.compare(1, 2), is(-1)); invertibleComparator.invertOrder(); assertThat(invertibleComparator.isAscending(), is(false)); assertThat(invertibleComparator.compare(1, 2), is(1)); }
/** * 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); }
@Test public void shouldDefaultToAscending() throws Exception { InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator); assertThat(invertibleComparator.isAscending(), is(true)); assertThat(invertibleComparator.compare(1, 2), is(-1)); }
@Test public void shouldCompareDescending() throws Exception { InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator, false); assertThat(invertibleComparator.compare(1, 2), is(1)); }
/** * Invert the sort order of each sort definition contained by this compound * comparator. */ public void invertOrder() { for (InvertibleComparator comparator : this.comparators) { comparator.invertOrder(); } }
@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; }
/** * 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(); }
@Test public void shouldCompareAscending() throws Exception { InvertibleComparator<Integer> invertibleComparator = new InvertibleComparator<>(comparator, true); assertThat(invertibleComparator.compare(1, 2), is(-1)); }
@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; }
/** * 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)); }
/** * 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); }
/** * Invert the sort order of each sort definition contained by this compound * comparator. */ public void invertOrder() { for (InvertibleComparator comparator : this.comparators) { comparator.invertOrder(); } }
public int compare(T o1, T o2) { Assert.state(this.comparators.size() > 0, "No sort definitions have been added to this CompoundComparator to compare"); for (InvertibleComparator<T> comparator : this.comparators) { int result = comparator.compare(o1, o2); if (result != 0) { return result; } } return 0; }
/** * 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)); } }
/** * 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); }
/** * 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(); }
@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; }
/** * 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)); } }
/** * 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); }