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 {
}
//-----------------------------------------------------------------------
+
+ /**
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <p>
+ * The {...@link Set} returned by this method is not guaranteed to be
thread safe.
+ * </p>
+ *
+ * <pre>
+ * Set<File> allFiles = ...
+ * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+ * FileFilterUtils.suffixFileFilter(".java"));
+ * </pre>
+ * @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 <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> 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<File> acceptedFiles = new ArrayList<File>();
+ 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()]);
+ }
+
+ /**
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <p>
+ * The {...@link Set} returned by this method is not guaranteed to be
thread safe.
+ * </p>
+ *
+ * <pre>
+ * Set<File> allFiles = ...
+ * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+ * FileFilterUtils.suffixFileFilter(".java"));
+ * </pre>
+ * @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 <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> value.
+ *
+ * @since Commons IO 2.0
+ */
+ public static File[] filter(IOFileFilter filter, Iterable<File> files) {
+ List<File> acceptedFiles = filterList(filter, files);
+ return acceptedFiles.toArray(new File[acceptedFiles.size()]);
+ }
+
+ /**
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <p>
+ * The {...@link List} returned by this method is not guaranteed to be
thread safe.
+ * </p>
+ *
+ * <pre>
+ * List<File> filesAndDirectories = ...
+ * List<File> directories =
FileFilterUtils.filterList(filesAndDirectories,
+ * FileFilterUtils.directoryFileFilter());
+ * </pre>
+ * @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 <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> value.
+ * @since Commons IO 2.0
+ */
+ public static List<File> filterList(IOFileFilter filter, Iterable<File>
files) {
+ return filter(filter, files, new ArrayList<File>());
+ }
+
+ /**
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <p>
+ * The {...@link List} returned by this method is not guaranteed to be
thread safe.
+ * </p>
+ *
+ * <pre>
+ * List<File> filesAndDirectories = ...
+ * List<File> directories =
FileFilterUtils.filterList(filesAndDirectories,
+ * FileFilterUtils.directoryFileFilter());
+ * </pre>
+ * @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 <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> value.
+ * @since Commons IO 2.0
+ */
+ public static List<File> filterList(IOFileFilter filter, File... files) {
+ File[] acceptedFiles = filter(filter, files);
+ return Arrays.asList(acceptedFiles);
+ }
+
+ /**
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <p>
+ * The {...@link Set} returned by this method is not guaranteed to be
thread safe.
+ * </p>
+ *
+ * <pre>
+ * Set<File> allFiles = ...
+ * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+ * FileFilterUtils.suffixFileFilter(".java"));
+ * </pre>
+ * @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 <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> value.
+ *
+ * @since Commons IO 2.0
+ */
+ public static Set<File> filterSet(IOFileFilter filter, File... files) {
+ File[] acceptedFiles = filter(filter, files);
+ return new HashSet<File>(Arrays.asList(acceptedFiles));
+ }
+
+ /**
+ * <p>
+ * 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.
+ * </p>
+ *
+ * <p>
+ * The {...@link Set} returned by this method is not guaranteed to be
thread safe.
+ * </p>
+ *
+ * <pre>
+ * Set<File> allFiles = ...
+ * Set<File> javaFiles = FileFilterUtils.filterSet(allFiles,
+ * FileFilterUtils.suffixFileFilter(".java"));
+ * </pre>
+ * @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 <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> value.
+ *
+ * @since Commons IO 2.0
+ */
+ public static Set<File> filterSet(IOFileFilter filter, Iterable<File>
files) {
+ return filter(filter, files, new HashSet<File>());
+ }
+
+ /**
+ * <p>
+ * Applies an {...@link IOFileFilter} to the provided {...@link File}
+ * objects and appends the accepted files to the other supplied
collection.
+ * </p>
+ *
+ * <pre>
+ * List<File> files = ...
+ * List<File> directories = FileFilterUtils.filterList(files,
+ * FileFilterUtils.sizeFileFilter(FileUtils.FIFTY_MB),
+ * new ArrayList<File>());
+ * </pre>
+ * @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 <T> the type of the file collection.
+ * @return a subset of <code>files</code> that is accepted by the
+ * file filter.
+ * @throws IllegalArgumentException if the filter is <code>null</code>
+ * or <code>files</code> contains a <code>null</code> value.
+ */
+ private static <T extends Collection<File>> T filter(IOFileFilter filter,
+ Iterable<File> 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<File> 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 <code>null</code> parameters and <code>null</code> 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<File> fileList = Arrays.asList(fileA, fileB);
+
+ IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
+
+ List<File> 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<File> filteredList = FileFilterUtils.filterList(filter, fileA,
fileB);
+
+ assertTrue(filteredList.contains(fileA));
+ assertFalse(filteredList.contains(fileB));
+ }
+
+ /**
+ * Test method for {...@link FileFilterUtils#filterList(IOFileFilter,
List)}
+ * that tests <code>null</code> parameters and <code>null</code> elements
+ * in the provided list.
+ */
+ public void testFilterListNullParameters() {
+ try {
+ FileFilterUtils.filterList(null, Collections.<File>emptyList());
+ fail();
+ } catch (IllegalArgumentException iae) {
+ // Test passes, exception thrown for null filter
+ }
+
+ IOFileFilter filter = FileFilterUtils.trueFileFilter();
+ try {
+ FileFilterUtils.filterList(filter, Arrays.<File>asList((File)
null));
+ fail();
+ } catch (IllegalArgumentException iae) {
+ // Test passes, exception thrown for list containing null
+ }
+
+ List<File> filteredList = FileFilterUtils.filterList(filter,
(List<File>)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<File> fileList = new HashSet<File>(Arrays.asList(fileA, fileB));
+
+ IOFileFilter filter = FileFilterUtils.nameFileFilter("A");
+
+ Set<File> 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<File> filteredSet = FileFilterUtils.filterSet(filter, fileA,
fileB);
+
+ assertTrue(filteredSet.contains(fileA));
+ assertFalse(filteredSet.contains(fileB));
+ }
+
+ /**
+ * Test method for {...@link FileFilterUtils#filterSet(IOFileFilter, Set)}
+ * that tests <code>null</code> parameters and <code>null</code> elements
+ * in the provided set.
+ */
+ public void testFilterSetNullParameters() {
+ try {
+ FileFilterUtils.filterSet(null, Collections.<File>emptySet());
+ fail();
+ } catch (IllegalArgumentException iae) {
+ // Test passes, exception thrown for null filter
+ }
+
+ IOFileFilter filter = FileFilterUtils.trueFileFilter();
+ try {
+ FileFilterUtils.filterSet(filter, new
HashSet<File>(Arrays.<File>asList((File) null)));
+ fail();
+ } catch (IllegalArgumentException iae) {
+ // Test passes, exception thrown for set containing null
+ }
+
+ Set<File> filteredSet = FileFilterUtils.filterSet(filter,
(Set<File>)null);
+ assertEquals(0, filteredSet.size());
+ }
+
}