You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2006/07/22 18:53:21 UTC

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

Author: niallp
Date: Sat Jul 22 09:53:20 2006
New Revision: 424601

URL: http://svn.apache.org/viewvc?rev=424601&view=rev
Log:
IO-85  IOFileFilter implementations for File.canRead(), File.canWrite(), File.isHidden() and empty files/directories

Added:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java   (with props)
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java   (with props)
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java   (with props)
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java   (with props)
Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=424601&r1=424600&r2=424601&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Sat Jul 22 09:53:20 2006
@@ -54,6 +54,22 @@
 - NameFileFilter
   - Ability to control case-sensitivity
 
+- CanReadFileFilter
+  - New IOFileFilter implementation
+  - Accepts files where File.canRead() is true
+
+- CanWriteFileFilter
+  - New IOFileFilter implementation
+  - Accepts files where File.canWrite() is true
+
+- HiddenFileFilter
+  - New IOFileFilter implementation
+  - Accepts files where File.isHidden() is true
+
+- EmptyFileFilter
+  - New IOFileFilter implementation
+  - Accepts files or directories that are empty
+
 
 Feedback
 --------

Added: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java?rev=424601&view=auto
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java (added)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java Sat Jul 22 09:53:20 2006
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.filefilter;
+
+import java.io.File;
+
+/**
+ * This filter accepts <code>File</code>s that can be read.
+ * <p>
+ * Example, showing how to print out a list of the 
+ * current directory's <i>readable</i> files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( CanReadFileFilter.CAN_READ );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * <p>
+ * Example, showing how to print out a list of the 
+ * current directory's <i>un-readable</i> files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( CanReadFileFilter.CANNOT_READ );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * <p>
+ * Example, showing how to print out a list of the 
+ * current directory's <i>read-only</i> files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( CanReadFileFilter.READ_ONLY );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * @since Commons IO 1.3
+ * @version $Revision$
+ */
+public class CanReadFileFilter extends AbstractFileFilter {
+    
+    /** Singleton instance of <i>readable</i> filter */
+    public static final IOFileFilter CAN_READ = new CanReadFileFilter();
+
+    /** Singleton instance of not <i>readable</i> filter */
+    public static final IOFileFilter CANNOT_READ = new NotFileFilter(CAN_READ);
+    
+    /** Singleton instance of <i>read-only</i> filter */
+    public static final IOFileFilter READ_ONLY = new AndFileFilter(CAN_READ,
+                                                CanWriteFileFilter.CANNOT_WRITE);
+    
+    /**
+     * Restrictive consructor.
+     */
+    protected CanReadFileFilter() {
+    }
+    
+    /**
+     * Checks to see if the file can be read.
+     * 
+     * @param file  the File to check.
+     * @return <code>true</code> if the file can be
+     *  read, otherwise <code>false</code>.
+     */
+    public boolean accept(File file) {
+        return file.canRead();
+    }
+    
+}

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanReadFileFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java?rev=424601&view=auto
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java (added)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java Sat Jul 22 09:53:20 2006
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.filefilter;
+
+import java.io.File;
+
+/**
+ * This filter accepts <code>File</code>s that can be written to.
+ * <p>
+ * Example, showing how to print out a list of the
+ * current directory's <i>writable</i> files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( CanWriteFileFilter.CAN_WRITE );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * <p>
+ * Example, showing how to print out a list of the
+ * current directory's <i>un-writable</i> files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( CanWriteFileFilter.CANNOT_WRITE );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * <p>
+ * <b>N.B.</b> For read-only files, use 
+ *    <code>CanReadFileFilter.READ_ONLY</code>.
+ *
+ * @since Commons IO 1.3
+ * @version $Revision$
+ */
+public class CanWriteFileFilter extends AbstractFileFilter {
+    
+    /** Singleton instance of <i>writable</i> filter */
+    public static final IOFileFilter CAN_WRITE = new CanWriteFileFilter();
+
+    /** Singleton instance of not <i>writable</i> filter */
+    public static final IOFileFilter CANNOT_WRITE = new NotFileFilter(CAN_WRITE);
+
+    /**
+     * Restrictive consructor.
+     */
+    protected CanWriteFileFilter() {
+    }
+    
+    /**
+     * Checks to see if the file can be written to.
+     * 
+     * @param file  the File to check
+     * @return <code>true</code> if the file can be
+     *  written to, otherwise <code>false</code>.
+     */
+    public boolean accept(File file) {
+        return file.canWrite();
+    }
+    
+}

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/CanWriteFileFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java?rev=424601&view=auto
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java (added)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java Sat Jul 22 09:53:20 2006
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.filefilter;
+
+import java.io.File;
+
+/**
+ * This filter accepts files or directories that are empty.
+ * <p>
+ * If the <code>File</code> is a directory it checks that
+ * it contains no files.
+ * <p>
+ * Example, showing how to print out a list of the 
+ * current directory's empty files/directories:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( EmptyFileFilter.EMPTY );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * <p>
+ * Example, showing how to print out a list of the 
+ * current directory's non-empty files/directories:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( EmptyFileFilter.NOT_EMPTY );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * @since Commons IO 1.3
+ * @version $Revision$
+ */
+public class EmptyFileFilter extends AbstractFileFilter {
+    
+    /** Singleton instance of <i>empty</i> filter */
+    public static final IOFileFilter EMPTY = new EmptyFileFilter();
+    
+    /** Singleton instance of <i>not-empty</i> filter */
+    public static final IOFileFilter NOT_EMPTY = new NotFileFilter(EMPTY);
+    
+    /**
+     * Restrictive consructor.
+     */
+    protected EmptyFileFilter() {
+    }
+    
+    /**
+     * Checks to see if the file is empty.
+     * 
+     * @param file  the file or directory to check
+     * @return <code>true</code> if the file or directory
+     *  is <i>empty</i>, otherwise <code>false</code>.
+     */
+    public boolean accept(File file) {
+        if (file.isDirectory()) {
+            File[] files = file.listFiles();
+            return (files == null || files.length == 0);
+        } else {
+            return (file.length() == 0);
+        }
+    }
+    
+}

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/EmptyFileFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java?rev=424601&view=auto
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java (added)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java Sat Jul 22 09:53:20 2006
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.filefilter;
+
+import java.io.File;
+
+/**
+ * This filter accepts <code>File</code>s that are hidden.
+ * <p>
+ * Example, showing how to print out a list of the
+ * current directory's <i>hidden</i> files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( HiddenFileFilter.HIDDEN );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * <p>
+ * Example, showing how to print out a list of the
+ * current directory's <i>visible</i> (i.e. not hidden) files:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( HiddenFileFilter.VISIBLE );
+ * for ( int i = 0; i &lt; files.length; i++ ) {
+ *     System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * @since Commons IO 1.3
+ * @version $Revision$
+ */
+public class HiddenFileFilter extends AbstractFileFilter {
+    
+    /** Singleton instance of <i>hidden</i> filter */
+    public static final IOFileFilter HIDDEN  = new HiddenFileFilter();
+    
+    /** Singleton instance of <i>visible</i> filter */
+    public static final IOFileFilter VISIBLE = new NotFileFilter(HIDDEN);
+    
+    /**
+     * Restrictive consructor.
+     */
+    protected HiddenFileFilter() {
+    }
+    
+    /**
+     * Checks to see if the file is hidden.
+     * 
+     * @param file  the File to check
+     * @return <code>true</code> if the file is
+     *  <i>hidden</i>, otherwise <code>false</code>.
+     */
+    public boolean accept(File file) {
+        return file.isHidden();
+    }
+    
+}

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/HiddenFileFilter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java?rev=424601&r1=424600&r2=424601&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java Sat Jul 22 09:53:20 2006
@@ -625,6 +625,63 @@
         }
     }
 
