Index: luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java =================================================================== --- luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java (revision 379882) +++ luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java (working copy) @@ -16,8 +16,11 @@ package org.apache.harmony.tests.java.util; +import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.Comparator; import java.util.LinkedList; import java.util.List; @@ -195,4 +198,86 @@ // expected } } + + /** + * @tests java.util.Collections#frequency(java.util.Collection,Object) + */ + public void test_frequencyLjava_util_CollectionLint() { + try { + Collections.frequency(null, null); + fail("Assert 0: frequency(null,) must throw NPE"); + } catch (NullPointerException e) {} + + List strings = Arrays.asList(new String[] { "1", "2", "3", "1", "1" }); + + assertEquals("Assert 1: did not find three \"1\" strings", 3, + Collections.frequency(strings, "1")); + + assertEquals("Assert 2: did not find one \"2\" strings", 1, Collections + .frequency(strings, "2")); + + assertEquals("Assert 3: did not find three \"3\" strings", 1, + Collections.frequency(strings, "3")); + + assertEquals("Assert 4: matched on null when there are none", 0, + Collections.frequency(strings, null)); + + List objects = Arrays.asList(new Object[] { new Integer(1), null, null, + new Long(1) }); + + assertEquals("Assert 5: did not find one Integer(1)", 1, Collections + .frequency(objects, new Integer(1))); + + assertEquals("Assert 6: did not find one Long(1)", 1, Collections + .frequency(objects, new Long(1))); + + assertEquals("Assert 7: did not find two null references", 2, + Collections.frequency(objects, null)); + } + + /** + * @tests java.util.Collections#reverseOrder() + */ + public void test_reverseOrder() { + Comparator roc = Collections.reverseOrder(); + assertNotNull("Assert 0: comparator must not be null", roc); + + assertTrue("Assert 1: comparator must implement Serializable", + roc instanceof Serializable); + + String[] fixtureDesc = new String[] { "2", "1", "0" }; + String[] numbers = new String[] { "0", "1", "2" }; + Arrays.sort(numbers, roc); + assertTrue("Assert 2: the arrays are not equal, the sort failed", + Arrays.equals(fixtureDesc, numbers)); + } + + /** + * @tests java.util.Collections#reverseOrder(java.util.Comparator) + */ + public void test_reverseOrderLjava_util_Comparator() { + Comparator roc = Collections + .reverseOrder(String.CASE_INSENSITIVE_ORDER); + assertNotNull("Assert 0: comparator must not be null", roc); + + assertTrue("Assert 1: comparator must implement Serializable", + roc instanceof Serializable); + + String[] fixtureDesc = new String[] { "2", "1", "0" }; + String[] numbers = new String[] { "0", "1", "2" }; + Arrays.sort(numbers, roc); + assertTrue("Assert 2: the arrays are not equal, the sort failed", + Arrays.equals(fixtureDesc, numbers)); + + roc = Collections.reverseOrder(null); + assertNotNull("Assert 3: comparator must not be null", roc); + + assertTrue("Assert 4: comparator must implement Serializable", + roc instanceof Serializable); + + numbers = new String[] { "0", "1", "2" }; + Arrays.sort(numbers, roc); + assertTrue("Assert 5: the arrays are not equal, the sort failed", + Arrays.equals(fixtureDesc, numbers)); + } } Index: luni/src/main/java/java/util/Collections.java =================================================================== --- luni/src/main/java/java/util/Collections.java (revision 379778) +++ luni/src/main/java/java/util/Collections.java (working copy) @@ -138,14 +138,29 @@ public static final Map EMPTY_MAP = new EmptyMap(); - private static class ReverseComparator implements Comparator, Serializable { + private static final class ReverseComparator implements Comparator, Serializable { private static final long serialVersionUID = 7207038068494060240L; public int compare(Object o1, Object o2) { return -((Comparable) o1).compareTo(o2); } } + + private static final class ReverseComparatorWithComparator implements + Comparator, Serializable { + private static final long serialVersionUID = 4374092139857L; + private final Comparator comparator; + ReverseComparatorWithComparator(Comparator comparator) { + super(); + this.comparator = comparator; + } + + public int compare(Object o1, Object o2) { + return comparator.compare(o2, o1); + } + } + private static final class SingletonSet extends AbstractSet implements Serializable { private static final long serialVersionUID = 3193687207550431679L; @@ -1579,14 +1594,44 @@ } /** - * A Comparator which reverses the natural order of the elements. - * - * @return a Comparator - */ - public static Comparator reverseOrder() { - return new ReverseComparator(); - } + *

+ * A Comparator which reverses the natural order of the elements. The + * Comparator that's returned is serializable. + *

+ * + * @return A Comparator instance. + * + * @see Comparator + * @see Comparable + */ + public static Comparator reverseOrder() { + return new ReverseComparator(); + } + /** + *

+ * Returns a {@link Comparator} that reverses the order of the + * Comparator passed. If the Comparatoer + * passed is null, then this method is equivalent to + * {@link #reverseOrder()}. + *

+ * + *

+ * The Comparator that's returned is serializable if the + * Comparator passed is serializable or null. + *

+ * + * @param c The Comparator to reverse or null. + * @return A Comparator instance. + * @see Comparator + * @since 1.5 + */ + public static Comparator reverseOrder(Comparator c) { + if (c == null) + return reverseOrder(); + return new ReverseComparatorWithComparator(c); + } + /** * Moves every element of the List to a random new position in the list. * @@ -2151,4 +2196,35 @@ throw new NullPointerException(); return new UnmodifiableSortedSet(set); } + + /** + *

+ * Returns the number of elements in the Collection that + * match the Object passed. If the Object is + * null, then the number of null elements is + * returned. + *

+ * + * @param c The Collection to search. + * @param o The Object to search for. + * @return The number of matching elements. + * @throws NullPointerException if the Collection parameter + * is null. + * + * @since 1.5 + */ + public static int frequency(Collection c, Object o) { + if (c == null) + throw new NullPointerException(); + if (c.isEmpty()) + return 0; + int result = 0; + Iterator itr = c.iterator(); + while (itr.hasNext()) { + Object e = itr.next(); + if (o == null ? e == null : o.equals(e)) + result++; + } + return result; + } }