commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r1002394 - in /commons/proper/io/trunk/src: java/org/apache/commons/io/filefilter/FileFilterUtils.java test/org/apache/commons/io/filefilter/FileFilterTestCase.java
Date Tue, 28 Sep 2010 22:52:54 GMT
Author: niallp
Date: Tue Sep 28 22:52:54 2010
New Revision: 1002394

URL: http://svn.apache.org/viewvc?rev=1002394&view=rev
Log:
IO-229 FileFilterUtils - add varargs methods for AndFileFilter and OrFileFilter - thanks to
Michael Wooten

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=1002394&r1=1002393&r2=1002394&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
Tue Sep 28 22:52:54 2010
@@ -19,7 +19,9 @@ package org.apache.commons.io.filefilter
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FilenameFilter;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
 
 import org.apache.commons.io.IOCase;
 
@@ -136,6 +138,7 @@ public class FileFilterUtils {
      * @param filter1  the first filter
      * @param filter2  the second filter
      * @return a filter that ANDs the two specified filters
+     * @deprecated use {@link #and(IOFileFilter...)}
      */
     public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2)
{
         return new AndFileFilter(filter1, filter2);
@@ -147,12 +150,64 @@ public class FileFilterUtils {
      * @param filter1  the first filter
      * @param filter2  the second filter
      * @return a filter that ORs the two specified filters
+     * @deprecated use {@link #or(IOFileFilter...)}
      */
     public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2) {
         return new OrFileFilter(filter1, filter2);
     }
 
     /**
+     * Returns a filter that ANDs the specified filters.
+     * 
+     * @param filters the IOFileFilters that will be ANDed together.
+     * @return a filter that ANDs the specified filters
+     * 
+     * @throws IllegalArgumentException if the filters are null or contain a 
+     *         null value.
+     * @since Commons IO 2.0
+     */
+    public static IOFileFilter and(IOFileFilter... filters) {
+        return new AndFileFilter(toList(filters));
+    }
+
+    /**
+     * Returns a filter that ORs the specified filters.
+     * 
+     * @param filters the IOFileFilters that will be ORed together.
+     * @return a filter that ORs the specified filters
+     * 
+     * @throws IllegalArgumentException if the filters are null or contain a 
+     *         null value.
+     * @since Commons IO 2.0
+     */
+    public static IOFileFilter or(IOFileFilter... filters) {
+        return new OrFileFilter(toList(filters));
+    }
+
+    /**
+     * Create a List of file filters.
+     *
+     * @param filters The file filters
+     * @return The list of file filters
+     * @throws IllegalArgumentException if the filters are null or contain a 
+     *         null value.
+     * @since Commons IO 2.0
+     */
+    public static List<IOFileFilter> toList(IOFileFilter... filters) {
+        if (filters == null) {
+            throw new IllegalArgumentException("The filters must not be null");
+        }
+        List<IOFileFilter> list = new ArrayList<IOFileFilter>(filters.length);
+        for (int i = 0; i < filters.length; i++) {
+            if (filters[i] == null) {
+                throw new IllegalArgumentException("The filter[" + i + "] is null");
+            }
+            list.add(filters[i]);
+        }
+        return list;
+    }
+
+    /**
      * Returns a filter that NOTs the specified filter.
      * 
      * @param filter  the filter to invert

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=1002394&r1=1002393&r2=1002394&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
Tue Sep 28 22:52:54 2010
@@ -482,6 +482,24 @@ public class FileFilterTestCase extends 
         OrFileFilter f = new OrFileFilter((List<IOFileFilter>) null);
         assertEquals(true, f.getFileFilters().isEmpty());
     }
+    public void testFileFilterUtils_and() throws Exception {
+        IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
+        IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
+        assertFiltering(FileFilterUtils.and(trueFilter, trueFilter, trueFilter), new File("foo.test"),
true);
+        assertFiltering(FileFilterUtils.and(trueFilter, falseFilter, trueFilter), new File("foo.test"),
false);
+        assertFiltering(FileFilterUtils.and(falseFilter, trueFilter), new File("foo.test"),
false);
+        assertFiltering(FileFilterUtils.and(falseFilter, falseFilter), new File("foo.test"),
false);
+    }
+
+    public void testFileFilterUtils_or() throws Exception {
+        IOFileFilter trueFilter = TrueFileFilter.INSTANCE;
+        IOFileFilter falseFilter = FalseFileFilter.INSTANCE;
+        File testFile = new File( "foo.test" );
+        assertFiltering(FileFilterUtils.or(trueFilter, trueFilter), testFile, true);
+        assertFiltering(FileFilterUtils.or(trueFilter, trueFilter, falseFilter), testFile,
true);
+        assertFiltering(FileFilterUtils.or(falseFilter, trueFilter), testFile, true);
+        assertFiltering(FileFilterUtils.or(falseFilter, falseFilter, falseFilter), testFile,
false);
+    }
 
     @SuppressWarnings("deprecation")
     public void testDeprecatedWildcard() throws Exception {



Mime
View raw message