[Japi-cvs] SF.net SVN: japi:[1278] libs/util/trunk/src
Status: Beta
Brought to you by:
christianhujer
From: <chr...@us...> - 2009-05-09 18:30:50
|
Revision: 1278 http://japi.svn.sourceforge.net/japi/?rev=1278&view=rev Author: christianhujer Date: 2009-05-09 18:30:43 +0000 (Sat, 09 May 2009) Log Message: ----------- Added collect method to Collections2. Modified Paths: -------------- libs/util/trunk/src/prj/net/sf/japi/util/Collections2.java Added Paths: ----------- libs/util/trunk/src/tst/test/net/sf/japi/util/Collections2Test.java Modified: libs/util/trunk/src/prj/net/sf/japi/util/Collections2.java =================================================================== --- libs/util/trunk/src/prj/net/sf/japi/util/Collections2.java 2009-05-09 18:30:43 UTC (rev 1277) +++ libs/util/trunk/src/prj/net/sf/japi/util/Collections2.java 2009-05-09 18:30:43 UTC (rev 1278) @@ -180,4 +180,16 @@ } } + /** Collects items from an Iterator into a Collection. + * @param c Collection to which the items shall be collected. + * @param iterator Iterator from which to collect items. + * @return <var>c</var> after the items from <var>iterator</var> have been added to it. + */ + public static <T, C extends Collection<T>> C collect(final C c, final Iterator<T> iterator) { + while (iterator.hasNext()) { + c.add(iterator.next()); + } + return c; + } + } // class Collections2 Added: libs/util/trunk/src/tst/test/net/sf/japi/util/Collections2Test.java =================================================================== --- libs/util/trunk/src/tst/test/net/sf/japi/util/Collections2Test.java (rev 0) +++ libs/util/trunk/src/tst/test/net/sf/japi/util/Collections2Test.java 2009-05-09 18:30:43 UTC (rev 1278) @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2009 Christian Hujer. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package test.net.sf.japi.util; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import net.sf.japi.util.Collections2; +import org.junit.Assert; +import org.junit.Test; + +/** Test for {@link Collections2}. + * @author <a href="mailto:ch...@ri...">Christian Hujer</a> + */ +public class Collections2Test { + + /** Test case for {@link Collections2#collect(Collection, Iterator)}. */ + @Test + public void testCollect() { + final Set<String> original = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList("foo", "bar"))); + final Set<String> collected = Collections2.collect(new HashSet<String>(), original.iterator()); + Assert.assertEquals("Collect must collect all elements.", original, collected); + } + +} Property changes on: libs/util/trunk/src/tst/test/net/sf/japi/util/Collections2Test.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + LF This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |