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 6cf14df9 Keep org.apache.commons.io.monitor.FileEntry Serializble for 
behavioral compatibility
6cf14df9 is described below

commit 6cf14df952b17b26dc7c86f2b6f38138998d88eb
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Jun 28 11:24:23 2022 -0400

    Keep org.apache.commons.io.monitor.FileEntry Serializble for behavioral
    compatibility
---
 .../org/apache/commons/io/monitor/FileEntry.java   | 16 ++--
 .../commons/io/monitor/SerializableFileTime.java   | 94 ++++++++++++++++++++++
 .../apache/commons/io/monitor/FileEntryTest.java   | 43 ++++++++++
 .../io/monitor/SerializableFileTimeTest.java       | 48 +++++++++++
 4 files changed, 195 insertions(+), 6 deletions(-)

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 7c55f32c..819892a5 100644
--- a/src/main/java/org/apache/commons/io/monitor/FileEntry.java
+++ b/src/main/java/org/apache/commons/io/monitor/FileEntry.java
@@ -59,7 +59,7 @@ public class FileEntry implements Serializable {
     private String name;
     private boolean exists;
     private boolean directory;
-    private FileTime lastModified = FileTimes.EPOCH;
+    private SerializableFileTime lastModified = SerializableFileTime.EPOCH;
     private long length;
 
     /**
@@ -120,7 +120,7 @@ public class FileEntry implements Serializable {
      * @since 2.12.0
      */
     public FileTime getLastModifiedFileTime() {
-        return lastModified;
+        return lastModified.unwrap();
     }
 
     /**
@@ -211,7 +211,7 @@ public class FileEntry implements Serializable {
     public boolean refresh(final File file) {
         // cache original values
         final boolean origExists = exists;
-        final FileTime origLastModified = lastModified;
+        final SerializableFileTime origLastModified = lastModified;
         final boolean origDirectory = directory;
         final long origLength = length;
 
@@ -220,9 +220,9 @@ public class FileEntry implements Serializable {
         exists = Files.exists(file.toPath());
         directory = exists && file.isDirectory();
         try {
-            lastModified = exists ? FileUtils.lastModifiedFileTime(file) : 
FileTimes.EPOCH;
+            setLastModified(exists ? FileUtils.lastModifiedFileTime(file) : 
FileTimes.EPOCH);
         } catch (final IOException e) {
-            lastModified = FileTimes.EPOCH;
+            setLastModified(SerializableFileTime.EPOCH);
         }
         length = exists && !directory ? file.length() : 0;
 
@@ -266,6 +266,10 @@ public class FileEntry implements Serializable {
      * @since 2.12.0
      */
     public void setLastModified(final FileTime lastModified) {
+        setLastModified(new SerializableFileTime(lastModified));
+    }
+
+    void setLastModified(final SerializableFileTime lastModified) {
         this.lastModified = lastModified;
     }
 
@@ -276,7 +280,7 @@ public class FileEntry implements Serializable {
      * @param lastModified The last modified time in milliseconds.
      */
     public void setLastModified(final long lastModified) {
-        this.lastModified = FileTime.fromMillis(lastModified);
+        setLastModified(FileTime.fromMillis(lastModified));
     }
 
     /**
diff --git 
a/src/main/java/org/apache/commons/io/monitor/SerializableFileTime.java 
b/src/main/java/org/apache/commons/io/monitor/SerializableFileTime.java
new file mode 100644
index 00000000..ee0dd52a
--- /dev/null
+++ b/src/main/java/org/apache/commons/io/monitor/SerializableFileTime.java
@@ -0,0 +1,94 @@
+/*
+ * 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.monitor;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.nio.file.attribute.FileTime;
+import java.time.Instant;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.commons.io.file.attribute.FileTimes;
+
+/**
+ * Wraps a {@link FileTime} and allows it to be serializable.
+ */
+class SerializableFileTime implements Serializable {
+
+    static final SerializableFileTime EPOCH = new 
SerializableFileTime(FileTimes.EPOCH);
+
+    private static final long serialVersionUID = 1L;
+
+    private FileTime fileTime;
+
+    public SerializableFileTime(final FileTime fileTime) {
+        this.fileTime = Objects.requireNonNull(fileTime);
+    }
+
+    public int compareTo(final FileTime other) {
+        return fileTime.compareTo(other);
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof SerializableFileTime)) {
+            return false;
+        }
+        final SerializableFileTime other = (SerializableFileTime) obj;
+        return Objects.equals(fileTime, other.fileTime);
+    }
+
+    @Override
+    public int hashCode() {
+        return fileTime.hashCode();
+    }
+
+    private void readObject(final ObjectInputStream ois) throws 
ClassNotFoundException, IOException {
+        this.fileTime = FileTime.from((Instant) ois.readObject());
+    }
+
+    long to(final TimeUnit unit) {
+        return fileTime.to(unit);
+    }
+
+    Instant toInstant() {
+        return fileTime.toInstant();
+    }
+
+    long toMillis() {
+        return fileTime.toMillis();
+    }
+
+    @Override
+    public String toString() {
+        return fileTime.toString();
+    }
+
+    FileTime unwrap() {
+        return fileTime;
+    }
+
+    private void writeObject(final ObjectOutputStream oos) throws IOException {
+        oos.writeObject(fileTime.toInstant());
+    }
+}
diff --git a/src/test/java/org/apache/commons/io/monitor/FileEntryTest.java 
b/src/test/java/org/apache/commons/io/monitor/FileEntryTest.java
new file mode 100644
index 00000000..a42d8f7b
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/monitor/FileEntryTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.monitor;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.SerializationUtils;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link FileEntry}.
+ */
+public class FileEntryTest {
+
+    @Test
+    public void testSerializable() {
+        final FileEntry fe = new FileEntry(FileUtils.current());
+        assertEquals(fe.getChildren(), 
SerializationUtils.roundtrip(fe).getChildren());
+        assertEquals(fe.getClass(), 
SerializationUtils.roundtrip(fe).getClass());
+        assertEquals(fe.getFile(), SerializationUtils.roundtrip(fe).getFile());
+        assertEquals(fe.getLastModified(), 
SerializationUtils.roundtrip(fe).getLastModified());
+        assertEquals(fe.getLastModifiedFileTime(), 
SerializationUtils.roundtrip(fe).getLastModifiedFileTime());
+        assertEquals(fe.getLength(), 
SerializationUtils.roundtrip(fe).getLength());
+        assertEquals(fe.getLevel(), 
SerializationUtils.roundtrip(fe).getLevel());
+        assertEquals(fe.getName(), SerializationUtils.roundtrip(fe).getName());
+        assertEquals(fe.getParent(), 
SerializationUtils.roundtrip(fe).getParent());
+    }
+}
diff --git 
a/src/test/java/org/apache/commons/io/monitor/SerializableFileTimeTest.java 
b/src/test/java/org/apache/commons/io/monitor/SerializableFileTimeTest.java
new file mode 100644
index 00000000..29249351
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/monitor/SerializableFileTimeTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.monitor;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.attribute.FileTime;
+
+import org.apache.commons.io.file.PathUtils;
+import org.apache.commons.lang3.SerializationUtils;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link SerializableFileTime}.
+ */
+public class SerializableFileTimeTest {
+
+    @Test
+    public void testSerializable() throws IOException {
+        final SerializableFileTime expected = new 
SerializableFileTime(Files.getLastModifiedTime(PathUtils.current()));
+        final SerializableFileTime actual = 
SerializationUtils.roundtrip(expected);
+        assertEquals(expected, actual);
+        final FileTime expectedFt = expected.unwrap();
+        assertEquals(expectedFt, actual.unwrap());
+        assertEquals(0, actual.compareTo(expectedFt));
+        assertEquals(expectedFt.hashCode(), actual.hashCode());
+        assertEquals(expectedFt.toInstant(), actual.toInstant());
+        assertEquals(expectedFt.toMillis(), actual.toMillis());
+        assertEquals(expectedFt.toString(), actual.toString());
+    }
+
+}

Reply via email to