You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2009/09/11 19:48:56 UTC

svn commit: r813951 - in /commons/proper/collections/branches/collections_jdk5_branch/src: java/org/apache/commons/collections/CollectionUtils.java test/org/apache/commons/collections/TestCollectionUtils.java

Author: mbenson
Date: Fri Sep 11 17:48:55 2009
New Revision: 813951

URL: http://svn.apache.org/viewvc?rev=813951&view=rev
Log:
[COLLECTIONS-286] CollectionsUtils.extractSingleton

Modified:
    commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java
    commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java

Modified: commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java?rev=813951&r1=813950&r2=813951&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/java/org/apache/commons/collections/CollectionUtils.java Fri Sep 11 17:48:55 2009
@@ -1291,4 +1291,17 @@
         return TransformedCollection.decorate(collection, transformer);
     }
 
+    /**
+     * Extract the lone element of the specified Collection.
+     * @param <E> collection type
+     * @param collection to read
+     * @return sole member of collection
+     * @throws IllegalArgumentException if collection is null/empty or contains more than one element
+     */
+    public static <E> E extractSingleton(Collection<E> collection) {
+        if (collection == null || collection.size() != 1) {
+            throw new IllegalArgumentException("Can extract singleton only when collection size == 1");
+        }
+        return collection.iterator().next();
+    }
 }

Modified: commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java?rev=813951&r1=813950&r2=813951&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java Fri Sep 11 17:48:55 2009
@@ -1414,6 +1414,29 @@
         assertEquals(collectionA, Arrays.asList(a));
     }
 
+    @Test
+    public void extractSingleton() {
+        ArrayList<String> coll = null;
+        try {
+            CollectionUtils.extractSingleton(coll);
+            fail("expected IllegalArgumentException from extractSingleton(null)");
+        } catch (IllegalArgumentException e) {
+        }
+        coll = new ArrayList<String>();
+        try {
+            CollectionUtils.extractSingleton(coll);
+            fail("expected IllegalArgumentException from extractSingleton(empty)");
+        } catch (IllegalArgumentException e) {
+        }
+        coll.add("foo");
+        assertEquals("foo", CollectionUtils.extractSingleton(coll));
+        coll.add("bar");
+        try {
+            CollectionUtils.extractSingleton(coll);
+            fail("expected IllegalArgumentException from extractSingleton(size == 2)");
+        } catch (IllegalArgumentException e) {
+        }
+    }
 
     /**
      * Records the next object returned for a mock iterator