Skip to content

Commit 8e26517

Browse files
committed
Add AgeFileFilter.AgeFileFilter(Instant, boolean)
- Add PathUtils.isNewer(Path, Instant, LinkOption...) - Add PathUtils.isNewer(Path, FileTime, LinkOption...).
1 parent ac36a6d commit 8e26517

4 files changed

Lines changed: 77 additions & 16 deletions

File tree

src/changes/changes.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ The <action> type attribute can be add,update,fix,remove.
147147
<action dev="ggregory" type="add" due-to="Gary Gregory">
148148
Add FileSystem.normalizeSeparators().
149149
</action>
150+
<action dev="ggregory" type="add" due-to="Gary Gregory">
151+
Add PathUtils.isNewer(Path, FileTime, LinkOption...).
152+
</action>
153+
<action dev="ggregory" type="add" due-to="Gary Gregory">
154+
Add PathUtils.isNewer(Path, Instant, LinkOption...).
155+
</action>
156+
<action dev="ggregory" type="add" due-to="Gary Gregory">
157+
Add AgeFileFilter.AgeFileFilter(Instant, boolean).
158+
</action>
150159
<!-- UPDATE -->
151160
<action dev="ggregory" type="update" due-to="Dependabot">
152161
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.

src/main/java/org/apache/commons/io/file/PathUtils.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@
4343
import java.nio.file.attribute.BasicFileAttributes;
4444
import java.nio.file.attribute.DosFileAttributeView;
4545
import java.nio.file.attribute.FileAttribute;
46+
import java.nio.file.attribute.FileTime;
4647
import java.nio.file.attribute.PosixFileAttributeView;
4748
import java.nio.file.attribute.PosixFileAttributes;
4849
import java.nio.file.attribute.PosixFilePermission;
50+
import java.time.Instant;
4951
import java.util.ArrayList;
5052
import java.util.Arrays;
5153
import java.util.Collection;
@@ -766,20 +768,56 @@ public static boolean isEmptyFile(final Path file) throws IOException {
766768
* Tests if the specified {@code Path} is newer than the specified time reference.
767769
*
768770
* @param file the {@code Path} of which the modification date must be compared
769-
* @param timeMillis the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
771+
* @param fileTime the time reference.
770772
* @param options options indicating how symbolic links are handled * @return true if the {@code Path} exists and has
771773
* been modified after the given time reference.
772774
* @return true if the {@code Path} exists and has been modified after the given time reference.
773775
* @throws IOException if an I/O error occurs.
774776
* @throws NullPointerException if the file is {@code null}
775-
* @since 2.9.0
777+
* @since 2.12.0
776778
*/
777-
public static boolean isNewer(final Path file, final long timeMillis, final LinkOption... options) throws IOException {
779+
public static boolean isNewer(final Path file, final FileTime fileTime, final LinkOption... options) throws IOException {
780+
Objects.requireNonNull(file, "file");
781+
if (Files.notExists(file)) {
782+
return false;
783+
}
784+
return Files.getLastModifiedTime(file, options).compareTo(fileTime) > 0;
785+
}
786+
787+
/**
788+
* Tests if the specified {@code Path} is newer than the specified time reference.
789+
*
790+
* @param file the {@code Path} of which the modification date must be compared
791+
* @param instant the time reference.
792+
* @param options options indicating how symbolic links are handled * @return true if the {@code Path} exists and has
793+
* been modified after the given time reference.
794+
* @return true if the {@code Path} exists and has been modified after the given time reference.
795+
* @throws IOException if an I/O error occurs.
796+
* @throws NullPointerException if the file is {@code null}
797+
* @since 2.12.0
798+
*/
799+
public static boolean isNewer(final Path file, final Instant instant, final LinkOption... options) throws IOException {
778800
Objects.requireNonNull(file, "file");
779801
if (Files.notExists(file)) {
780802
return false;
781803
}
782-
return Files.getLastModifiedTime(file, options).toMillis() > timeMillis;
804+
return Files.getLastModifiedTime(file, options).toInstant().isAfter(instant);
805+
}
806+
807+
/**
808+
* Tests if the specified {@code Path} is newer than the specified time reference.
809+
*
810+
* @param file the {@code Path} of which the modification date must be compared
811+
* @param timeMillis the time reference measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970)
812+
* @param options options indicating how symbolic links are handled * @return true if the {@code Path} exists and has
813+
* been modified after the given time reference.
814+
* @return true if the {@code Path} exists and has been modified after the given time reference.
815+
* @throws IOException if an I/O error occurs.
816+
* @throws NullPointerException if the file is {@code null}
817+
* @since 2.9.0
818+
*/
819+
public static boolean isNewer(final Path file, final long timeMillis, final LinkOption... options) throws IOException {
820+
return isNewer(file, FileTime.fromMillis(timeMillis), options);
783821
}
784822

785823
/**

src/main/java/org/apache/commons/io/filefilter/AgeFileFilter.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.FileVisitResult;
2323
import java.nio.file.Path;
2424
import java.nio.file.attribute.BasicFileAttributes;
25+
import java.time.Instant;
2526
import java.util.Date;
2627

2728
import org.apache.commons.io.FileUtils;
@@ -80,7 +81,7 @@ public class AgeFileFilter extends AbstractFileFilter implements Serializable {
8081
private final boolean acceptOlder;
8182

8283
/** The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970). */
83-
private final long cutoffMillis;
84+
private final Instant cutoffInstant;
8485

8586
/**
8687
* Constructs a new age file filter for files older than (at or before) a certain cutoff date.
@@ -124,6 +125,20 @@ public AgeFileFilter(final File cutoffReference, final boolean acceptOlder) {
124125
this(FileUtils.lastModifiedUnchecked(cutoffReference), acceptOlder);
125126
}
126127

128+
/**
129+
* Constructs a new age file filter for files on any one side of a certain cutoff.
130+
*
131+
* @param cutoffInstant The cutoff time threshold since the epoch (00:00:00 GMT, January 1,
132+
* 1970).
133+
* @param acceptOlder if true, older files (at or before the cutoff) are accepted, else newer ones (after the
134+
* cutoff).
135+
* @since 2.12.0
136+
*/
137+
public AgeFileFilter(final Instant cutoffInstant, final boolean acceptOlder) {
138+
this.acceptOlder = acceptOlder;
139+
this.cutoffInstant = cutoffInstant;
140+
}
141+
127142
/**
128143
* Constructs a new age file filter for files equal to or older than a certain cutoff
129144
*
@@ -143,8 +158,7 @@ public AgeFileFilter(final long cutoffMillis) {
143158
* cutoff).
144159
*/
145160
public AgeFileFilter(final long cutoffMillis, final boolean acceptOlder) {
146-
this.acceptOlder = acceptOlder;
147-
this.cutoffMillis = cutoffMillis;
161+
this(Instant.ofEpochMilli(cutoffMillis), acceptOlder);
148162
}
149163

150164
/**
@@ -159,7 +173,7 @@ public AgeFileFilter(final long cutoffMillis, final boolean acceptOlder) {
159173
*/
160174
@Override
161175
public boolean accept(final File file) {
162-
final boolean newer = FileUtils.isFileNewer(file, cutoffMillis);
176+
final boolean newer = FileUtils.isFileNewer(file, cutoffInstant);
163177
return acceptOlder != newer;
164178
}
165179

@@ -178,7 +192,7 @@ public boolean accept(final File file) {
178192
public FileVisitResult accept(final Path file, final BasicFileAttributes attributes) {
179193
final boolean newer;
180194
try {
181-
newer = PathUtils.isNewer(file, cutoffMillis);
195+
newer = PathUtils.isNewer(file, cutoffInstant);
182196
} catch (final IOException e) {
183197
return handle(e);
184198
}
@@ -193,6 +207,6 @@ public FileVisitResult accept(final Path file, final BasicFileAttributes attribu
193207
@Override
194208
public String toString() {
195209
final String condition = acceptOlder ? "<=" : ">";
196-
return super.toString() + "(" + condition + cutoffMillis + ")";
210+
return super.toString() + "(" + condition + cutoffInstant + ")";
197211
}
198212
}

src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,26 @@ public static IOFileFilter ageFileFilter(final File cutoffReference, final boole
111111
* Returns a filter that returns true if the file was last modified before
112112
* or at the specified cutoff time.
113113
*
114-
* @param cutoff the time threshold
114+
* @param cutoffMillis the time threshold
115115
* @return an appropriately configured age file filter
116116
* @see AgeFileFilter
117117
* @since 1.2
118118
*/
119-
public static IOFileFilter ageFileFilter(final long cutoff) {
120-
return new AgeFileFilter(cutoff);
119+
public static IOFileFilter ageFileFilter(final long cutoffMillis) {
120+
return new AgeFileFilter(cutoffMillis);
121121
}
122122

123123
/**
124124
* Returns a filter that filters files based on a cutoff time.
125125
*
126-
* @param cutoff the time threshold
126+
* @param cutoffMillis the time threshold
127127
* @param acceptOlder if true, older files get accepted, if false, newer
128128
* @return an appropriately configured age file filter
129129
* @see AgeFileFilter
130130
* @since 1.2
131131
*/
132-
public static IOFileFilter ageFileFilter(final long cutoff, final boolean acceptOlder) {
133-
return new AgeFileFilter(cutoff, acceptOlder);
132+
public static IOFileFilter ageFileFilter(final long cutoffMillis, final boolean acceptOlder) {
133+
return new AgeFileFilter(cutoffMillis, acceptOlder);
134134
}
135135

136136
/**

0 commit comments

Comments
 (0)