You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2007/10/12 15:33:33 UTC

svn commit: r584162 - in /commons/proper/io/trunk: ./ src/java/org/apache/commons/io/filefilter/ src/test/org/apache/commons/io/filefilter/

Author: niallp
Date: Fri Oct 12 06:33:33 2007
New Revision: 584162

URL: http://svn.apache.org/viewvc?rev=584162&view=rev
Log:
IO-126 Add facility to specify case sensitivity for prefix and suffix file filters

Modified:
    commons/proper/io/trunk/RELEASE-NOTES.txt
    commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
    commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Modified: commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=584162&r1=584161&r2=584162&view=diff
==============================================================================
--- commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ commons/proper/io/trunk/RELEASE-NOTES.txt Fri Oct 12 06:33:33 2007
@@ -32,6 +32,12 @@
 - ThesholdingOuputStream [IO-121]
   - Add a reset() method which sets the count of the bytes written back to zero.
 
+- PrefixFileFilter [IO-126]
+  - Add faciltiy to specify case sensitivty on prefix matching
+
+- SuffixFileFilter [IO-126]
+  - Add faciltiy to specify case sensitivty on suffix matching
+
 
 Feedback
 --------

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java?rev=584162&r1=584161&r2=584162&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/PrefixFileFilter.java Fri Oct 12 06:33:33 2007
@@ -18,6 +18,7 @@
 
 import java.io.File;
 import java.util.List;
+import org.apache.commons.io.IOCase;
 
 /**
  * Filters filenames for a certain prefix.
@@ -46,6 +47,9 @@
     /** The filename prefixes to search for */
     private String[] prefixes;
 
