View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.filter;
18  
19  import java.io.Serializable;
20  import java.util.Date;
21  
22  import org.apache.commons.vfs2.FileContent;
23  import org.apache.commons.vfs2.FileFilter;
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSelectInfo;
26  import org.apache.commons.vfs2.FileSystemException;
27  
28  /**
29   * Filters files based on a cutoff time, can filter either newer files or files
30   * equal to or older.
31   * <p>
32   * For example, to print all files and directories in the current directory
33   * older than one day:
34   * </p>
35   *
36   * <pre>
37   * FileSystemManager fsManager = VFS.getManager();
38   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
39   * // We are interested in files older than one day
40   * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
41   * AgeFileFilter filter = new AgeFileFilter(cutoff);
42   * FileObject[] files = dir.findFiles(new FileFilterSelector(filter));
43   * for (int i = 0; i &lt; files.length; i++) {
44   *     System.out.println(files[i]);
45   * }
46   * </pre>
47   *
48   * @author This code was originally ported from Apache Commons IO File Filter
49   * @see "http://commons.apache.org/proper/commons-io/"
50   * @since 2.4
51   */
52  public class AgeFileFilter implements FileFilter, Serializable {
53  
54      private static final long serialVersionUID = 1L;
55  
56      /** Whether the files accepted will be older or newer. */
57      private final boolean acceptOlder;
58  
59      /** The cutoff time threshold. */
60      private final long cutoff;
61  
62      /**
63       * Tests if the specified <code>File</code> is newer than the specified time
64       * reference.
65       *
66       * @param fileObject the <code>File</code> of which the modification date must
67       *                   be compared, must not be {@code null}
68       * @param timeMillis the time reference measured in milliseconds since the epoch
69       *                   (00:00:00 GMT, January 1, 1970)
70       * @return true if the <code>File</code> exists and has been modified after the
71       *         given time reference.
72       * @throws FileSystemException Thrown for file system errors.
73       * @throws IllegalArgumentException if the file is {@code null}
74       */
75      private static boolean isFileNewer(final FileObject fileObject, final long timeMillis) throws FileSystemException {
76          if (fileObject == null) {
77              throw new IllegalArgumentException("No specified file");
78          }
79          if (!fileObject.exists()) {
80              return false;
81          }
82          try (final FileContent content = fileObject.getContent()) {
83              final long lastModified = content.getLastModifiedTime();
84              return lastModified > timeMillis;
85          }
86      }
87  
88      /**
89       * Constructs a new age file filter for files older than (at or before) a
90       * certain cutoff date.
91       *
92       * @param cutoffDate the threshold age of the files
93       */
94      public AgeFileFilter(final Date cutoffDate) {
95          this(cutoffDate, true);
96      }
97  
98      /**
99       * Constructs a new age file filter for files on any one side of a certain
100      * cutoff date.
101      *
102      * @param cutoffDate  the threshold age of the files
103      * @param acceptOlder if true, older files (at or before the cutoff) are
104      *                    accepted, else newer ones (after the cutoff).
105      */
106     public AgeFileFilter(final Date cutoffDate, final boolean acceptOlder) {
107         this(cutoffDate.getTime(), acceptOlder);
108     }
109 
110     /**
111      * Constructs a new age file filter for files older than (at or before) a
112      * certain File (whose last modification time will be used as reference).
113      *
114      * @param cutoffReference the file whose last modification time is usesd as the
115      *                        threshold age of the files
116      *
117      * @throws FileSystemException Error reading the last modification time from the
118      *                             reference file object.
119      */
120     public AgeFileFilter(final FileObject cutoffReference) throws FileSystemException {
121         this(cutoffReference, true);
122     }
123 
124     /**
125      * Constructs a new age file filter for files on any one side of a certain File
126      * (whose last modification time will be used as reference).
127      *
128      * @param cutoffReference the file whose last modification time is usesd as the
129      *                        threshold age of the files
130      * @param acceptOlder     if true, older files (at or before the cutoff) are
131      *                        accepted, else newer ones (after the cutoff).
132      *
133      * @throws FileSystemException Error reading the last modification time from the
134      *                             reference file object.
135      */
136     public AgeFileFilter(final FileObject cutoffReference, final boolean acceptOlder) throws FileSystemException {
137         this(cutoffReference.getContent().getLastModifiedTime(), acceptOlder);
138     }
139 
140     /**
141      * Constructs a new age file filter for files equal to or older than a certain
142      * cutoff.
143      *
144      * @param cutoff the threshold age of the files
145      */
146     public AgeFileFilter(final long cutoff) {
147         this(cutoff, true);
148     }
149 
150     /**
151      * Constructs a new age file filter for files on any one side of a certain
152      * cutoff.
153      *
154      * @param cutoff      the threshold age of the files
155      * @param acceptOlder if true, older files (at or before the cutoff) are
156      *                    accepted, else newer ones (after the cutoff).
157      */
158     public AgeFileFilter(final long cutoff, final boolean acceptOlder) {
159         this.acceptOlder = acceptOlder;
160         this.cutoff = cutoff;
161     }
162 
163     /**
164      * Checks to see if the last modification of the file matches cutoff favorably.
165      * <p>
166      * If last modification time equals cutoff and newer files are required, file
167      * <b>IS NOT</b> selected. If last modification time equals cutoff and older
168      * files are required, file <b>IS</b> selected.
169      * </p>
170      *
171      * @param fileInfo the File to check
172      *
173      * @return true if the file name matches
174      * @throws FileSystemException Thrown for file system errors.
175      */
176     @Override
177     public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
178         final boolean newer = isFileNewer(fileInfo.getFile(), cutoff);
179         return acceptOlder ? !newer : newer;
180     }
181 
182     /**
183      * Provide a String representaion of this file filter.
184      *
185      * @return a String representaion
186      */
187     @Override
188     public String toString() {
189         final String condition = acceptOlder ? "<=" : ">";
190         return super.toString() + "(" + condition + cutoff + ")";
191     }
192 }