[COLLECTIONS-286] CollectionsUtils.extractSingleton git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/collections_jdk5_branch@813951 13f79535-47bb-0310-9956-ffa450edef68
Project: http://git-wip-us.apache.org/repos/asf/commons-collections/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-collections/commit/0a52bf59 Tree: http://git-wip-us.apache.org/repos/asf/commons-collections/tree/0a52bf59 Diff: http://git-wip-us.apache.org/repos/asf/commons-collections/diff/0a52bf59 Branch: refs/heads/collections_jdk5_branch Commit: 0a52bf59a788abfd5cc64c3cbe0cc73cbbfb4cfd Parents: dabd240 Author: Matthew Jason Benson <mben...@apache.org> Authored: Fri Sep 11 17:48:55 2009 +0000 Committer: Matthew Jason Benson <mben...@apache.org> Committed: Fri Sep 11 17:48:55 2009 +0000 ---------------------------------------------------------------------- .../commons/collections/CollectionUtils.java | 13 +++++++++++ .../collections/TestCollectionUtils.java | 23 ++++++++++++++++++++ 2 files changed, 36 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-collections/blob/0a52bf59/src/java/org/apache/commons/collections/CollectionUtils.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java index cf34edc..89b06e4 100644 --- a/src/java/org/apache/commons/collections/CollectionUtils.java +++ b/src/java/org/apache/commons/collections/CollectionUtils.java @@ -1291,4 +1291,17 @@ public class CollectionUtils { 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(); + } } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/0a52bf59/src/test/org/apache/commons/collections/TestCollectionUtils.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/collections/TestCollectionUtils.java b/src/test/org/apache/commons/collections/TestCollectionUtils.java index 7b613e6..6d102fb 100644 --- a/src/test/org/apache/commons/collections/TestCollectionUtils.java +++ b/src/test/org/apache/commons/collections/TestCollectionUtils.java @@ -1414,6 +1414,29 @@ public class TestCollectionUtils extends MockTestCase { 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