You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2006/01/01 18:49:27 UTC
svn commit: r360504 - in /jakarta/commons/proper/io/trunk: ./
src/java/org/apache/commons/io/filefilter/
src/test/org/apache/commons/io/filefilter/
Author: scolebourne
Date: Sun Jan 1 09:49:18 2006
New Revision: 360504
URL: http://svn.apache.org/viewcvs?rev=360504&view=rev
Log:
Add AgeFileFilter and SizeFileFilter
rfe 36824, from Rahul Akolkar
Added:
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java (with props)
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java (with props)
Modified:
jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
jakarta/commons/proper/io/trunk/project.xml
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java
jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/package.html
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/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=360504&r1=360503&r2=360504&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Sun Jan 1 09:49:18 2006
@@ -36,6 +36,9 @@
Enhancements from 1.1
---------------------
+- AgeFileFilter/SizeFileFilter
+ New file filters that compares against the age and size of the file
+
- FileUtils.contentEquals(File,File)
Performance improved by adding length and file location checking
Modified: jakarta/commons/proper/io/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/project.xml?rev=360504&r1=360503&r2=360504&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/project.xml (original)
+++ jakarta/commons/proper/io/trunk/project.xml Sun Jan 1 09:49:18 2006
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<!--
- Copyright 2001-2005 The Apache Software Foundation
+ Copyright 2001-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.
@@ -166,6 +166,9 @@
</developers>
<contributors>
+ <contributor>
+ <name>Rahul Akolkar</name>
+ </contributor>
<contributor>
<name>Jason Anderson</name>
</contributor>
Added: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java?rev=360504&view=auto
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java (added)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java Sun Jan 1 09:49:18 2006
@@ -0,0 +1,128 @@
+/*
+ * Copyright 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;
+import java.util.Date;
+
+import org.apache.commons.io.FileUtils;
+
+/**
+ * Filters files based on a cutoff time, can filter either older or newer files.
+ * <p>
+ * For example, to print all files and directories in the
+ * current directory older than one day:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * // We are interested in files older than one day
+ * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
+ * String[] files = dir.list( new AgeFileFilter(cutoff) );
+ * for ( int i = 0; i < files.length; i++ ) {
+ * System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * @author Rahul Akolkar
+ * @version $Id$
+ * @since Commons IO 1.2
+ */
+public class AgeFileFilter extends AbstractFileFilter {
+
+ /** The cutoff time threshold. */
+ private long cutoff;
+ /** Whether the files accepted will be older or newer. */
+ private boolean acceptOlder;
+
+ /**
+ * Constructs a new age file filter for files older than a certain cutoff.
+ *
+ * @param age the threshold age of the files
+ */
+ public AgeFileFilter(long cutoff) {
+ this(cutoff, true);
+ }
+
+ /**
+ * Constructs a new age file filter for files on any one side
+ * of a certain cutoff.
+ *
+ * @param age the threshold age of the files
+ * @param acceptOlder if true, older files are accepted, else newer ones
+ */
+ public AgeFileFilter(long cutoff, boolean acceptOlder) {
+ this.acceptOlder = acceptOlder;
+ this.cutoff = cutoff;
+ }
+
+ /**
+ * Constructs a new age file filter for files older than a certain
+ * cutoff date.
+ *
+ * @param age the threshold age of the files
+ */
+ public AgeFileFilter(Date cutoffDate) {
+ this(cutoffDate, true);
+ }
+
+ /**
+ * Constructs a new age file filter for files on any one side
+ * of a certain cutoff date.
+ *
+ * @param age the threshold age of the files
+ * @param acceptOlder if true, older files are accepted, else newer ones
+ */
+ public AgeFileFilter(Date cutoffDate, boolean acceptOlder) {
+ this(cutoffDate.getTime(), acceptOlder);
+ }
+
+ /**
+ * Constructs a new age file filter for files older than a certain
+ * File (whose last modification time will be used as reference).
+ *
+ * @param age the threshold age of the files
+ */
+ public AgeFileFilter(File cutoffReference) {
+ this(cutoffReference, true);
+ }
+
+ /**
+ * Constructs a new age file filter for files on any one side
+ * of a certain File (whose last modification time will be used as
+ * reference).
+ *
+ * @param age the threshold age of the files
+ * @param acceptOlder if true, older files are accepted, else newer ones
+ */
+ public AgeFileFilter(File cutoffReference, boolean acceptOlder) {
+ this(cutoffReference.lastModified(), acceptOlder);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Checks to see if the last modification of the file matches cutoff
+ * favorably.
+ * If last modification time equals cutoff, file is not selected.
+ *
+ * @param file the File to check
+ * @return true if the filename matches
+ */
+ public boolean accept(File file) {
+ boolean newer = FileUtils.isFileNewer(file, cutoff);
+ return acceptOlder ? !newer : newer;
+ }
+
+}
Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/AgeFileFilter.java
------------------------------------------------------------------------------
svn:keywords = "author date id revision"
Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java?rev=360504&r1=360503&r2=360504&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/FileFilterUtils.java Sun Jan 1 09:49:18 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2005 The Apache Software Foundation.
+ * Copyright 2002-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.
@@ -15,8 +15,10 @@
*/
package org.apache.commons.io.filefilter;
+import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
+import java.util.Date;
/**
* Useful utilities for working with file filters. It provides access to all
@@ -30,6 +32,7 @@
* @author Stephen Colebourne
* @author Jeremias Maerki
* @author Masato Tezuka
+ * @author Rahul Akolkar
*/
public class FileFilterUtils {
@@ -202,6 +205,104 @@
} else {
return andFileFilter(filter, svnFilter);
}
+ }
+
+ //-----------------------------------------------------------------------
+
+ /**
+ * Returns a filter that returns true if the file was last modified after
+ * the specified cutoff time.
+ *
+ * @param cutoff the time threshold
+ * @return an appropriately configured age file filter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter ageFileFilter(long cutoff) {
+ return new AgeFileFilter(cutoff);
+ }
+
+ /**
+ * Returns a filter that filters files based on a cutoff time.
+ *
+ * @param cutoff the time threshold
+ * @param acceptOlder if true, older files get accepted, if false, newer
+ * @return an appropriately configured age file filter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter ageFileFilter(long cutoff, boolean acceptOlder) {
+ return new AgeFileFilter(cutoff, acceptOlder);
+ }
+
+ /**
+ * Returns a filter that returns true if the file was last modified after
+ * the specified cutoff date.
+ *
+ * @param cutoff the time threshold
+ * @return an appropriately configured age file filter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter ageFileFilter(Date cutoffDate) {
+ return new AgeFileFilter(cutoffDate);
+ }
+
+ /**
+ * Returns a filter that filters files based on a cutoff date.
+ *
+ * @param cutoff the time threshold
+ * @param acceptOlder if true, older files get accepted, if false, newer
+ * @return an appropriately configured age file filter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter ageFileFilter(Date cutoffDate, boolean acceptOlder) {
+ return new AgeFileFilter(cutoffDate, acceptOlder);
+ }
+
+ /**
+ * Returns a filter that returns true if the file was last modified after
+ * the specified reference file.
+ *
+ * @param cutoff the time threshold
+ * @return an appropriately configured age file filter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter ageFileFilter(File cutoffReference) {
+ return new AgeFileFilter(cutoffReference);
+ }
+
+ /**
+ * Returns a filter that filters files based on a cutoff reference file.
+ *
+ * @param cutoff the time threshold
+ * @param acceptOlder if true, older files get accepted, if false, newer
+ * @return an appropriately configured age file filter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter ageFileFilter(File cutoffReference, boolean acceptOlder) {
+ return new AgeFileFilter(cutoffReference, acceptOlder);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns a filter that returns true if the file is bigger than a certain size.
+ *
+ * @param long the file size threshold
+ * @return an appropriately configured SizeFileFilter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter sizeFileFilter(long threshold) {
+ return new SizeFileFilter(threshold);
+ }
+
+ /**
+ * Returns a filter that filters based on file size.
+ *
+ * @param long the file size threshold
+ * @param acceptLarger if true, larger files get accepted, if false, smaller
+ * @return an appropriately configured SizeFileFilter
+ * @since Commons IO 1.2
+ */
+ public static IOFileFilter sizeFileFilter(long threshold, boolean acceptLarger) {
+ return new SizeFileFilter(threshold, acceptLarger);
}
}
Added: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java?rev=360504&view=auto
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java (added)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java Sun Jan 1 09:49:18 2006
@@ -0,0 +1,85 @@
+/*
+ * Copyright 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;
+
+/**
+ * Filters files based on size, can filter either larger or smaller files
+ * as compared to a given threshold.
+ * <p>
+ * For example, to print all files and directories in the
+ * current directory whose size is greater than 1 MB:
+ *
+ * <pre>
+ * File dir = new File(".");
+ * String[] files = dir.list( new SizeFileFilter(1024 * 1024) );
+ * for ( int i = 0; i < files.length; i++ ) {
+ * System.out.println(files[i]);
+ * }
+ * </pre>
+ *
+ * @author Rahul Akolkar
+ * @version $Id$
+ * @since Commons IO 1.2
+ */
+public class SizeFileFilter extends AbstractFileFilter {
+
+ /** The size threshold. */
+ private long size;
+ /** Whether the files accepted will be larger or smaller. */
+ private boolean acceptLarger;
+
+ /**
+ * Constructs a new size file filter for files larger than a certain size.
+ *
+ * @param size the threshold size of the files
+ * @throws IllegalArgumentException if the size is negative
+ */
+ public SizeFileFilter(long size) {
+ this(size, true);
+ }
+
+ /**
+ * Constructs a new size file filter for files based on a certain size
+ * threshold.
+ *
+ * @param size the threshold size of the files
+ * @param acceptLarger if true, larger files are accepted, else smaller ones
+ * @throws IllegalArgumentException if the size is negative
+ */
+ public SizeFileFilter(long size, boolean acceptLarger) {
+ if (size < 0) {
+ throw new IllegalArgumentException("The size must be non-negative");
+ }
+ this.size = size;
+ this.acceptLarger = acceptLarger;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Checks to see if the size of the file is favorable.
+ * If size equals threshold, file is not selected.
+ *
+ * @param file the File to check
+ * @return true if the filename matches
+ */
+ public boolean accept(File file) {
+ boolean smaller = file.length() < size;
+ return acceptLarger ? !smaller : smaller;
+ }
+
+}
Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/SizeFileFilter.java
------------------------------------------------------------------------------
svn:keywords = "author date id revision"
Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/package.html
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/package.html?rev=360504&r1=360503&r2=360504&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/package.html (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/filefilter/package.html Sun Jan 1 09:49:18 2006
@@ -48,6 +48,14 @@
<td><a href="WildcardFileFilter.html">WildcardFileFilter</a></td>
<td>Filter based on wildcards</td>
</tr>
+ <tr>
+ <td><a href="AgeFileFilter.html">AgeFileFilter</a></td>
+ <td>Filter based on last modified time of file</td>
+ </tr>
+ <tr>
+ <td><a href="SizeFileFilter.html">SizeFileFilter</a></td>
+ <td>Filter based on file size</td>
+ </tr>
</tbody>
</table>
@@ -94,7 +102,7 @@
new SuffixFileFilter(".java")
)
),
- new NotdFileFilter(
+ new NotFileFilter(
new DirectoryFileFilter()
)
)
Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/filefilter/FileFilterTestCase.java?rev=360504&r1=360503&r2=360504&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 Sun Jan 1 09:49:18 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2005 The Apache Software Foundation.
+ * Copyright 2002-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.
@@ -20,6 +20,7 @@
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Date;
import java.util.List;
import junit.framework.TestSuite;
@@ -456,6 +457,76 @@
assertFiltering(filter1, file, true);
assertFiltering(filter2, file, false);
}
-
+
+ public void testAgeFilter() throws Exception {
+ File oldFile = new File(getTestDirectory(), "old.txt");
+ createFile(oldFile, 0);
+ spin();
+ long now = System.currentTimeMillis();
+ IOFileFilter filter1 = FileFilterUtils.ageFileFilter(now);
+ IOFileFilter filter2 = FileFilterUtils.ageFileFilter(now, true);
+ IOFileFilter filter3 = FileFilterUtils.ageFileFilter(now, false);
+ Date date = new Date();
+ IOFileFilter filter4 = FileFilterUtils.ageFileFilter(date);
+ IOFileFilter filter5 = FileFilterUtils.ageFileFilter(date, true);
+ IOFileFilter filter6 = FileFilterUtils.ageFileFilter(date, false);
+ File reference = new File(getTestDirectory(), "reference.txt");
+ createFile(reference, 0);
+ IOFileFilter filter7 = FileFilterUtils.ageFileFilter(reference);
+ IOFileFilter filter8 = FileFilterUtils.ageFileFilter(reference, true);
+ IOFileFilter filter9 = FileFilterUtils.ageFileFilter(reference, false);
+ spin();
+ File newFile = new File(getTestDirectory(), "new.txt");
+ createFile(newFile, 0);
+
+ assertFiltering(filter1, oldFile, true);
+ assertFiltering(filter2, oldFile, true);
+ assertFiltering(filter3, oldFile, false);
+ assertFiltering(filter4, oldFile, true);
+ assertFiltering(filter5, oldFile, true);
+ assertFiltering(filter6, oldFile, false);
+ assertFiltering(filter7, oldFile, true);
+ assertFiltering(filter8, oldFile, true);
+ assertFiltering(filter9, oldFile, false);
+ assertFiltering(filter1, newFile, false);
+ assertFiltering(filter2, newFile, false);
+ assertFiltering(filter3, newFile, true);
+ assertFiltering(filter4, newFile, false);
+ assertFiltering(filter5, newFile, false);
+ assertFiltering(filter6, newFile, true);
+ assertFiltering(filter7, newFile, false);
+ assertFiltering(filter8, newFile, false);
+ assertFiltering(filter9, newFile, true);
+ }
+
+ public void testSizeFilter() throws Exception {
+ File smallFile = new File(getTestDirectory(), "small.txt");
+ createFile(smallFile, 32);
+ File largeFile = new File(getTestDirectory(), "large.txt");
+ createFile(largeFile, 128);
+ IOFileFilter filter1 = FileFilterUtils.sizeFileFilter(64);
+ IOFileFilter filter2 = FileFilterUtils.sizeFileFilter(64, true);
+ IOFileFilter filter3 = FileFilterUtils.sizeFileFilter(64, false);
+
+ assertFiltering(filter1, smallFile, false);
+ assertFiltering(filter2, smallFile, false);
+ assertFiltering(filter3, smallFile, true);
+ assertFiltering(filter1, largeFile, true);
+ assertFiltering(filter2, largeFile, true);
+ assertFiltering(filter3, largeFile, false);
+
+ try {
+ FileFilterUtils.sizeFileFilter(-1);
+ fail();
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ private void spin() {
+ long now = System.currentTimeMillis();
+ while (System.currentTimeMillis() <= now);
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org