+    /** Whether the comparison is case sensitive. */
+    private IOCase caseSensitivity = IOCase.SENSITIVE;
+
     /**
      * Constructs a new Prefix file filter for a single prefix.
      * 
@@ -60,6 +64,20 @@
     }
 
     /**
+     * Constructs a new Prefix file filter for a single prefix 
+     * specifying case-sensitivity.
+     * 
+     * @param prefix  the prefix to allow, must not be null
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @throws IllegalArgumentException if the prefix is null
+     * @since Commons IO 1.4
+     */
+    public PrefixFileFilter(String prefix, IOCase caseSensitivity) {
+        this(prefix);
+        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
+    }
+
+    /**
      * Constructs a new Prefix file filter for any of an array of prefixes.
      * <p>
      * The array is not cloned, so could be changed after constructing the
@@ -76,6 +94,23 @@
     }
 
     /**
+     * Constructs a new Prefix file filter for any of an array of prefixes
+     * specifying case-sensitivity.
+     * <p>
+     * The array is not cloned, so could be changed after constructing the
+     * instance. This would be inadvisable however.
+     * 
+     * @param prefixes  the prefixes to allow, must not be null
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @throws IllegalArgumentException if the prefix is null
+     * @since Commons IO 1.4
+     */
+    public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity) {
+        this(prefixes);
+        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
+    }
+
+    /**
      * Constructs a new Prefix file filter for a list of prefixes.
      * 
      * @param prefixes  the prefixes to allow, must not be null
@@ -90,6 +125,21 @@
     }
 
     /**
+     * Constructs a new Prefix file filter for a list of prefixes
+     * specifying case-sensitivity.
+     * 
+     * @param prefixes  the prefixes to allow, must not be null
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @throws IllegalArgumentException if the prefix list is null
+     * @throws ClassCastException if the list does not contain Strings
+     * @since Commons IO 1.4
+     */
+    public PrefixFileFilter(List prefixes, IOCase caseSensitivity) {
+        this(prefixes);
+        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
+    }
+
+    /**
      * Checks to see if the filename starts with the prefix.
      * 
      * @param file  the File to check
@@ -98,7 +148,7 @@
     public boolean accept(File file) {
         String name = file.getName();
         for (int i = 0; i < this.prefixes.length; i++) {
-            if (name.startsWith(this.prefixes[i])) {
+            if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
                 return true;
             }
         }
@@ -114,7 +164,7 @@
      */
     public boolean accept(File file, String name) {
         for (int i = 0; i < prefixes.length; i++) {
-            if (name.startsWith(prefixes[i])) {
+            if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
                 return true;
             }
         }

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SuffixFileFilter.java?rev=584162&r1=584161&r2=584162&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SuffixFileFilter.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SuffixFileFilter.java Fri Oct 12 06:33:33 2007
@@ -18,6 +18,7 @@
 
 import java.io.File;
 import java.util.List;
+import org.apache.commons.io.IOCase;
 
 /**
  * Filters files based on the suffix (what the filename ends with).
@@ -47,6 +48,9 @@
     /** The filename suffixes to search for */
     private String[] suffixes;
 
+    /** Whether the comparison is case sensitive. */
+    private IOCase caseSensitivity = IOCase.SENSITIVE;
+
     /**
      * Constructs a new Suffix file filter for a single extension.
      * 
@@ -61,6 +65,20 @@
     }
 
     /**
+     * Constructs a new Suffix file filter for a single extension
+     * specifying case-sensitivity.
+     *
+     * @param suffix  the suffix to allow, must not be null
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @throws IllegalArgumentException if the suffix is null
+     * @since Commons IO 1.4
+     */
+    public SuffixFileFilter(String suffix, IOCase caseSensitivity) {
+        this(suffix);
+        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
+    }
+
+    /**
      * Constructs a new Suffix file filter for an array of suffixs.
      * <p>
      * The array is not cloned, so could be changed after constructing the
@@ -77,6 +95,23 @@
     }
 
     /**
+     * Constructs a new Suffix file filter for an array of suffixs
+     * specifying case-sensitivity.
+     * <p>
+     * The array is not cloned, so could be changed after constructing the
+     * instance. This would be inadvisable however.
+     * 
+     * @param suffixes  the suffixes to allow, must not be null
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @throws IllegalArgumentException if the suffix array is null
+     * @since Commons IO 1.4
+     */
+    public SuffixFileFilter(String[] suffixes, IOCase caseSensitivity) {
+        this(suffixes);
+        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
+    }
+
+    /**
      * Constructs a new Suffix file filter for a list of suffixes.
      * 
      * @param suffixes  the suffixes to allow, must not be null
@@ -91,6 +126,21 @@
     }
 
     /**
+     * Constructs a new Suffix file filter for a list of suffixes
+     * specifying case-sensitivity.
+     * 
+     * @param suffixes  the suffixes to allow, must not be null
+     * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
+     * @throws IllegalArgumentException if the suffix list is null
+     * @throws ClassCastException if the list does not contain Strings
+     * @since Commons IO 1.4
+     */
+    public SuffixFileFilter(List suffixes, IOCase caseSensitivity) {
+        this(suffixes);
+        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
+    }
+
+    /**
      * Checks to see if the filename ends with the suffix.
      * 
      * @param file  the File to check
@@ -99,7 +149,7 @@
     public boolean accept(File file) {
         String name = file.getName();
         for (int i = 0; i < this.suffixes.length; i++) {
-            if (name.endsWith(this.suffixes[i])) {
+            if (caseSensitivity.checkEndsWith(name, suffixes[i])) {
                 return true;
             }
         }
@@ -115,7 +165,7 @@
      */
     public boolean accept(File file, String name) {
         for (int i = 0; i < this.suffixes.length; i++) {
-            if (name.endsWith(this.suffixes[i])) {
+            if (caseSensitivity.checkEndsWith(name, suffixes[i])) {
                 return true;
             }
         }

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=584162&r1=584161&r2=584162&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 Fri Oct 12 06:33:33 2007
@@ -129,6 +129,46 @@
         }
 }
 
+    public void testSuffixCaseInsensitive() throws Exception {
+
+        IOFileFilter filter = new SuffixFileFilter(new String[] { "tes", "est" }, IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("foo.tes"), true);
+        assertFiltering(filter, new File("foo.est"), true);
+        assertFiltering(filter, new File("foo.EST"), true); //case-sensitive
+        assertFiltering(filter, new File("foo.TES"), true); //case-sensitive
+        assertFiltering(filter, new File("foo.exe"), false);
+
+        filter = new SuffixFileFilter("est", IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("test"), true);
+        assertFiltering(filter, new File("TEST"), true);
+
+        List suffixes = Arrays.asList( new String[] { "tes", "est" } );
+        filter = new SuffixFileFilter(suffixes, IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("bar.tes"), true);
+        assertFiltering(filter, new File("bar.est"), true);
+        assertFiltering(filter, new File("bar.EST"), true); //case-sensitive
+        assertFiltering(filter, new File("bar.TES"), true); //case-sensitive
+        assertFiltering(filter, new File("bar.exe"), false);
+
+        try {
+            new SuffixFileFilter((String) null, IOCase.INSENSITIVE);
+            fail();
+        } catch (IllegalArgumentException ex) {
+        }
+
+        try {
+            new SuffixFileFilter((String[]) null, IOCase.INSENSITIVE);
+            fail();
+        } catch (IllegalArgumentException ex) {
+        }
+
+        try {
+            new SuffixFileFilter((List) null, IOCase.INSENSITIVE);
+            fail();
+        } catch (IllegalArgumentException ex) {
+        }
+    }
+
     public void testDirectory() throws Exception {
         // XXX: This test presumes the current working dir is the base dir of the source checkout.
         IOFileFilter filter = new DirectoryFileFilter();
@@ -206,6 +246,46 @@
 
         try {
             new PrefixFileFilter((List) null);
+            fail();
+        } catch (IllegalArgumentException ex) {
+        }
+    }
+
+    public void testPrefixCaseInsensitive() throws Exception {
+
+        IOFileFilter filter = new PrefixFileFilter(new String[] { "foo", "bar" }, IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("foo.test1"), true);
+        assertFiltering(filter, new File("bar.test1"), true);
+        assertFiltering(filter, new File("FOO.test1"), true);  //case-sensitive
+        assertFiltering(filter, new File("BAR.test1"), true);  //case-sensitive
+
+        filter = new PrefixFileFilter("bar", IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("foo.test2"), false);
+        assertFiltering(filter, new File("bar.test2"), true);
+        assertFiltering(filter, new File("FOO.test2"), false); //case-sensitive
+        assertFiltering(filter, new File("BAR.test2"), true);  //case-sensitive
+
+        List prefixes = Arrays.asList( new String[] { "foo", "bar" } );
+        filter = new PrefixFileFilter(prefixes, IOCase.INSENSITIVE);
+        assertFiltering(filter, new File("foo.test3"), true);
+        assertFiltering(filter, new File("bar.test3"), true);
+        assertFiltering(filter, new File("FOO.test3"), true);  //case-sensitive
+        assertFiltering(filter, new File("BAR.test3"), true);  //case-sensitive
+
+        try {
+            new PrefixFileFilter((String) null, IOCase.INSENSITIVE);
+            fail();
+        } catch (IllegalArgumentException ex) {
+        }
+
+        try {
+            new PrefixFileFilter((String[]) null, IOCase.INSENSITIVE);
+            fail();
+        } catch (IllegalArgumentException ex) {
+        }
+
+        try {
+            new PrefixFileFilter((List) null, IOCase.INSENSITIVE);
             fail();
         } catch (IllegalArgumentException ex) {
         }