Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 70905 invoked from network); 29 Sep 2010 03:23:24 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 29 Sep 2010 03:23:24 -0000 Received: (qmail 41143 invoked by uid 500); 29 Sep 2010 03:23:24 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 40783 invoked by uid 500); 29 Sep 2010 03:23:21 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 40776 invoked by uid 99); 29 Sep 2010 03:23:20 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Sep 2010 03:23:20 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Sep 2010 03:23:19 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 127A723889CB; Wed, 29 Sep 2010 03:22:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1002454 - in /commons/proper/io/trunk/src: java/org/apache/commons/io/filefilter/FileFilterUtils.java test/org/apache/commons/io/filefilter/FileFilterTestCase.java Date: Wed, 29 Sep 2010 03:22:57 -0000 To: commits@commons.apache.org From: niallp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100929032257.127A723889CB@eris.apache.org> Author: niallp Date: Wed Sep 29 03:22:56 2010 New Revision: 1002454 URL: http://svn.apache.org/viewvc?rev=1002454&view=rev Log: IO-198 FileFilterUtils - add ability to apply file filters to collections and arrays - thanks to Michael Wooten for the patch Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java?rev=1002454&r1=1002453&r2=1002454&view=diff ============================================================================== --- commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java (original) +++ commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java Wed Sep 29 03:22:56 2010 @@ -20,8 +20,12 @@ import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.commons.io.IOCase; @@ -47,6 +51,243 @@ public class FileFilterUtils { } //----------------------------------------------------------------------- + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects. The resulting array is a subset of the original file list that + * matches the provided filter. + *

+ * + *

+ * The {@link Set} returned by this method is not guaranteed to be thread safe. + *

+ * + *
+     * Set<File> allFiles = ...
+     * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+     *     FileFilterUtils.suffixFileFilter(".java"));
+     * 
+ * @param filter the filter to apply to the set of files. + * @param files the array of files to apply the filter to. + * + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + * + * @since Commons IO 2.0 + */ + public static File[] filter(IOFileFilter filter, File... files) { + if (filter == null) { + throw new IllegalArgumentException("file filter is null"); + } + if (files == null) { + return new File[0]; + } + List acceptedFiles = new ArrayList(); + for (File file : files) { + if (file == null) { + throw new IllegalArgumentException("file array contains null"); + } + if (filter.accept(file)) { + acceptedFiles.add(file); + } + } + return acceptedFiles.toArray(new File[acceptedFiles.size()]); + } + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects. The resulting array is a subset of the original file list that + * matches the provided filter. + *

+ * + *

+ * The {@link Set} returned by this method is not guaranteed to be thread safe. + *

+ * + *
+     * Set<File> allFiles = ...
+     * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+     *     FileFilterUtils.suffixFileFilter(".java"));
+     * 
+ * @param filter the filter to apply to the set of files. + * @param files the array of files to apply the filter to. + * + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + * + * @since Commons IO 2.0 + */ + public static File[] filter(IOFileFilter filter, Iterable files) { + List acceptedFiles = filterList(filter, files); + return acceptedFiles.toArray(new File[acceptedFiles.size()]); + } + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects. The resulting list is a subset of the original files that + * matches the provided filter. + *

+ * + *

+ * The {@link List} returned by this method is not guaranteed to be thread safe. + *

+ * + *
+     * List<File> filesAndDirectories = ...
+     * List<File> directories = FileFilterUtils.filterList(filesAndDirectories,
+     *     FileFilterUtils.directoryFileFilter());
+     * 
+ * @param filter the filter to apply to each files in the list. + * @param files the collection of files to apply the filter to. + * + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + * @since Commons IO 2.0 + */ + public static List filterList(IOFileFilter filter, Iterable files) { + return filter(filter, files, new ArrayList()); + } + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects. The resulting list is a subset of the original files that + * matches the provided filter. + *

+ * + *

+ * The {@link List} returned by this method is not guaranteed to be thread safe. + *

