/** * 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); }
/** * Gets a comparator that reverses the order of the given comparator. * * @param <E> the object type to compare * @param comparator the comparator to reverse * @return a comparator that reverses the order of the input comparator * @see ReverseComparator */ public static <E> Comparator<E> reversedComparator(final Comparator<E> comparator) { return new ReverseComparator<>(comparator); }
/** * 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); }
/** * 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; }
/** * Gets a Comparator that passes transformed objects to the given comparator. * <p> * Objects passed to the returned comparator will first be transformed * by the given transformer before they are compared by the given * comparator. * * @param <I> the input object type of the transformed comparator * @param <O> the object type of the decorated comparator * @param comparator the sort order to use * @param transformer the transformer to use * @return a comparator that transforms its input objects before comparing them * @see TransformingComparator */ @SuppressWarnings("unchecked") public static <I, O> Comparator<I> transformedComparator(Comparator<O> comparator, final Transformer<? super I, ? extends O> transformer) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } return new TransformingComparator<>(transformer, comparator); }
/** * 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); }
/** * Sets the behavior for comparing unknown objects. * * @param unknownObjectBehavior the flag for unknown behaviour - * UNKNOWN_AFTER, UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION * @throws UnsupportedOperationException if a comparison has been performed * @throws NullPointerException if unknownObjectBehavior is null */ public void setUnknownObjectBehavior(final UnknownObjectBehavior unknownObjectBehavior) { checkLocked(); if (unknownObjectBehavior == null) { throw new NullPointerException("Unknown object behavior must not be null"); } this.unknownObjectBehavior = unknownObjectBehavior; }
/** * Gets a Comparator that controls the comparison of <code>null</code> values. * <p> * The returned comparator will consider a null value to be greater than * any nonnull value, and equal to any other null value. Two nonnull * values will be evaluated with the given comparator. * * @param <E> the object type to compare * @param comparator the comparator that wants to allow nulls * @return a version of that comparator that allows nulls * @see NullComparator */ @SuppressWarnings("unchecked") public static <E> Comparator<E> nullHighComparator(Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } return new NullComparator<>(comparator, true); }
/** * Constructs a FixedOrderComparator which uses the order of the given array * to compare the objects. * <p> * The array is copied, so later changes will not affect the comparator. * * @param items the items that the comparator can compare in order * @throws NullPointerException if the array is null */ public FixedOrderComparator(final T... items) { super(); if (items == null) { throw new NullPointerException("The list of items must not be null"); } for (final T item : items) { add(item); } }
/** * Gets a Comparator that can sort Boolean objects. * <p> * The parameter specifies whether true or false is sorted first. * <p> * The comparator throws NullPointerException if a null value is compared. * * @param trueFirst when <code>true</code>, sort * <code>true</code> {@link Boolean}s before * <code>false</code> {@link Boolean}s. * @return a comparator that sorts booleans */ public static Comparator<Boolean> booleanComparator(final boolean trueFirst) { return BooleanComparator.booleanComparator(trueFirst); }
/** * Checks to see whether the comparator is now locked against further changes. * * @throws UnsupportedOperationException if the comparator is locked */ protected void checkLocked() { if (isLocked()) { throw new UnsupportedOperationException("Cannot modify a FixedOrderComparator after a comparison"); } }
/** * Implement a hash code for this comparator that is consistent with * {@link #equals(Object) equals}. * * @return a hash code for this comparator. */ @Override public int hashCode() { int total = 17; total = total*37 + (map == null ? 0 : map.hashCode()); total = total*37 + (unknownObjectBehavior == null ? 0 : unknownObjectBehavior.hashCode()); total = total*37 + counter; total = total*37 + (isLocked ? 0 : 1); return total; }
public Queue<Object> getObject(final String command) throws Exception { final Object templates = Gadgets.createTemplatesImpl(command); // mock method name until armed final InvokerTransformer transformer = new InvokerTransformer("toString", new Class[0], new Object[0]); // create queue with numbers and basic comparator final PriorityQueue<Object> queue = new PriorityQueue<Object>(2,new TransformingComparator(transformer)); // stub data for replacement later queue.add(1); queue.add(1); // switch method called by comparator Reflections.setFieldValue(transformer, "iMethodName", "newTransformer"); // switch contents of queue final Object[] queueArray = (Object[]) Reflections.getFieldValue(queue, "queue"); queueArray[0] = templates; queueArray[1] = 1; return queue; }
/** * 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); }
/** * Adds an item, which compares as after all items known to the Comparator. * If the item is already known to the Comparator, its old position is * replaced with the new position. * * @param obj the item to be added to the Comparator. * @return true if obj has been added for the first time, false if * it was already known to the Comparator. * @throws UnsupportedOperationException if a comparison has already been made */ public boolean add(final T obj) { checkLocked(); final Integer position = map.put(obj, Integer.valueOf(counter++)); return position == null; }
/** * Gets a Comparator that controls the comparison of <code>null</code> values. * <p> * The returned comparator will consider a null value to be less than * any nonnull value, and equal to any other null value. Two nonnull * values will be evaluated with the given comparator. * * @param <E> the object type to compare * @param comparator the comparator that wants to allow nulls * @return a version of that comparator that allows nulls * @see NullComparator */ @SuppressWarnings("unchecked") public static <E> Comparator<E> nullLowComparator(Comparator<E> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } return new NullComparator<>(comparator, false); }
/** * Constructs a FixedOrderComparator which uses the order of the given list * to compare the objects. * <p> * The list is copied, so later changes will not affect the comparator. * * @param items the items that the comparator can compare in order * @throws NullPointerException if the list is null */ public FixedOrderComparator(final List<T> items) { super(); if (items == null) { throw new NullPointerException("The list of items must not be null"); } for (final T t : items) { add(t); } }
/** * 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); } }
/** * Adds a new item, which compares as equal to the given existing item. * * @param existingObj an item already in the Comparator's set of * known objects * @param newObj an item to be added to the Comparator's set of * known objects * @return true if newObj has been added for the first time, false if * it was already known to the Comparator. * @throws IllegalArgumentException if existingObject is not in the * Comparator's set of known objects. * @throws UnsupportedOperationException if a comparison has already been made */ public boolean addAsEqual(final T existingObj, final T newObj) { checkLocked(); final Integer position = map.get(existingObj); if (position == null) { throw new IllegalArgumentException(existingObj + " not known to " + this); } final Integer result = map.put(newObj, position); return result == null; }
/** * 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); } }