/** * Gets a comparator that compares using an array of {@link Comparator}s, applied * in sequence until one returns not equal or the array 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 array is null or contains a null * @see ComparatorChain */ public static <E> Comparator<E> chainedComparator(final Comparator<E>... comparators) { final ComparatorChain<E> chain = new ComparatorChain<>(); for (final Comparator<E> comparator : comparators) { if (comparator == null) { throw new NullPointerException("Comparator cannot be null"); } chain.addComparator(comparator); } return chain; }
/** * Change the sort order at the given index in the * ComparatorChain to a forward sort. * * @param index Index of the ComparatorChain */ public void setForwardSort(final int index) { checkLocked(); orderingBits.clear(index); }
/** * Replace the Comparator at the given index, maintaining * the existing sort order. * * @param index index of the Comparator to replace * @param comparator Comparator to place at the given index * @throws IndexOutOfBoundsException * if index < 0 or index >= size() */ public void setComparator(final int index, final Comparator<E> comparator) throws IndexOutOfBoundsException { setComparator(index, comparator, false); }
/** * Add a Comparator to the end of the chain using the * forward sort order * * @param comparator Comparator with the forward sort order */ public void addComparator(final Comparator<E> comparator) { addComparator(comparator, false); }
public int compare(final E o1, final E o2) throws UnsupportedOperationException { if (isLocked == false) { checkChainIntegrity(); isLocked = true;
updateDeckCounts(true); mDecklistChain = new ComparatorChain<>(); mDecklistChain.addComparator(new CardHelpers.CardComparatorSideboard()); mDecklistChain.addComparator(new CardHelpers.CardComparatorSupertype( getResources().getStringArray(R.array.card_types_extra) )); mDecklistChain.addComparator(new CardHelpers.CardComparatorCMC()); mDecklistChain.addComparator(new CardHelpers.CardComparatorColor()); mDecklistChain.addComparator(new CardHelpers.CardComparatorName());
/** * Change the sort order at the given index in the * ComparatorChain to a reverse sort. * * @param index Index of the ComparatorChain */ public void setReverseSort(final int index) { checkLocked(); orderingBits.set(index); }
/** * Add a Comparator to the end of the chain using the * given sort order * * @param comparator Comparator to add to the end of the chain * @param reverse false = forward sort order; true = reverse sort order */ public void addComparator(final Comparator<E> comparator, final boolean reverse) { checkLocked(); comparatorChain.add(comparator); if (reverse == true) { orderingBits.set(comparatorChain.size() - 1); } }
/** * Replace the Comparator at the given index in the * ComparatorChain, using the given sort order * * @param index index of the Comparator to replace * @param comparator Comparator to set * @param reverse false = forward sort order; true = reverse sort order */ public void setComparator(final int index, final Comparator<E> comparator, final boolean reverse) { checkLocked(); comparatorChain.set(index,comparator); if (reverse == true) { orderingBits.set(index); } else { orderingBits.clear(index); } }