+    public void testHidden() throws Exception {
+        File hiddenDir = new File(".svn");
+        if (hiddenDir.exists()) {
+            assertFiltering(HiddenFileFilter.HIDDEN,  hiddenDir, true);
+            assertFiltering(HiddenFileFilter.VISIBLE, hiddenDir, false);
+        }
+        assertFiltering(HiddenFileFilter.HIDDEN,  getTestDirectory(), false);
+        assertFiltering(HiddenFileFilter.VISIBLE, getTestDirectory(), true);
+    }
+
+    public void testCanRead() throws Exception {
+        File readOnlyFile = new File(getTestDirectory(), "read-only-file1.txt");
+        createFile(readOnlyFile, 32);
+        readOnlyFile.setReadOnly();
+        assertFiltering(CanReadFileFilter.CAN_READ,  readOnlyFile, true);
+        assertFiltering(CanReadFileFilter.CANNOT_READ,  readOnlyFile, false);
+        assertFiltering(CanReadFileFilter.READ_ONLY, readOnlyFile, true);
+        readOnlyFile.delete();
+    }
+
+    public void testCanWrite() throws Exception {
+        File readOnlyFile = new File(getTestDirectory(), "read-only-file2.txt");
+        createFile(readOnlyFile, 32);
+        readOnlyFile.setReadOnly();
+        assertFiltering(CanWriteFileFilter.CAN_WRITE,    getTestDirectory(), true);
+        assertFiltering(CanWriteFileFilter.CANNOT_WRITE, getTestDirectory(), false);
+        assertFiltering(CanWriteFileFilter.CAN_WRITE,    readOnlyFile, false);
+        assertFiltering(CanWriteFileFilter.CANNOT_WRITE, readOnlyFile, true);
+        readOnlyFile.delete();
+    }
+
+    public void testEmpty() throws Exception {
+
+        // Empty Dir        
+        File emptyDir  = new File(getTestDirectory(), "empty-dir");
+        emptyDir.mkdirs();
+        assertFiltering(EmptyFileFilter.EMPTY, emptyDir, true);
+        assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyDir, false);
+
+        // Empty File
+        File emptyFile = new File(emptyDir, "empty-file.txt");
+        createFile(emptyFile, 0);
+        assertFiltering(EmptyFileFilter.EMPTY, emptyFile, true);
+        assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyFile, false);
+
+        // Not Empty Dir
+        assertFiltering(EmptyFileFilter.EMPTY, emptyDir, false);
+        assertFiltering(EmptyFileFilter.NOT_EMPTY, emptyDir, true);
+
+        // Not Empty File
+        File notEmptyFile = new File(emptyDir, "not-empty-file.txt");
+        createFile(notEmptyFile, 32);
+        assertFiltering(EmptyFileFilter.EMPTY, notEmptyFile, false);
+        assertFiltering(EmptyFileFilter.NOT_EMPTY, notEmptyFile, true);
+        FileUtils.forceDelete(emptyDir);
+    }
+
     private void spin(long now) throws InterruptedException {
         final long end = now + 1000;
         while (System.currentTimeMillis() <= end) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org