You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/09/07 13:53:04 UTC
[commons-io] branch master updated: Update FileEntry to use
FileTime instead of long for file time stamps.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push:
new cce1173 Update FileEntry to use FileTime instead of long for file time stamps.
cce1173 is described below
commit cce11736b688378f8b610a7a06ea2843fb666129
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Sep 7 09:53:02 2021 -0400
Update FileEntry to use FileTime instead of long for file time stamps.
Add FileTimes.
---
src/changes/changes.xml | 6 ++
.../commons/io/file/attribute/FileTimes.java | 39 +++++++++++
.../commons/io/file/attribute/package-info.java | 21 ++++++
.../org/apache/commons/io/monitor/FileEntry.java | 81 ++++++++++++++--------
.../commons/io/file/attribute/FileTimesTest.java | 34 +++++++++
5 files changed, 154 insertions(+), 27 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9f65711..4a4e5d5 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -160,6 +160,12 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add FileUtils.lastModifiedFileTime(File).
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add FileTimes.
+ </action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Update FileEntry to use FileTime instead of long for file time stamps.
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot">
Bump Maven Javadoc plugin from 3.2.0 to 3.3.0.
diff --git a/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java b/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java
new file mode 100644
index 0000000..72d92a9
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.file.attribute;
+
+import java.nio.file.attribute.FileTime;
+import java.time.Instant;
+
+/**
+ * Helps use {@link FileTime}.
+ */
+public class FileTimes {
+
+ /**
+ * Constant for the 1970-01-01T00:00:00Z epoch time stamp attribute.
+ *
+ * @see Instant#EPOCH
+ */
+ public static final FileTime EPOCH = FileTime.from(Instant.EPOCH);
+
+ private FileTimes() {
+ // No instances.
+ }
+
+}
diff --git a/src/main/java/org/apache/commons/io/file/attribute/package-info.java b/src/main/java/org/apache/commons/io/file/attribute/package-info.java
new file mode 100644
index 0000000..351bb63
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/file/attribute/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/**
+ * Helps use {@link java.nio.file.attribute}.
+ */
+package org.apache.commons.io.file.attribute;
diff --git a/src/main/java/org/apache/commons/io/monitor/FileEntry.java b/src/main/java/org/apache/commons/io/monitor/FileEntry.java
index 5b9dece..a08f03a 100644
--- a/src/main/java/org/apache/commons/io/monitor/FileEntry.java
+++ b/src/main/java/org/apache/commons/io/monitor/FileEntry.java
@@ -20,8 +20,10 @@ import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
+import java.nio.file.attribute.FileTime;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.file.attribute.FileTimes;
/**
* The state of a file or directory, capturing the following {@link File} attributes at a point in time.
@@ -56,11 +58,11 @@ public class FileEntry implements Serializable {
private String name;
private boolean exists;
private boolean directory;
- private long lastModified;
+ private FileTime lastModified = FileTimes.EPOCH;
private long length;
/**
- * Construct a new monitor for a specified {@link File}.
+ * Constructs a new monitor for a specified {@link File}.
*
* @param file The file being monitored
*/
@@ -69,14 +71,14 @@ public class FileEntry implements Serializable {
}
/**
- * Construct a new monitor for a specified {@link File}.
+ * Constructs a new monitor for a specified {@link File}.
*
* @param parent The parent
* @param file The file being monitored
*/
public FileEntry(final FileEntry parent, final File file) {
if (file == null) {
- throw new IllegalArgumentException("File is missing");
+ throw new IllegalArgumentException("File is null.");
}
this.file = file;
this.parent = parent;
@@ -84,15 +86,17 @@ public class FileEntry implements Serializable {
}
/**
- * Refresh the attributes from the {@link File}, indicating
+ * Refreshes the attributes from the {@link File}, indicating
* whether the file has changed.
* <p>
* This implementation refreshes the {@code name}, {@code exists},
* {@code directory}, {@code lastModified} and {@code length}
* properties.
+ * </p>
* <p>
* The {@code exists}, {@code directory}, {@code lastModified}
* and {@code length} properties are compared for changes
+ * </p>
*
* @param file the file instance to compare to
* @return {@code true} if the file has changed, otherwise {@code false}
@@ -100,7 +104,7 @@ public class FileEntry implements Serializable {
public boolean refresh(final File file) {
// cache original values
final boolean origExists = exists;
- final long origLastModified = lastModified;
+ final FileTime origLastModified = lastModified;
final boolean origDirectory = directory;
final long origLength = length;
@@ -109,22 +113,23 @@ public class FileEntry implements Serializable {
exists = Files.exists(file.toPath());
directory = exists && file.isDirectory();
try {
- lastModified = exists ? FileUtils.lastModified(file) : 0;
+ lastModified = exists ? FileUtils.lastModifiedFileTime(file) : FileTimes.EPOCH;
} catch (final IOException e) {
- lastModified = 0;
+ lastModified = FileTimes.EPOCH;
}
length = exists && !directory ? file.length() : 0;
// Return if there are changes
- return exists != origExists || lastModified != origLastModified || directory != origDirectory
+ return exists != origExists || !lastModified.equals(origLastModified) || directory != origDirectory
|| length != origLength;
}
/**
- * Create a new child instance.
+ * Creates a new child instance.
* <p>
* Custom implementations should override this method to return
* a new instance of the appropriate type.
+ * </p>
*
* @param file The child file
* @return a new child instance
@@ -134,7 +139,7 @@ public class FileEntry implements Serializable {
}
/**
- * Return the parent entry.
+ * Gets the parent entry.
*
* @return the parent entry
*/
@@ -143,7 +148,7 @@ public class FileEntry implements Serializable {
}
/**
- * Return the level
+ * Gets the level
*
* @return the level
*/
@@ -152,7 +157,7 @@ public class FileEntry implements Serializable {
}
/**
- * Return the directory's files.
+ * Gets the directory's files.
*
* @return This directory's files or an empty
* array if the file is not a directory or the
@@ -163,7 +168,7 @@ public class FileEntry implements Serializable {
}
/**
- * Set the directory's files.
+ * Sets the directory's files.
*
* @param children This directory's files, may be null
*/
@@ -172,7 +177,7 @@ public class FileEntry implements Serializable {
}
/**
- * Return the file being monitored.
+ * Gets the file being monitored.
*
* @return the file being monitored
*/
@@ -181,7 +186,7 @@ public class FileEntry implements Serializable {
}
/**
- * Return the file name.
+ * Gets the file name.
*
* @return the file name
*/
@@ -190,7 +195,7 @@ public class FileEntry implements Serializable {
}
/**
- * Set the file name.
+ * Sets the file name.
*
* @param name the file name
*/
@@ -199,27 +204,49 @@ public class FileEntry implements Serializable {
}
/**
- * Return the last modified time from the last time it
+ * Gets the last modified time from the last time it
* was checked.
*
- * @return the last modified time
+ * @return the last modified time in milliseconds.
*/
public long getLastModified() {
+ return lastModified.toMillis();
+ }
+
+ /**
+ * Gets the last modified time from the last time it
+ * was checked.
+ *
+ * @return the last modified time.
+ * @since 2.12.0
+ */
+ public FileTime getLastModifiedFileTime() {
return lastModified;
}
/**
- * Return the last modified time from the last time it
+ * Sets the last modified time from the last time it
* was checked.
*
- * @param lastModified The last modified time
+ * @param lastModified The last modified time in milliseconds.
*/
public void setLastModified(final long lastModified) {
+ this.lastModified = FileTime.fromMillis(lastModified);
+ }
+
+ /**
+ * Sets the last modified time from the last time it
+ * was checked.
+ *
+ * @param lastModified The last modified time.
+ * @since 2.12.0
+ */
+ public void setLastModified(final FileTime lastModified) {
this.lastModified = lastModified;
}
/**
- * Return the length.
+ * Gets the length.
*
* @return the length
*/
@@ -228,7 +255,7 @@ public class FileEntry implements Serializable {
}
/**
- * Set the length.
+ * Sets the length.
*
* @param length the length
*/
@@ -237,7 +264,7 @@ public class FileEntry implements Serializable {
}
/**
- * Indicate whether the file existed the last time it
+ * Tests whether the file existed the last time it
* was checked.
*
* @return whether the file existed
@@ -247,7 +274,7 @@ public class FileEntry implements Serializable {
}
/**
- * Set whether the file existed the last time it
+ * Sets whether the file existed the last time it
* was checked.
*
* @param exists whether the file exists or not
@@ -257,7 +284,7 @@ public class FileEntry implements Serializable {
}
/**
- * Indicate whether the file is a directory or not.
+ * Tests whether the file is a directory or not.
*
* @return whether the file is a directory or not
*/
@@ -266,7 +293,7 @@ public class FileEntry implements Serializable {
}
/**
- * Set whether the file is a directory or not.
+ * Sets whether the file is a directory or not.
*
* @param directory whether the file is a directory or not
*/
diff --git a/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java b/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java
new file mode 100644
index 0000000..6e1aeea
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.file.attribute;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link FileTimes}.
+ */
+public class FileTimesTest {
+
+ @Test
+ public void testEpoch() {
+ assertEquals(0, FileTimes.EPOCH.toMillis());
+ }
+
+}