http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/ComparatorChain.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/ComparatorChain.java b/src/java/org/apache/commons/collections/comparators/ComparatorChain.java index 32dcd21..cbb6c89 100644 --- a/src/java/org/apache/commons/collections/comparators/ComparatorChain.java +++ b/src/java/org/apache/commons/collections/comparators/ComparatorChain.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -34,33 +34,33 @@ import java.util.List; * to multi-column sorting in SQL, and this class * allows Java classes to emulate that kind of behaviour * when sorting a List.</p> - * + * * <p>To further facilitate SQL-like sorting, the order of * any single Comparator in the list can be reversed.</p> - * + * * <p>Calling a method that adds new Comparators or * changes the ascend/descend sort <i>after compare(Object, * Object) has been called</i> will result in an * UnsupportedOperationException. However, <i>take care</i> * to not alter the underlying List of Comparators * or the BitSet that defines the sort order.</p> - * + * * <p>Instances of ComparatorChain are not synchronized. * The class is not thread-safe at construction time, but * it <i>is</i> thread-safe to perform multiple comparisons * after all the setup operations are complete.</p> - * + * * @since Commons Collections 2.0 * @author Morgan Delagrange * @version $Revision$ $Date$ */ -public class ComparatorChain implements Comparator, Serializable { +public class ComparatorChain<E> implements Comparator<E>, Serializable { /** Serialization version from Collections 2.0. */ private static final long serialVersionUID = -721644942746081630L; - + /** The list of comparators in the chain. */ - protected List comparatorChain = null; + protected List<Comparator<E>> comparatorChain = null; /** Order - false (clear) = ascend; true (set) = descend. */ protected BitSet orderingBits = null; /** Whether the chain has been "locked". */ @@ -70,32 +70,32 @@ public class ComparatorChain implements Comparator, Serializable { /** * Construct a ComparatorChain with no Comparators. * You must add at least one Comparator before calling - * the compare(Object,Object) method, or an + * the compare(Object,Object) method, or an * UnsupportedOperationException is thrown */ public ComparatorChain() { - this(new ArrayList(),new BitSet()); + this(new ArrayList<Comparator<E>>(), new BitSet()); } /** * Construct a ComparatorChain with a single Comparator, * sorting in the forward order - * + * * @param comparator First comparator in the Comparator chain */ - public ComparatorChain(Comparator comparator) { - this(comparator,false); + public ComparatorChain(Comparator<E> comparator) { + this(comparator, false); } /** * Construct a Comparator chain with a single Comparator, * sorting in the given order - * + * * @param comparator First Comparator in the ComparatorChain * @param reverse false = forward sort; true = reverse sort */ - public ComparatorChain(Comparator comparator, boolean reverse) { - comparatorChain = new ArrayList(); + public ComparatorChain(Comparator<E> comparator, boolean reverse) { + comparatorChain = new ArrayList<Comparator<E>>(); comparatorChain.add(comparator); orderingBits = new BitSet(1); if (reverse == true) { @@ -105,14 +105,14 @@ public class ComparatorChain implements Comparator, Serializable { /** * Construct a ComparatorChain from the Comparators in the - * List. All Comparators will default to the forward + * List. All Comparators will default to the forward * sort order. - * + * * @param list List of Comparators * @see #ComparatorChain(List,BitSet) */ - public ComparatorChain(List list) { - this(list,new BitSet(list.size())); + public ComparatorChain(List<Comparator<E>> list) { + this(list, new BitSet(list.size())); } /** @@ -124,13 +124,13 @@ public class ComparatorChain implements Comparator, Serializable { * If that method returns <i>false</i>, the forward * sort order is used; a return value of <i>true</i> * indicates reverse sort order. - * + * * @param list List of Comparators. NOTE: This constructor does not perform a * defensive copy of the list * @param bits Sort order for each Comparator. Extra bits are ignored, * unless extra Comparators are added by another method. */ - public ComparatorChain(List list, BitSet bits) { + public ComparatorChain(List<Comparator<E>> list, BitSet bits) { comparatorChain = list; orderingBits = bits; } @@ -139,23 +139,23 @@ public class ComparatorChain implements Comparator, Serializable { /** * 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(Comparator comparator) { - addComparator(comparator,false); + public void addComparator(Comparator<E> comparator) { + addComparator(comparator, false); } /** * 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(Comparator comparator, boolean reverse) { + public void addComparator(Comparator<E> comparator, boolean reverse) { checkLocked(); - + comparatorChain.add(comparator); if (reverse == true) { orderingBits.set(comparatorChain.size() - 1); @@ -165,26 +165,25 @@ public class ComparatorChain implements Comparator, Serializable { /** * 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 * @exception IndexOutOfBoundsException * if index < 0 or index >= size() */ - public void setComparator(int index, Comparator comparator) - throws IndexOutOfBoundsException { - setComparator(index,comparator,false); + public void setComparator(int index, Comparator<E> comparator) throws IndexOutOfBoundsException { + setComparator(index, comparator, false); } /** * 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(int index, Comparator comparator, boolean reverse) { + public void setComparator(int index, Comparator<E> comparator, boolean reverse) { checkLocked(); comparatorChain.set(index,comparator); @@ -195,11 +194,10 @@ public class ComparatorChain implements Comparator, Serializable { } } - /** * Change the sort order at the given index in the * ComparatorChain to a forward sort. - * + * * @param index Index of the ComparatorChain */ public void setForwardSort(int index) { @@ -210,7 +208,7 @@ public class ComparatorChain implements Comparator, Serializable { /** * Change the sort order at the given index in the * ComparatorChain to a reverse sort. - * + * * @param index Index of the ComparatorChain */ public void setReverseSort(int index) { @@ -220,7 +218,7 @@ public class ComparatorChain implements Comparator, Serializable { /** * Number of Comparators in the current ComparatorChain. - * + * * @return Comparator count */ public int size() { @@ -231,8 +229,8 @@ public class ComparatorChain implements Comparator, Serializable { * Determine if modifications can still be made to the * ComparatorChain. ComparatorChains cannot be modified * once they have performed a comparison. - * - * @return true = ComparatorChain cannot be modified; false = + * + * @return true = ComparatorChain cannot be modified; false = * ComparatorChain can still be modified. */ public boolean isLocked() { @@ -256,7 +254,7 @@ public class ComparatorChain implements Comparator, Serializable { /** * Perform comparisons on the Objects as per * Comparator.compare(o1,o2). - * + * * @param o1 the first object to compare * @param o2 the second object to compare * @return -1, 0, or 1 @@ -264,31 +262,29 @@ public class ComparatorChain implements Comparator, Serializable { * if the ComparatorChain does not contain at least one * Comparator */ - public int compare(Object o1, Object o2) throws UnsupportedOperationException { + public int compare(E o1, E o2) throws UnsupportedOperationException { if (isLocked == false) { checkChainIntegrity(); isLocked = true; } // iterate over all comparators in the chain - Iterator comparators = comparatorChain.iterator(); + Iterator<Comparator<E>> comparators = comparatorChain.iterator(); for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) { - Comparator comparator = (Comparator) comparators.next(); + Comparator<E> comparator = comparators.next(); int retval = comparator.compare(o1,o2); if (retval != 0) { // invert the order if it is a reverse sort if (orderingBits.get(comparatorIndex) == true) { if(Integer.MIN_VALUE == retval) { retval = Integer.MAX_VALUE; - } else { + } else { retval *= -1; } } - return retval; } - } // if comparators are exhausted, return 0 @@ -299,49 +295,51 @@ public class ComparatorChain implements Comparator, Serializable { /** * Implement a hash code for this comparator that is consistent with * {@link #equals(Object) equals}. - * + * * @return a suitable hash code * @since Commons Collections 3.0 */ public int hashCode() { int hash = 0; - if(null != comparatorChain) { + if (null != comparatorChain) { hash ^= comparatorChain.hashCode(); } - if(null != orderingBits) { + if (null != orderingBits) { hash ^= orderingBits.hashCode(); } return hash; } /** - * Returns <code>true</code> iff <i>that</i> Object is - * is a {@link Comparator} whose ordering is known to be + * Returns <code>true</code> iff <i>that</i> Object is + * is a {@link Comparator} whose ordering is known to be * equivalent to mine. * <p> * This implementation returns <code>true</code> * iff <code><i>object</i>.{@link Object#getClass() getClass()}</code> - * equals <code>this.getClass()</code>, and the underlying + * equals <code>this.getClass()</code>, and the underlying * comparators and order bits are equal. * Subclasses may want to override this behavior to remain consistent * with the {@link Comparator#equals(Object)} contract. - * + * * @param object the object to compare with * @return true if equal * @since Commons Collections 3.0 */ public boolean equals(Object object) { - if(this == object) { + if (this == object) { return true; - } else if(null == object) { - return false; - } else if(object.getClass().equals(this.getClass())) { - ComparatorChain chain = (ComparatorChain)object; - return ( (null == orderingBits ? null == chain.orderingBits : orderingBits.equals(chain.orderingBits)) - && (null == comparatorChain ? null == chain.comparatorChain : comparatorChain.equals(chain.comparatorChain)) ); - } else { + } + if (null == object) { return false; } + if (object.getClass().equals(this.getClass())) { + ComparatorChain<?> chain = (ComparatorChain<?>) object; + return ((null == orderingBits ? null == chain.orderingBits : orderingBits + .equals(chain.orderingBits)) && (null == comparatorChain ? null == chain.comparatorChain + : comparatorChain.equals(chain.comparatorChain))); + } + return false; } }
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java b/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java index dc47da4..05fc835 100644 --- a/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java +++ b/src/java/org/apache/commons/collections/comparators/FixedOrderComparator.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,11 +18,10 @@ package org.apache.commons.collections.comparators; import java.util.Comparator; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; -/** +/** * A Comparator which imposes a specific order on a specific set of Objects. * Objects are presented to the FixedOrderComparator in a specified order and * subsequent calls to {@link #compare(Object, Object) compare} yield that order. @@ -40,7 +39,7 @@ import java.util.Map; * Instances of FixedOrderComparator are not synchronized. The class is not * thread-safe at construction time, but it is thread-safe to perform * multiple comparisons after all the setup operations are complete. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ * @@ -48,55 +47,47 @@ import java.util.Map; * @author Stephen Colebourne * @author Janek Bogucki */ -public class FixedOrderComparator implements Comparator { - - /** - * Behavior when comparing unknown Objects: - * unknown objects compare as before known Objects. - */ - public static final int UNKNOWN_BEFORE = 0; +public class FixedOrderComparator<T> implements Comparator<T> { - /** - * Behavior when comparing unknown Objects: - * unknown objects compare as after known Objects. - */ - public static final int UNKNOWN_AFTER = 1; - - /** - * Behavior when comparing unknown Objects: - * unknown objects cause a IllegalArgumentException to be thrown. - * This is the default behavior. + /** + * Unknown object behavior enum. + * @since Commons Collections 5 */ - public static final int UNKNOWN_THROW_EXCEPTION = 2; + public static enum UnknownObjectBehavior { + BEFORE, AFTER, EXCEPTION; + } /** Internal map of object to position */ - private final Map map = new HashMap(); + private final Map<T, Integer> map = new HashMap<T, Integer>(); + /** Counter used in determining the position in the map */ private int counter = 0; + /** Is the comparator locked against further change */ private boolean isLocked = false; + /** The behaviour in the case of an unknown object */ - private int unknownObjectBehavior = UNKNOWN_THROW_EXCEPTION; + private UnknownObjectBehavior unknownObjectBehavior = UnknownObjectBehavior.EXCEPTION; // Constructors //----------------------------------------------------------------------- - /** + /** * Constructs an empty FixedOrderComparator. */ public FixedOrderComparator() { super(); } - /** + /** * 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 IllegalArgumentException if the array is null */ - public FixedOrderComparator(Object[] items) { + public FixedOrderComparator(T[] items) { super(); if (items == null) { throw new IllegalArgumentException("The list of items must not be null"); @@ -106,22 +97,22 @@ public class FixedOrderComparator implements Comparator { } } - /** + /** * 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 IllegalArgumentException if the list is null */ - public FixedOrderComparator(List items) { + public FixedOrderComparator(List<T> items) { super(); if (items == null) { throw new IllegalArgumentException("The list of items must not be null"); } - for (Iterator it = items.iterator(); it.hasNext();) { - add(it.next()); + for (T t : items) { + add(t); } } @@ -130,7 +121,7 @@ public class FixedOrderComparator implements Comparator { /** * Returns true if modifications cannot be made to the FixedOrderComparator. * FixedOrderComparators cannot be modified once they have performed a comparison. - * + * * @return true if attempts to change the FixedOrderComparator yield an * UnsupportedOperationException, false if it can be changed. */ @@ -140,7 +131,7 @@ public class FixedOrderComparator implements Comparator { /** * Checks to see whether the comparator is now locked against further changes. - * + * * @throws UnsupportedOperationException if the comparator is locked */ protected void checkLocked() { @@ -149,118 +140,108 @@ public class FixedOrderComparator implements Comparator { } } - /** + /** * Gets the behavior for comparing unknown objects. - * - * @return the flag for unknown behaviour - UNKNOWN_AFTER, - * UNKNOWN_BEFORE or UNKNOWN_THROW_EXCEPTION + * + * @return {@link UnknownObjectBehavior} */ - public int getUnknownObjectBehavior() { + public UnknownObjectBehavior getUnknownObjectBehavior() { return unknownObjectBehavior; } - /** + /** * 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 IllegalArgumentException if the unknown flag is not valid */ - public void setUnknownObjectBehavior(int unknownObjectBehavior) { + public void setUnknownObjectBehavior(UnknownObjectBehavior unknownObjectBehavior) { checkLocked(); - if (unknownObjectBehavior != UNKNOWN_AFTER - && unknownObjectBehavior != UNKNOWN_BEFORE - && unknownObjectBehavior != UNKNOWN_THROW_EXCEPTION) { - throw new IllegalArgumentException("Unrecognised value for unknown behaviour flag"); + if (unknownObjectBehavior == null) { + throw new IllegalArgumentException("Unknown object behavior must not be null"); } this.unknownObjectBehavior = unknownObjectBehavior; } // Methods for adding items //----------------------------------------------------------------------- - /** + /** * 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(Object obj) { + public boolean add(T obj) { checkLocked(); - Object position = map.put(obj, new Integer(counter++)); + Integer position = map.put(obj, new Integer(counter++)); return (position == null); } /** * Adds a new item, which compares as equal to the given existing item. - * - * @param existingObj an item already in the Comparator's set of + * + * @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 + * @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(Object existingObj, Object newObj) { + public boolean addAsEqual(T existingObj, T newObj) { checkLocked(); - Integer position = (Integer) map.get(existingObj); + Integer position = map.get(existingObj); if (position == null) { throw new IllegalArgumentException(existingObj + " not known to " + this); } - Object result = map.put(newObj, position); + Integer result = map.put(newObj, position); return (result == null); } // Comparator methods //----------------------------------------------------------------------- - /** + /** * Compares two objects according to the order of this Comparator. * <p> * It is important to note that this class will throw an IllegalArgumentException - * in the case of an unrecognised object. This is not specified in the + * in the case of an unrecognised object. This is not specified in the * Comparator interface, but is the most appropriate exception. - * + * * @param obj1 the first object to compare * @param obj2 the second object to compare * @return negative if obj1 is less, positive if greater, zero if equal - * @throws IllegalArgumentException if obj1 or obj2 are not known + * @throws IllegalArgumentException if obj1 or obj2 are not known * to this Comparator and an alternative behavior has not been set * via {@link #setUnknownObjectBehavior(int)}. */ - public int compare(Object obj1, Object obj2) { + public int compare(T obj1, T obj2) { isLocked = true; - Integer position1 = (Integer) map.get(obj1); - Integer position2 = (Integer) map.get(obj2); + Integer position1 = map.get(obj1); + Integer position2 = map.get(obj2); if (position1 == null || position2 == null) { switch (unknownObjectBehavior) { - case UNKNOWN_BEFORE : - if (position1 == null) { - return (position2 == null) ? 0 : -1; - } else { - return 1; - } - case UNKNOWN_AFTER : - if (position1 == null) { - return (position2 == null) ? 0 : 1; - } else { - return -1; - } - case UNKNOWN_THROW_EXCEPTION : - Object unknownObj = (position1 == null) ? obj1 : obj2; - throw new IllegalArgumentException("Attempting to compare unknown object " + unknownObj); - default : - throw new UnsupportedOperationException("Unknown unknownObjectBehavior: " + unknownObjectBehavior); + case BEFORE: + return position1 == null ? position2 == null ? 0 : -1 : 1; + case AFTER: + return position1 == null ? position2 == null ? 0 : 1 : -1; + case EXCEPTION: + Object unknownObj = (position1 == null) ? obj1 : obj2; + throw new IllegalArgumentException("Attempting to compare unknown object " + + unknownObj); + default: //could be null + throw new UnsupportedOperationException("Unknown unknownObjectBehavior: " + + unknownObjectBehavior); } - } else { - return position1.compareTo(position2); } + return position1.compareTo(position2); } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/NullComparator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/NullComparator.java b/src/java/org/apache/commons/collections/comparators/NullComparator.java index 633fe36..ea2c5a1 100644 --- a/src/java/org/apache/commons/collections/comparators/NullComparator.java +++ b/src/java/org/apache/commons/collections/comparators/NullComparator.java @@ -19,6 +19,8 @@ package org.apache.commons.collections.comparators; import java.io.Serializable; import java.util.Comparator; +import org.apache.commons.collections.ComparatorUtils; + /** * A Comparator that will compare nulls to be either lower or higher than * other objects. @@ -28,7 +30,7 @@ import java.util.Comparator; * * @author Michael A. Smith */ -public class NullComparator implements Comparator, Serializable { +public class NullComparator<E> implements Comparator<E>, Serializable { /** Serialization version. */ private static final long serialVersionUID = -5820772575483504339L; @@ -36,7 +38,7 @@ public class NullComparator implements Comparator, Serializable { /** * The comparator to use when comparing two non-<code>null</code> objects. **/ - private Comparator nonNullComparator; + private Comparator<E> nonNullComparator; /** * Specifies whether a <code>null</code> are compared as higher than @@ -51,8 +53,9 @@ public class NullComparator implements Comparator, Serializable { * non-<code>null</code> objects, the {@link ComparableComparator} is * used. **/ + @SuppressWarnings("unchecked") public NullComparator() { - this(ComparableComparator.getInstance(), true); + this(ComparatorUtils.NATURAL_COMPARATOR, true); } /** @@ -68,7 +71,7 @@ public class NullComparator implements Comparator, Serializable { * @exception NullPointerException if <code>nonNullComparator</code> is * <code>null</code> **/ - public NullComparator(Comparator nonNullComparator) { + public NullComparator(Comparator<E> nonNullComparator) { this(nonNullComparator, true); } @@ -84,8 +87,9 @@ public class NullComparator implements Comparator, Serializable { * that <code>null</code> should be compared as lower than a * non-<code>null</code> object. **/ + @SuppressWarnings("unchecked") public NullComparator(boolean nullsAreHigh) { - this(ComparableComparator.getInstance(), nullsAreHigh); + this(ComparatorUtils.NATURAL_COMPARATOR, nullsAreHigh); } /** @@ -107,11 +111,11 @@ public class NullComparator implements Comparator, Serializable { * @exception NullPointerException if <code>nonNullComparator</code> is * <code>null</code> **/ - public NullComparator(Comparator nonNullComparator, boolean nullsAreHigh) { + public NullComparator(Comparator<E> nonNullComparator, boolean nullsAreHigh) { this.nonNullComparator = nonNullComparator; this.nullsAreHigh = nullsAreHigh; - if(nonNullComparator == null) { + if (nonNullComparator == null) { throw new NullPointerException("null nonNullComparator"); } } @@ -133,7 +137,7 @@ public class NullComparator implements Comparator, Serializable { * "higher" than (greater than, after, etc.) <code>o2</code>; or * <code>0</code> if <code>o1</code> and <code>o2</code> are equal. **/ - public int compare(Object o1, Object o2) { + public int compare(E o1, E o2) { if(o1 == o2) { return 0; } if(o1 == null) { return (this.nullsAreHigh ? 1 : -1); } if(o2 == null) { return (this.nullsAreHigh ? -1 : 1); } @@ -167,7 +171,7 @@ public class NullComparator implements Comparator, Serializable { if(obj == this) { return true; } if(!obj.getClass().equals(this.getClass())) { return false; } - NullComparator other = (NullComparator)obj; + NullComparator<?> other = (NullComparator<?>) obj; return ((this.nullsAreHigh == other.nullsAreHigh) && (this.nonNullComparator.equals(other.nonNullComparator))); http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/ReverseComparator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/ReverseComparator.java b/src/java/org/apache/commons/collections/comparators/ReverseComparator.java index 9148f27..a28ead1 100644 --- a/src/java/org/apache/commons/collections/comparators/ReverseComparator.java +++ b/src/java/org/apache/commons/collections/comparators/ReverseComparator.java @@ -19,6 +19,8 @@ package org.apache.commons.collections.comparators; import java.io.Serializable; import java.util.Comparator; +import org.apache.commons.collections.ComparatorUtils; + /** * Reverses the order of another comparator by reversing the arguments * to its {@link #compare(Object, Object) compare} method. @@ -31,13 +33,13 @@ import java.util.Comparator; * * @see java.util.Collections#reverseOrder() */ -public class ReverseComparator implements Comparator, Serializable { +public class ReverseComparator<E> implements Comparator<E>, Serializable { /** Serialization version from Collections 2.0. */ private static final long serialVersionUID = 2858887242028539265L; /** The comparator being decorated. */ - private Comparator comparator; + private Comparator<E> comparator; //----------------------------------------------------------------------- /** @@ -61,12 +63,9 @@ public class ReverseComparator implements Comparator, Serializable { * * @param comparator Comparator to reverse */ - public ReverseComparator(Comparator comparator) { - if(comparator != null) { - this.comparator = comparator; - } else { - this.comparator = ComparableComparator.getInstance(); - } + @SuppressWarnings("unchecked") + public ReverseComparator(Comparator<E> comparator) { + this.comparator = comparator == null ? ComparatorUtils.NATURAL_COMPARATOR : comparator; } //----------------------------------------------------------------------- @@ -77,7 +76,7 @@ public class ReverseComparator implements Comparator, Serializable { * @param obj2 the second object to compare * @return negative if obj1 is less, positive if greater, zero if equal */ - public int compare(Object obj1, Object obj2) { + public int compare(E obj1, E obj2) { return comparator.compare(obj2, obj1); } @@ -110,16 +109,17 @@ public class ReverseComparator implements Comparator, Serializable { * @since Commons Collections 3.0 */ public boolean equals(Object object) { - if(this == object) { + if (this == object) { return true; - } else if(null == object) { + } + if (null == object) { return false; - } else if(object.getClass().equals(this.getClass())) { - ReverseComparator thatrc = (ReverseComparator)object; + } + if (object.getClass().equals(this.getClass())) { + ReverseComparator<?> thatrc = (ReverseComparator<?>) object; return comparator.equals(thatrc.comparator); - } else { - return false; } + return false; } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/comparators/TransformingComparator.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/comparators/TransformingComparator.java b/src/java/org/apache/commons/collections/comparators/TransformingComparator.java index 4a2a073..62ba50c 100644 --- a/src/java/org/apache/commons/collections/comparators/TransformingComparator.java +++ b/src/java/org/apache/commons/collections/comparators/TransformingComparator.java @@ -18,6 +18,7 @@ package org.apache.commons.collections.comparators; import java.util.Comparator; +import org.apache.commons.collections.ComparatorUtils; import org.apache.commons.collections.Transformer; /** @@ -31,12 +32,12 @@ import org.apache.commons.collections.Transformer; * @see org.apache.commons.collections.Transformer * @see org.apache.commons.collections.comparators.ComparableComparator */ -public class TransformingComparator implements Comparator { +public class TransformingComparator<E> implements Comparator<E> { /** The decorated comparator. */ - protected Comparator decorated; + protected Comparator<E> decorated; /** The transformer being used. */ - protected Transformer transformer; + protected Transformer<? super E, ? extends E> transformer; //----------------------------------------------------------------------- /** @@ -45,8 +46,9 @@ public class TransformingComparator implements Comparator { * * @param transformer what will transform the arguments to <code>compare</code> */ - public TransformingComparator(Transformer transformer) { - this(transformer, new ComparableComparator()); + @SuppressWarnings("unchecked") + public TransformingComparator(Transformer<? super E, ? extends E> transformer) { + this(transformer, ComparatorUtils.NATURAL_COMPARATOR); } /** @@ -55,7 +57,7 @@ public class TransformingComparator implements Comparator { * @param transformer what will transform the arguments to <code>compare</code> * @param decorated the decorated Comparator */ - public TransformingComparator(Transformer transformer, Comparator decorated) { + public TransformingComparator(Transformer<? super E, ? extends E> transformer, Comparator<E> decorated) { this.decorated = decorated; this.transformer = transformer; } @@ -68,9 +70,9 @@ public class TransformingComparator implements Comparator { * @param obj2 the second object to transform then compare * @return negative if obj1 is less, positive if greater, zero if equal */ - public int compare(Object obj1, Object obj2) { - Object value1 = this.transformer.transform(obj1); - Object value2 = this.transformer.transform(obj2); + public int compare(E obj1, E obj2) { + E value1 = this.transformer.transform(obj1); + E value2 = this.transformer.transform(obj2); return this.decorated.compare(value1, value2); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/AllPredicate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/AllPredicate.java b/src/java/org/apache/commons/collections/functors/AllPredicate.java index cce6f8e..edde7e9 100644 --- a/src/java/org/apache/commons/collections/functors/AllPredicate.java +++ b/src/java/org/apache/commons/collections/functors/AllPredicate.java @@ -99,7 +99,7 @@ public final class AllPredicate<T> implements Predicate<T>, PredicateDecorator<T * @throws IllegalArgumentException if any predicate in the array is null * @deprecated Use {@link #allPredicate(Collection<Predicate<? super T>>)} instead */ - public static <T> Predicate<T> getInstance(Collection<Predicate<? super T>> predicates) { + public static <T> Predicate<T> getInstance(Collection<Predicate<T>> predicates) { return allPredicate(predicates); } @@ -114,13 +114,13 @@ public final class AllPredicate<T> implements Predicate<T>, PredicateDecorator<T * @throws IllegalArgumentException if the predicates array is null * @throws IllegalArgumentException if any predicate in the array is null */ - public static <T> Predicate<T> allPredicate(Collection<Predicate<? super T>> predicates) { - final Predicate<? super T>[] preds = validate(predicates); + public static <T> Predicate<T> allPredicate(Collection<? extends Predicate<T>> predicates) { + final Predicate<T>[] preds = validate(predicates); if (preds.length == 0) { return truePredicate(); } if (preds.length == 1) { - return coerce(preds[0]); + return preds[0]; } return new AllPredicate<T>(preds); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/AndPredicate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/AndPredicate.java b/src/java/org/apache/commons/collections/functors/AndPredicate.java index e2ff36d..a30fcd0 100644 --- a/src/java/org/apache/commons/collections/functors/AndPredicate.java +++ b/src/java/org/apache/commons/collections/functors/AndPredicate.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,45 +22,45 @@ import org.apache.commons.collections.Predicate; /** * Predicate implementation that returns true if both the predicates return true. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ * * @author Stephen Colebourne */ -public final class AndPredicate implements Predicate, PredicateDecorator, Serializable { +public final class AndPredicate<T> implements Predicate<T>, PredicateDecorator<T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 4189014213763186912L; - + /** The array of predicates to call */ - private final Predicate iPredicate1; + private final Predicate<? super T> iPredicate1; /** The array of predicates to call */ - private final Predicate iPredicate2; - + private final Predicate<? super T> iPredicate2; + /** * Factory to create the predicate. - * + * * @param predicate1 the first predicate to check, not null * @param predicate2 the second predicate to check, not null * @return the <code>and</code> predicate * @throws IllegalArgumentException if either predicate is null */ - public static Predicate getInstance(Predicate predicate1, Predicate predicate2) { + public static <T> Predicate<T> getInstance(Predicate<? super T> predicate1, Predicate<? super T> predicate2) { if (predicate1 == null || predicate2 == null) { throw new IllegalArgumentException("Predicate must not be null"); } - return new AndPredicate(predicate1, predicate2); + return new AndPredicate<T>(predicate1, predicate2); } /** * Constructor that performs no validation. * Use <code>getInstance</code> if you want that. - * + * * @param predicate1 the first predicate to check, not null * @param predicate2 the second predicate to check, not null */ - public AndPredicate(Predicate predicate1, Predicate predicate2) { + public AndPredicate(Predicate<? super T> predicate1, Predicate<? super T> predicate2) { super(); iPredicate1 = predicate1; iPredicate2 = predicate2; @@ -68,21 +68,22 @@ public final class AndPredicate implements Predicate, PredicateDecorator, Serial /** * Evaluates the predicate returning true if both predicates return true. - * + * * @param object the input object * @return true if both decorated predicates return true */ - public boolean evaluate(Object object) { + public boolean evaluate(T object) { return (iPredicate1.evaluate(object) && iPredicate2.evaluate(object)); } /** * Gets the two predicates being decorated as an array. - * + * * @return the predicates * @since Commons Collections 3.1 */ - public Predicate[] getPredicates() { + @SuppressWarnings("unchecked") + public Predicate<? super T>[] getPredicates() { return new Predicate[] {iPredicate1, iPredicate2}; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/AnyPredicate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/AnyPredicate.java b/src/java/org/apache/commons/collections/functors/AnyPredicate.java index b3cb88b..430d6d3 100644 --- a/src/java/org/apache/commons/collections/functors/AnyPredicate.java +++ b/src/java/org/apache/commons/collections/functors/AnyPredicate.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -35,14 +35,14 @@ import org.apache.commons.collections.Predicate; * @author Stephen Colebourne * @author Matt Benson */ -public final class AnyPredicate implements Predicate, PredicateDecorator, Serializable { +public final class AnyPredicate<T> implements Predicate<T>, PredicateDecorator<T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 7429999530934647542L; - + /** The array of predicates to call */ - private final Predicate[] iPredicates; - + private final Predicate<? super T>[] iPredicates; + /** * Factory to create the predicate. * <p> @@ -54,15 +54,16 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial * @throws IllegalArgumentException if the predicates array is null * @throws IllegalArgumentException if any predicate in the array is null */ - public static Predicate getInstance(Predicate[] predicates) { + @SuppressWarnings("unchecked") + public static <T> Predicate<T> getInstance(Predicate<? super T>[] predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { - return FalsePredicate.INSTANCE; + return FalsePredicate.<T>falsePredicate(); } if (predicates.length == 1) { - return predicates[0]; + return (Predicate<T>) predicates[0]; } - return new AnyPredicate(FunctorUtils.copy(predicates)); + return new AnyPredicate<T>(FunctorUtils.copy(predicates)); } /** @@ -76,35 +77,36 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial * @throws IllegalArgumentException if the predicates array is null * @throws IllegalArgumentException if any predicate in the array is null */ - public static Predicate getInstance(Collection predicates) { - Predicate[] preds = FunctorUtils.validate(predicates); + @SuppressWarnings("unchecked") + public static <T> Predicate<T> getInstance(Collection<? extends Predicate<T>> predicates) { + Predicate<? super T>[] preds = FunctorUtils.validate(predicates); if (preds.length == 0) { - return FalsePredicate.INSTANCE; + return FalsePredicate.<T>falsePredicate(); } if (preds.length == 1) { - return preds[0]; + return (Predicate<T>) preds[0]; } - return new AnyPredicate(preds); + return new AnyPredicate<T>(preds); } /** * Constructor that performs no validation. * Use <code>getInstance</code> if you want that. - * + * * @param predicates the predicates to check, not cloned, not null */ - public AnyPredicate(Predicate[] predicates) { + public AnyPredicate(Predicate<? super T>[] predicates) { super(); iPredicates = predicates; } /** * Evaluates the predicate returning true if any predicate returns true. - * + * * @param object the input object * @return true if any decorated predicate return true */ - public boolean evaluate(Object object) { + public boolean evaluate(T object) { for (int i = 0; i < iPredicates.length; i++) { if (iPredicates[i].evaluate(object)) { return true; @@ -115,11 +117,11 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial /** * Gets the predicates, do not modify the array. - * + * * @return the predicates * @since Commons Collections 3.1 */ - public Predicate[] getPredicates() { + public Predicate<? super T>[] getPredicates() { return iPredicates; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ChainedClosure.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ChainedClosure.java b/src/java/org/apache/commons/collections/functors/ChainedClosure.java index 6f2346a..bf2c476 100644 --- a/src/java/org/apache/commons/collections/functors/ChainedClosure.java +++ b/src/java/org/apache/commons/collections/functors/ChainedClosure.java @@ -18,7 +18,6 @@ package org.apache.commons.collections.functors; import java.io.Serializable; import java.util.Collection; -import java.util.Iterator; import org.apache.commons.collections.Closure; @@ -30,13 +29,13 @@ import org.apache.commons.collections.Closure; * * @author Stephen Colebourne */ -public class ChainedClosure implements Closure, Serializable { +public class ChainedClosure<E> implements Closure<E>, Serializable { /** Serial version UID */ private static final long serialVersionUID = -3520677225766901240L; /** The closures to call in turn */ - private final Closure[] iClosures; + private final Closure<? super E>[] iClosures; /** * Factory method that performs validation and copies the parameter array. @@ -46,15 +45,15 @@ public class ChainedClosure implements Closure, Serializable { * @throws IllegalArgumentException if the closures array is null * @throws IllegalArgumentException if any closure in the array is null */ - public static Closure getInstance(Closure[] closures) { + public static <E> Closure<E> getInstance(Closure<? super E>[] closures) { FunctorUtils.validate(closures); if (closures.length == 0) { - return NOPClosure.INSTANCE; + return NOPClosure.<E>getInstance(); } closures = FunctorUtils.copy(closures); - return new ChainedClosure(closures); + return new ChainedClosure<E>(closures); } - + /** * Create a new Closure that calls each closure in turn, passing the * result into the next closure. The ordering is that of the iterator() @@ -65,21 +64,22 @@ public class ChainedClosure implements Closure, Serializable { * @throws IllegalArgumentException if the closures collection is null * @throws IllegalArgumentException if any closure in the collection is null */ - public static Closure getInstance(Collection closures) { + @SuppressWarnings("unchecked") + public static <E> Closure<E> getInstance(Collection<Closure<E>> closures) { if (closures == null) { throw new IllegalArgumentException("Closure collection must not be null"); } if (closures.size() == 0) { - return NOPClosure.INSTANCE; + return NOPClosure.<E>getInstance(); } // convert to array like this to guarantee iterator() ordering - Closure[] cmds = new Closure[closures.size()]; + Closure<? super E>[] cmds = new Closure[closures.size()]; int i = 0; - for (Iterator it = closures.iterator(); it.hasNext();) { - cmds[i++] = (Closure) it.next(); + for (Closure<? super E> closure : closures) { + cmds[i++] = (Closure<E>) closure; } FunctorUtils.validate(cmds); - return new ChainedClosure(cmds); + return new ChainedClosure<E>(cmds); } /** @@ -90,12 +90,13 @@ public class ChainedClosure implements Closure, Serializable { * @return the <code>chained</code> closure * @throws IllegalArgumentException if either closure is null */ - public static Closure getInstance(Closure closure1, Closure closure2) { + @SuppressWarnings("unchecked") + public static <E> Closure<E> getInstance(Closure<? super E> closure1, Closure<? super E> closure2) { if (closure1 == null || closure2 == null) { throw new IllegalArgumentException("Closures must not be null"); } - Closure[] closures = new Closure[] { closure1, closure2 }; - return new ChainedClosure(closures); + Closure<E>[] closures = new Closure[] { closure1, closure2 }; + return new ChainedClosure<E>(closures); } /** @@ -104,7 +105,7 @@ public class ChainedClosure implements Closure, Serializable { * * @param closures the closures to chain, not copied, no nulls */ - public ChainedClosure(Closure[] closures) { + public ChainedClosure(Closure<? super E>[] closures) { super(); iClosures = closures; } @@ -114,7 +115,7 @@ public class ChainedClosure implements Closure, Serializable { * * @param input the input object passed to each closure */ - public void execute(Object input) { + public void execute(E input) { for (int i = 0; i < iClosures.length; i++) { iClosures[i].execute(input); } @@ -125,7 +126,7 @@ public class ChainedClosure implements Closure, Serializable { * @return the closures * @since Commons Collections 3.1 */ - public Closure[] getClosures() { + public Closure<? super E>[] getClosures() { return iClosures; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ChainedTransformer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ChainedTransformer.java b/src/java/org/apache/commons/collections/functors/ChainedTransformer.java index 4d86b89..ca69f45 100644 --- a/src/java/org/apache/commons/collections/functors/ChainedTransformer.java +++ b/src/java/org/apache/commons/collections/functors/ChainedTransformer.java @@ -18,7 +18,6 @@ package org.apache.commons.collections.functors; import java.io.Serializable; import java.util.Collection; -import java.util.Iterator; import org.apache.commons.collections.Transformer; @@ -33,13 +32,13 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class ChainedTransformer implements Transformer, Serializable { +public class ChainedTransformer<T> implements Transformer<T, T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 3514945074733160196L; /** The transformers to call in turn */ - private final Transformer[] iTransformers; + private final Transformer<? super T, ? extends T>[] iTransformers; /** * Factory method that performs validation and copies the parameter array. @@ -49,13 +48,13 @@ public class ChainedTransformer implements Transformer, Serializable { * @throws IllegalArgumentException if the transformers array is null * @throws IllegalArgumentException if any transformer in the array is null */ - public static Transformer getInstance(Transformer[] transformers) { + public static <T> Transformer<T, T> getInstance(Transformer<? super T, ? extends T>[] transformers) { FunctorUtils.validate(transformers); if (transformers.length == 0) { - return NOPTransformer.INSTANCE; + return NOPTransformer.<T>getInstance(); } transformers = FunctorUtils.copy(transformers); - return new ChainedTransformer(transformers); + return new ChainedTransformer<T>(transformers); } /** @@ -68,21 +67,18 @@ public class ChainedTransformer implements Transformer, Serializable { * @throws IllegalArgumentException if the transformers collection is null * @throws IllegalArgumentException if any transformer in the collection is null */ - public static Transformer getInstance(Collection transformers) { + @SuppressWarnings("unchecked") + public static <T> Transformer<T, T> getInstance(Collection<? extends Transformer<T, T>> transformers) { if (transformers == null) { throw new IllegalArgumentException("Transformer collection must not be null"); } if (transformers.size() == 0) { - return NOPTransformer.INSTANCE; + return NOPTransformer.<T>getInstance(); } // convert to array like this to guarantee iterator() ordering - Transformer[] cmds = new Transformer[transformers.size()]; - int i = 0; - for (Iterator it = transformers.iterator(); it.hasNext();) { - cmds[i++] = (Transformer) it.next(); - } + Transformer<T, T>[] cmds = transformers.toArray(new Transformer[transformers.size()]); FunctorUtils.validate(cmds); - return new ChainedTransformer(cmds); + return new ChainedTransformer<T>(cmds); } /** @@ -93,12 +89,13 @@ public class ChainedTransformer implements Transformer, Serializable { * @return the <code>chained</code> transformer * @throws IllegalArgumentException if either transformer is null */ - public static Transformer getInstance(Transformer transformer1, Transformer transformer2) { + @SuppressWarnings("unchecked") + public static <T> Transformer<T, T> getInstance(Transformer<? super T, ? extends T> transformer1, Transformer<? super T, ? extends T> transformer2) { if (transformer1 == null || transformer2 == null) { throw new IllegalArgumentException("Transformers must not be null"); } - Transformer[] transformers = new Transformer[] { transformer1, transformer2 }; - return new ChainedTransformer(transformers); + Transformer<? super T, ? extends T>[] transformers = new Transformer[] { transformer1, transformer2 }; + return new ChainedTransformer<T>(transformers); } /** @@ -107,7 +104,7 @@ public class ChainedTransformer implements Transformer, Serializable { * * @param transformers the transformers to chain, not copied, no nulls */ - public ChainedTransformer(Transformer[] transformers) { + public ChainedTransformer(Transformer<? super T, ? extends T>[] transformers) { super(); iTransformers = transformers; } @@ -118,7 +115,7 @@ public class ChainedTransformer implements Transformer, Serializable { * @param object the input object passed to the first transformer * @return the transformed result */ - public Object transform(Object object) { + public T transform(T object) { for (int i = 0; i < iTransformers.length; i++) { object = iTransformers[i].transform(object); } @@ -130,7 +127,7 @@ public class ChainedTransformer implements Transformer, Serializable { * @return the transformers * @since Commons Collections 3.1 */ - public Transformer[] getTransformers() { + public Transformer<? super T, ? extends T>[] getTransformers() { return iTransformers; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/CloneTransformer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/CloneTransformer.java b/src/java/org/apache/commons/collections/functors/CloneTransformer.java index c4752c4..e17888d 100644 --- a/src/java/org/apache/commons/collections/functors/CloneTransformer.java +++ b/src/java/org/apache/commons/collections/functors/CloneTransformer.java @@ -30,13 +30,13 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class CloneTransformer implements Transformer, Serializable { +public class CloneTransformer<T> implements Transformer<T, T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = -8188742709499652567L; /** Singleton predicate instance */ - public static final Transformer INSTANCE = new CloneTransformer(); + public static final Transformer<Object, Object> INSTANCE = new CloneTransformer<Object>(); /** * Factory returning the singleton instance. @@ -44,8 +44,9 @@ public class CloneTransformer implements Transformer, Serializable { * @return the singleton instance * @since Commons Collections 3.1 */ - public static Transformer getInstance() { - return INSTANCE; + @SuppressWarnings("unchecked") + public static <T> Transformer<T, T> getInstance() { + return (Transformer<T, T>) INSTANCE; } /** @@ -61,7 +62,7 @@ public class CloneTransformer implements Transformer, Serializable { * @param input the input object to transform * @return the transformed result */ - public Object transform(Object input) { + public T transform(T input) { if (input == null) { return null; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ClosureTransformer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ClosureTransformer.java b/src/java/org/apache/commons/collections/functors/ClosureTransformer.java index 6eda96d..e55685e 100644 --- a/src/java/org/apache/commons/collections/functors/ClosureTransformer.java +++ b/src/java/org/apache/commons/collections/functors/ClosureTransformer.java @@ -30,13 +30,13 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class ClosureTransformer implements Transformer, Serializable { +public class ClosureTransformer<T> implements Transformer<T, T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 478466901448617286L; /** The closure to wrap */ - private final Closure iClosure; + private final Closure<? super T> iClosure; /** * Factory method that performs validation. @@ -45,11 +45,11 @@ public class ClosureTransformer implements Transformer, Serializable { * @return the <code>closure</code> transformer * @throws IllegalArgumentException if the closure is null */ - public static Transformer getInstance(Closure closure) { + public static <T> Transformer<T, T> getInstance(Closure<? super T> closure) { if (closure == null) { throw new IllegalArgumentException("Closure must not be null"); } - return new ClosureTransformer(closure); + return new ClosureTransformer<T>(closure); } /** @@ -58,7 +58,7 @@ public class ClosureTransformer implements Transformer, Serializable { * * @param closure the closure to call, not null */ - public ClosureTransformer(Closure closure) { + public ClosureTransformer(Closure<? super T> closure) { super(); iClosure = closure; } @@ -69,7 +69,7 @@ public class ClosureTransformer implements Transformer, Serializable { * @param input the input object to transform * @return the transformed result */ - public Object transform(Object input) { + public T transform(T input) { iClosure.execute(input); return input; } @@ -80,7 +80,7 @@ public class ClosureTransformer implements Transformer, Serializable { * @return the closure * @since Commons Collections 3.1 */ - public Closure getClosure() { + public Closure<? super T> getClosure() { return iClosure; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ConstantFactory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ConstantFactory.java b/src/java/org/apache/commons/collections/functors/ConstantFactory.java index 658ad4e..ef7fea9 100644 --- a/src/java/org/apache/commons/collections/functors/ConstantFactory.java +++ b/src/java/org/apache/commons/collections/functors/ConstantFactory.java @@ -32,16 +32,16 @@ import org.apache.commons.collections.Factory; * * @author Stephen Colebourne */ -public class ConstantFactory implements Factory, Serializable { +public class ConstantFactory<T> implements Factory<T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = -3520677225766901240L; /** Returns null each time */ - public static final Factory NULL_INSTANCE = new ConstantFactory(null); + public static final Factory<Object> NULL_INSTANCE = new ConstantFactory<Object>(null); /** The closures to call in turn */ - private final Object iConstant; + private final T iConstant; /** * Factory method that performs validation. @@ -49,11 +49,12 @@ public class ConstantFactory implements Factory, Serializable { * @param constantToReturn the constant object to return each time in the factory * @return the <code>constant</code> factory. */ - public static Factory getInstance(Object constantToReturn) { + @SuppressWarnings("unchecked") + public static <T> Factory<T> getInstance(T constantToReturn) { if (constantToReturn == null) { - return NULL_INSTANCE; + return (Factory<T>) NULL_INSTANCE; } - return new ConstantFactory(constantToReturn); + return new ConstantFactory<T>(constantToReturn); } /** @@ -62,7 +63,7 @@ public class ConstantFactory implements Factory, Serializable { * * @param constantToReturn the constant to return each time */ - public ConstantFactory(Object constantToReturn) { + public ConstantFactory(T constantToReturn) { super(); iConstant = constantToReturn; } @@ -72,7 +73,7 @@ public class ConstantFactory implements Factory, Serializable { * * @return the stored constant value */ - public Object create() { + public T create() { return iConstant; } @@ -82,7 +83,7 @@ public class ConstantFactory implements Factory, Serializable { * @return the constant * @since Commons Collections 3.1 */ - public Object getConstant() { + public T getConstant() { return iConstant; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ConstantTransformer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ConstantTransformer.java b/src/java/org/apache/commons/collections/functors/ConstantTransformer.java index 6ac19e0..bd748bd 100644 --- a/src/java/org/apache/commons/collections/functors/ConstantTransformer.java +++ b/src/java/org/apache/commons/collections/functors/ConstantTransformer.java @@ -32,16 +32,27 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class ConstantTransformer implements Transformer, Serializable { +public class ConstantTransformer<I, O> implements Transformer<I, O>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 6374440726369055124L; /** Returns null each time */ - public static final Transformer NULL_INSTANCE = new ConstantTransformer(null); + public static final Transformer<Object, Object> NULL_INSTANCE = new ConstantTransformer<Object, Object>(null); /** The closures to call in turn */ - private final Object iConstant; + private final O iConstant; + + /** + * Get a typed null instance. + * @param <I> + * @param <O> + * @return Transformer<I, O> that always returns null. + */ + @SuppressWarnings("unchecked") + public static <I, O> Transformer<I, O> getNullInstance() { + return (Transformer<I, O>) NULL_INSTANCE; + } /** * Transformer method that performs validation. @@ -49,11 +60,11 @@ public class ConstantTransformer implements Transformer, Serializable { * @param constantToReturn the constant object to return each time in the factory * @return the <code>constant</code> factory. */ - public static Transformer getInstance(Object constantToReturn) { + public static <I, O> Transformer<I, O> getInstance(O constantToReturn) { if (constantToReturn == null) { - return NULL_INSTANCE; + return getNullInstance(); } - return new ConstantTransformer(constantToReturn); + return new ConstantTransformer<I, O>(constantToReturn); } /** @@ -62,7 +73,7 @@ public class ConstantTransformer implements Transformer, Serializable { * * @param constantToReturn the constant to return each time */ - public ConstantTransformer(Object constantToReturn) { + public ConstantTransformer(O constantToReturn) { super(); iConstant = constantToReturn; } @@ -73,7 +84,7 @@ public class ConstantTransformer implements Transformer, Serializable { * @param input the input object which is ignored * @return the stored constant */ - public Object transform(Object input) { + public O transform(I input) { return iConstant; } @@ -83,8 +94,34 @@ public class ConstantTransformer implements Transformer, Serializable { * @return the constant * @since Commons Collections 3.1 */ - public Object getConstant() { + public O getConstant() { return iConstant; } + /** + * {@inheritDoc} + */ + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj instanceof ConstantTransformer == false) { + return false; + } + Object otherConstant = ((ConstantTransformer<?, ?>) obj).getConstant(); + return otherConstant == getConstant() || otherConstant != null && otherConstant.equals(getConstant()); + } + + /** + * {@inheritDoc} + */ + @Override + public int hashCode() { + int result = "ConstantTransformer".hashCode() << 2; + if (getConstant() != null) { + result |= getConstant().hashCode(); + } + return result; + } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/EqualPredicate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/EqualPredicate.java b/src/java/org/apache/commons/collections/functors/EqualPredicate.java index bf9fc4d..231a235 100644 --- a/src/java/org/apache/commons/collections/functors/EqualPredicate.java +++ b/src/java/org/apache/commons/collections/functors/EqualPredicate.java @@ -62,7 +62,7 @@ public final class EqualPredicate<T> implements Predicate<T>, Serializable { * @return the predicate * @throws IllegalArgumentException if the predicate is null */ - public static <T, O extends T> Predicate<T> equalPredicate(O object) { + public static <T> Predicate<T> equalPredicate(T object) { if (object == null) { return nullPredicate(); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ExceptionClosure.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ExceptionClosure.java b/src/java/org/apache/commons/collections/functors/ExceptionClosure.java index 5079348..b5f73a9 100644 --- a/src/java/org/apache/commons/collections/functors/ExceptionClosure.java +++ b/src/java/org/apache/commons/collections/functors/ExceptionClosure.java @@ -29,14 +29,13 @@ import org.apache.commons.collections.FunctorException; * * @author Stephen Colebourne */ -public final class ExceptionClosure implements Closure, Serializable { +public final class ExceptionClosure<E> implements Closure<E>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 7179106032121985545L; - /** Singleton predicate instance */ - public static final Closure INSTANCE = new ExceptionClosure(); + public static final Closure<Object> INSTANCE = new ExceptionClosure<Object>(); /** * Factory returning the singleton instance. @@ -44,8 +43,9 @@ public final class ExceptionClosure implements Closure, Serializable { * @return the singleton instance * @since Commons Collections 3.1 */ - public static Closure getInstance() { - return INSTANCE; + @SuppressWarnings("unchecked") + public static <E> Closure<E> getInstance() { + return (Closure<E>) INSTANCE; } /** @@ -61,7 +61,7 @@ public final class ExceptionClosure implements Closure, Serializable { * @param input the input object * @throws FunctorException always */ - public void execute(Object input) { + public void execute(E input) { throw new FunctorException("ExceptionClosure invoked"); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ExceptionFactory.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ExceptionFactory.java b/src/java/org/apache/commons/collections/functors/ExceptionFactory.java index 2f8a934..56d54ef 100644 --- a/src/java/org/apache/commons/collections/functors/ExceptionFactory.java +++ b/src/java/org/apache/commons/collections/functors/ExceptionFactory.java @@ -29,14 +29,13 @@ import org.apache.commons.collections.FunctorException; * * @author Stephen Colebourne */ -public final class ExceptionFactory implements Factory, Serializable { +public final class ExceptionFactory<T> implements Factory<T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 7179106032121985545L; - /** Singleton predicate instance */ - public static final Factory INSTANCE = new ExceptionFactory(); + public static final Factory<Object> INSTANCE = new ExceptionFactory<Object>(); /** * Factory returning the singleton instance. @@ -44,8 +43,9 @@ public final class ExceptionFactory implements Factory, Serializable { * @return the singleton instance * @since Commons Collections 3.1 */ - public static Factory getInstance() { - return INSTANCE; + @SuppressWarnings("unchecked") + public static <T> Factory<T> getInstance() { + return (Factory<T>) INSTANCE; } /** @@ -61,7 +61,7 @@ public final class ExceptionFactory implements Factory, Serializable { * @return never * @throws FunctorException always */ - public Object create() { + public T create() { throw new FunctorException("ExceptionFactory invoked"); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java b/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java index c7e4f21..238e455 100644 --- a/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java +++ b/src/java/org/apache/commons/collections/functors/ExceptionPredicate.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,28 +23,29 @@ import org.apache.commons.collections.Predicate; /** * Predicate implementation that always throws an exception. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ * * @author Stephen Colebourne */ -public final class ExceptionPredicate implements Predicate, Serializable { +public final class ExceptionPredicate<T> implements Predicate<T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 7179106032121985545L; - + /** Singleton predicate instance */ - public static final Predicate INSTANCE = new ExceptionPredicate(); + public static final Predicate<Object> INSTANCE = new ExceptionPredicate<Object>(); /** * Factory returning the singleton instance. - * + * * @return the singleton instance * @since Commons Collections 3.1 */ - public static Predicate getInstance() { - return INSTANCE; + @SuppressWarnings("unchecked") + public static <T> Predicate<T> getInstance() { + return (Predicate<T>) INSTANCE; } /** @@ -56,13 +57,13 @@ public final class ExceptionPredicate implements Predicate, Serializable { /** * Evaluates the predicate always throwing an exception. - * + * * @param object the input object * @return never * @throws FunctorException always */ - public boolean evaluate(Object object) { + public boolean evaluate(T object) { throw new FunctorException("ExceptionPredicate invoked"); } - + } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java b/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java index c41a76f..b1bcf2c 100644 --- a/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java +++ b/src/java/org/apache/commons/collections/functors/ExceptionTransformer.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,29 +23,29 @@ import org.apache.commons.collections.Transformer; /** * Transformer implementation that always throws an exception. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ * * @author Stephen Colebourne */ -public final class ExceptionTransformer implements Transformer, Serializable { +public final class ExceptionTransformer<I, O> implements Transformer<I, O>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 7179106032121985545L; - /** Singleton predicate instance */ - public static final Transformer INSTANCE = new ExceptionTransformer(); + public static final Transformer<Object, Object> INSTANCE = new ExceptionTransformer<Object, Object>(); /** * Factory returning the singleton instance. - * + * * @return the singleton instance * @since Commons Collections 3.1 */ - public static Transformer getInstance() { - return INSTANCE; + @SuppressWarnings("unchecked") + public static <I, O> Transformer<I, O> getInstance() { + return (Transformer<I, O>) INSTANCE; } /** @@ -57,12 +57,12 @@ public final class ExceptionTransformer implements Transformer, Serializable { /** * Transforms the input to result by cloning it. - * + * * @param input the input object to transform * @return never * @throws FunctorException always */ - public Object transform(Object input) { + public O transform(I input) { throw new FunctorException("ExceptionTransformer invoked"); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/FactoryTransformer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/FactoryTransformer.java b/src/java/org/apache/commons/collections/functors/FactoryTransformer.java index 97359b3..340aa00 100644 --- a/src/java/org/apache/commons/collections/functors/FactoryTransformer.java +++ b/src/java/org/apache/commons/collections/functors/FactoryTransformer.java @@ -29,13 +29,13 @@ import org.apache.commons.collections.Transformer; * * @author Stephen Colebourne */ -public class FactoryTransformer implements Transformer, Serializable { +public class FactoryTransformer<I, O> implements Transformer<I, O>, Serializable { /** Serial version UID */ private static final long serialVersionUID = -6817674502475353160L; /** The factory to wrap */ - private final Factory iFactory; + private final Factory<? extends O> iFactory; /** * Factory method that performs validation. @@ -44,11 +44,11 @@ public class FactoryTransformer implements Transformer, Serializable { * @return the <code>factory</code> transformer * @throws IllegalArgumentException if the factory is null */ - public static Transformer getInstance(Factory factory) { + public static <I, O> Transformer<I, O> getInstance(Factory<? extends O> factory) { if (factory == null) { throw new IllegalArgumentException("Factory must not be null"); } - return new FactoryTransformer(factory); + return new FactoryTransformer<I, O>(factory); } /** @@ -57,7 +57,7 @@ public class FactoryTransformer implements Transformer, Serializable { * * @param factory the factory to call, not null */ - public FactoryTransformer(Factory factory) { + public FactoryTransformer(Factory<? extends O> factory) { super(); iFactory = factory; } @@ -69,7 +69,7 @@ public class FactoryTransformer implements Transformer, Serializable { * @param input the input object to transform * @return the transformed result */ - public Object transform(Object input) { + public O transform(I input) { return iFactory.create(); } @@ -79,7 +79,7 @@ public class FactoryTransformer implements Transformer, Serializable { * @return the factory * @since Commons Collections 3.1 */ - public Factory getFactory() { + public Factory<? extends O> getFactory() { return iFactory; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/FalsePredicate.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/FalsePredicate.java b/src/java/org/apache/commons/collections/functors/FalsePredicate.java index 4560b9b..c007844 100644 --- a/src/java/org/apache/commons/collections/functors/FalsePredicate.java +++ b/src/java/org/apache/commons/collections/functors/FalsePredicate.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,28 +22,40 @@ import org.apache.commons.collections.Predicate; /** * Predicate implementation that always returns false. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ * * @author Stephen Colebourne */ -public final class FalsePredicate implements Predicate, Serializable { +public final class FalsePredicate<T> implements Predicate<T>, Serializable { /** Serial version UID */ private static final long serialVersionUID = 7533784454832764388L; - + /** Singleton predicate instance */ - public static final Predicate INSTANCE = new FalsePredicate(); + public static final Predicate<Object> INSTANCE = new FalsePredicate<Object>(); /** - * Factory returning the singleton instance. - * + * Get a typed instance. + * * @return the singleton instance * @since Commons Collections 3.1 + * @deprecated use {@link #falsePredicate()} instead. + */ + public static <T> Predicate<T> getInstance() { + return FalsePredicate.<T>falsePredicate(); + } + + /** + * Get a typed instance. + * + * @return the singleton instance + * @since Commons Collections 5 */ - public static Predicate getInstance() { - return INSTANCE; + @SuppressWarnings("unchecked") + public static <T> Predicate<T> falsePredicate() { + return (Predicate<T>) INSTANCE; } /** @@ -55,11 +67,11 @@ public final class FalsePredicate implements Predicate, Serializable { /** * Evaluates the predicate returning false always. - * + * * @param object the input object * @return false always */ - public boolean evaluate(Object object) { + public boolean evaluate(T object) { return false; } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/java/org/apache/commons/collections/functors/ForClosure.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/functors/ForClosure.java b/src/java/org/apache/commons/collections/functors/ForClosure.java index 9ebad4a..b77ed5e 100644 --- a/src/java/org/apache/commons/collections/functors/ForClosure.java +++ b/src/java/org/apache/commons/collections/functors/ForClosure.java @@ -28,7 +28,7 @@ import org.apache.commons.collections.Closure; * * @author Stephen Colebourne */ -public class ForClosure implements Closure, Serializable { +public class ForClosure<E> implements Closure<E>, Serializable { /** Serial version UID */ private static final long serialVersionUID = -1190120533393621674L; @@ -36,7 +36,7 @@ public class ForClosure implements Closure, Serializable { /** The number of times to loop */ private final int iCount; /** The closure to call */ - private final Closure iClosure; + private final Closure<? super E> iClosure; /** * Factory method that performs validation. @@ -48,14 +48,15 @@ public class ForClosure implements Closure, Serializable { * @param closure the closure to execute, not null * @return the <code>for</code> closure */ - public static Closure getInstance(int count, Closure closure) { + @SuppressWarnings("unchecked") + public static <E> Closure<E> getInstance(int count, Closure<? super E> closure) { if (count <= 0 || closure == null) { - return NOPClosure.INSTANCE; + return NOPClosure.<E>getInstance(); } if (count == 1) { - return closure; + return (Closure<E>) closure; } - return new ForClosure(count, closure); + return new ForClosure<E>(count, closure); } /** @@ -65,7 +66,7 @@ public class ForClosure implements Closure, Serializable { * @param count the number of times to execute the closure * @param closure the closure to execute, not null */ - public ForClosure(int count, Closure closure) { + public ForClosure(int count, Closure<? super E> closure) { super(); iCount = count; iClosure = closure; @@ -76,7 +77,7 @@ public class ForClosure implements Closure, Serializable { * * @param input the input object */ - public void execute(Object input) { + public void execute(E input) { for (int i = 0; i < iCount; i++) { iClosure.execute(input); } @@ -88,7 +89,7 @@ public class ForClosure implements Closure, Serializable { * @return the closure * @since Commons Collections 3.1 */ - public Closure getClosure() { + public Closure<? super E> getClosure() { return iClosure; }