You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/01 11:26:32 UTC

svn commit: r381974 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Collections.java test/java/org/apache/harmony/tests/java/util/CollectionsTest.java

Author: tellison
Date: Wed Mar  1 02:26:29 2006
New Revision: 381974

URL: http://svn.apache.org/viewcvs?rev=381974&view=rev
Log:
Apply patch HARMONY-140 ([classlib][luni] java.util.Collections.frequency(Collection,Object) implementation)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java?rev=381974&r1=381973&r2=381974&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java Wed Mar  1 02:26:29 2006
@@ -138,13 +138,28 @@
 
 	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 {
@@ -1579,13 +1594,43 @@
 	}
 
 	/**
-	 * A Comparator which reverses the natural order of the elements.
-	 * 
-	 * @return a Comparator
-	 */
-	public static Comparator reverseOrder() {
-		return new ReverseComparator();
-	}
+     * <p>
+     * A Comparator which reverses the natural order of the elements. The
+     * <code>Comparator</code> that's returned is serializable.
+     * </p>
+     * 
+     * @return A <code>Comparator</code> instance.
+     * 
+     * @see Comparator
+     * @see Comparable
+     */
+    public static Comparator reverseOrder() {
+        return new ReverseComparator();
+    }
+
+    /**
+     * <p>
+     * Returns a {@link Comparator} that reverses the order of the
+     * <code>Comparator</code> passed. If the <code>Comparatoer</code>
+     * passed is <code>null</code>, then this method is equivalent to
+     * {@link #reverseOrder()}.
+     * </p>
+     * 
+     * <p>
+     * The <code>Comparator</code> that's returned is serializable if the
+     * <code>Comparator</code> passed is serializable or <code>null</code>.
+     * </p>
+     * 
+     * @param c The <code>Comparator</code> to reverse or <code>null</code>.
+     * @return A <code>Comparator</code> 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);
 	}
+    
+    /**
+     * <p>
+     * Returns the number of elements in the <code>Collection</code> that
+     * match the <code>Object</code> passed. If the <code>Object</code> is
+     * <code>null</code>, then the number of <code>null</code> elements is
+     * returned.
+     * </p>
+     * 
+     * @param c The <code>Collection</code> to search.
+     * @param o The <code>Object</code> to search for.
+     * @return The number of matching elements.
+     * @throws NullPointerException if the <code>Collection</code> parameter
+     *         is <code>null</code>.
+     *         
+     * @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;
+    }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java?rev=381974&r1=381973&r2=381974&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/CollectionsTest.java Wed Mar  1 02:26:29 2006
@@ -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,<any>) 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));
+    }
 }