+ * + *
+     * List<File> filesAndDirectories = ...
+     * List<File> directories = FileFilterUtils.filterList(filesAndDirectories,
+     *     FileFilterUtils.directoryFileFilter());
+     * 
+ * @param filter the filter to apply to each files in the list. + * @param files the collection of files to apply the filter to. + * + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + * @since Commons IO 2.0 + */ + public static List filterList(IOFileFilter filter, File... files) { + File[] acceptedFiles = filter(filter, files); + return Arrays.asList(acceptedFiles); + } + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects. The resulting set is a subset of the original file list that + * matches the provided filter. + *

+ * + *

+ * The {@link Set} returned by this method is not guaranteed to be thread safe. + *

+ * + *
+     * Set<File> allFiles = ...
+     * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+     *     FileFilterUtils.suffixFileFilter(".java"));
+     * 
+ * @param filter the filter to apply to the set of files. + * @param files the collection of files to apply the filter to. + * + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + * + * @since Commons IO 2.0 + */ + public static Set filterSet(IOFileFilter filter, File... files) { + File[] acceptedFiles = filter(filter, files); + return new HashSet(Arrays.asList(acceptedFiles)); + } + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects. The resulting set is a subset of the original file list that + * matches the provided filter. + *

+ * + *

+ * The {@link Set} returned by this method is not guaranteed to be thread safe. + *

