Keep org.apache.commons.io.monitor.FileEntry Serializble for behavioral
compatibility
MODIFY
src/main/java/org/apache/commons/io/monitor/FileEntry.java
src/main/java/org/apache/commons/io/monitor/FileEntry.java
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 long getLastModified() {
* @since 2.12.0
*/
public FileTime getLastModifiedFileTime() {
- return lastModified;
+ return lastModified.unwrap();
}
/**
@@ -211,7 +211,7 @@ public FileEntry newChildInstance(final File file) {
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 boolean refresh(final File file) {
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 void setExists(final boolean exists) {
* @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 void setLastModified(final FileTime lastModified) {
* @param lastModified The last modified time in milliseconds.
*/
public void setLastModified(final long lastModified) {
- this.lastModified = FileTime.fromMillis(lastModified);
+ setLastModified(FileTime.fromMillis(lastModified));
}
/**

ADD
src/main/java/org/apache/commons/io/monitor/SerializableFileTime.java
+/*
+ * 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());
+ }
+}

ADD
src/test/java/org/apache/commons/io/monitor/FileEntryTest.java
+/*
+ * 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());
+ }
+}

ADD
src/test/java/org/apache/commons/io/monitor/SerializableFileTimeTest.java
+/*
+ * 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());
+ }
+
+}

Keep org.apache.commons.io.monitor.FileEntry Serializble for behavioral
compatibility