+ * + *
+     * Set<File> allFiles = ...
+     * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+     *     FileFilterUtils.suffixFileFilter(".java"));
+     * 
+ * @param filter the filter to apply to the set of files. + * @param files the collection of files to apply the filter to. + * + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + * + * @since Commons IO 2.0 + */ + public static Set filterSet(IOFileFilter filter, Iterable files) { + return filter(filter, files, new HashSet()); + } + + /** + *

+ * Applies an {@link IOFileFilter} to the provided {@link File} + * objects and appends the accepted files to the other supplied collection. + *

+ * + *
+     * List<File> files = ...
+     * List<File> directories = FileFilterUtils.filterList(files,
+     *     FileFilterUtils.sizeFileFilter(FileUtils.FIFTY_MB), 
+     *         new ArrayList<File>());
+     * 
+ * @param filter the filter to apply to the collection of files. + * @param files the collection of files to apply the filter to. + * @param acceptedFiles the list of files to add accepted files to. + * + * @param the type of the file collection. + * @return a subset of files that is accepted by the + * file filter. + * @throws IllegalArgumentException if the filter is null + * or files contains a null value. + */ + private static > T filter(IOFileFilter filter, + Iterable files, T acceptedFiles) { + if (filter == null) { + throw new IllegalArgumentException("file filter is null"); + } + if (files != null) { + for (File file : files) { + if (file == null) { + throw new IllegalArgumentException("file collection contains null"); + } + if (filter.accept(file)) { + acceptedFiles.add(file); + } + } + } + return acceptedFiles; + } + /** * Returns a filter that returns true if the filename starts with the specified text. * Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java?rev=1002454&r1=1002453&r2=1002454&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java (original) +++ commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java Wed Sep 29 03:22:56 2010 @@ -22,8 +22,11 @@ import java.io.FilenameFilter; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Date; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; @@ -1055,4 +1058,181 @@ public class FileFilterTestCase extends assertFiltering(filter, randomFileB, false); assertFiltering(filter, dir, false); } + + /** + * Test method for {@link FileFilterUtils#filterList(IOFileFilter, List)} + * that tests that the method properly filters files from the list. + */ + public void testFilterArray() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + + IOFileFilter filter = FileFilterUtils.nameFileFilter("A"); + + File[] filtered = FileFilterUtils.filter(filter, fileA, fileB); + + assertEquals(1, filtered.length); + assertEquals(fileA, filtered[0]); + } + + /** + * Test method for {@link FileFilterUtils#filterList(IOFileFilter, List)} + * that tests that the method properly filters files from the list. + */ + public void testFilterArray_fromList() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + List fileList = Arrays.asList(fileA, fileB); + + IOFileFilter filter = FileFilterUtils.nameFileFilter("A"); + + File[] filtered = FileFilterUtils.filter(filter, fileList); + + assertEquals(1, filtered.length); + assertEquals(fileA, filtered[0]); + } + + /** + * Test method for {@link FileFilterUtils#filterList(IOFileFilter, List)} + * that tests null parameters and null elements + * in the provided list. + */ + public void testFilterArrayNullParameters() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + try { + FileFilterUtils.filter(null, fileA, fileB); + fail(); + } catch (IllegalArgumentException iae) { + // Test passes, exception thrown for null filter + } + + IOFileFilter filter = FileFilterUtils.trueFileFilter(); + try { + FileFilterUtils.filter(filter, fileA, null); + fail(); + } catch (IllegalArgumentException iae) { + // Test passes, exception thrown for list containing null + } + + File[] filtered = FileFilterUtils.filter(filter, (File[])null); + assertEquals(0, filtered.length); + } + + /** + * Test method for {@link FileFilterUtils#filterList(IOFileFilter, List)} + * that tests that the method properly filters files from the list. + */ + public void testFilterList() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + List fileList = Arrays.asList(fileA, fileB); + + IOFileFilter filter = FileFilterUtils.nameFileFilter("A"); + + List filteredList = FileFilterUtils.filterList(filter, fileList); + + assertTrue(filteredList.contains(fileA)); + assertFalse(filteredList.contains(fileB)); + } + + /** + * Test method for {@link FileFilterUtils#filterList(IOFileFilter, List)} + * that tests that the method properly filters files from the list. + */ + public void testFilterList_fromArray() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + + IOFileFilter filter = FileFilterUtils.nameFileFilter("A"); + + List filteredList = FileFilterUtils.filterList(filter, fileA, fileB); + + assertTrue(filteredList.contains(fileA)); + assertFalse(filteredList.contains(fileB)); + } + + /** + * Test method for {@link FileFilterUtils#filterList(IOFileFilter, List)} + * that tests null parameters and null elements + * in the provided list. + */ + public void testFilterListNullParameters() { + try { + FileFilterUtils.filterList(null, Collections.emptyList()); + fail(); + } catch (IllegalArgumentException iae) { + // Test passes, exception thrown for null filter + } + + IOFileFilter filter = FileFilterUtils.trueFileFilter(); + try { + FileFilterUtils.filterList(filter, Arrays.asList((File) null)); + fail(); + } catch (IllegalArgumentException iae) { + // Test passes, exception thrown for list containing null + } + + List filteredList = FileFilterUtils.filterList(filter, (List)null); + assertEquals(0, filteredList.size()); + } + + /** + * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, Set)} + * that tests that the method properly filters files from the set. + */ + public void testFilterSet() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + Set fileList = new HashSet(Arrays.asList(fileA, fileB)); + + IOFileFilter filter = FileFilterUtils.nameFileFilter("A"); + + Set filteredSet = FileFilterUtils.filterSet(filter, fileList); + + assertTrue(filteredSet.contains(fileA)); + assertFalse(filteredSet.contains(fileB)); + } + + /** + * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, Set)} + * that tests that the method properly filters files from the set. + */ + public void testFilterSet_fromArray() throws Exception { + File fileA = newFile("A"); + File fileB = newFile("B"); + + IOFileFilter filter = FileFilterUtils.nameFileFilter("A"); + + Set filteredSet = FileFilterUtils.filterSet(filter, fileA, fileB); + + assertTrue(filteredSet.contains(fileA)); + assertFalse(filteredSet.contains(fileB)); + } + + /** + * Test method for {@link FileFilterUtils#filterSet(IOFileFilter, Set)} + * that tests null parameters and null elements + * in the provided set. + */ + public void testFilterSetNullParameters() { + try { + FileFilterUtils.filterSet(null, Collections.emptySet()); + fail(); + } catch (IllegalArgumentException iae) { + // Test passes, exception thrown for null filter + } + + IOFileFilter filter = FileFilterUtils.trueFileFilter(); + try { + FileFilterUtils.filterSet(filter, new HashSet(Arrays.asList((File) null))); + fail(); + } catch (IllegalArgumentException iae) { + // Test passes, exception thrown for set containing null + } + + Set filteredSet = FileFilterUtils.filterSet(filter, (Set)null); + assertEquals(0, filteredSet.size()); + } + }