1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.monitor;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23 import java.nio.file.attribute.FileTime;
24 import java.time.Instant;
25 import java.util.Objects;
26 import java.util.concurrent.TimeUnit;
27
28 import org.apache.commons.io.file.attribute.FileTimes;
29
30
31
32
33
34
35
36
37
38 final class SerializableFileTime implements Serializable {
39
40 static final SerializableFileTime EPOCH = new SerializableFileTime(FileTimes.EPOCH);
41
42 private static final long serialVersionUID = 1L;
43
44 private FileTime fileTime;
45
46 public SerializableFileTime(final FileTime fileTime) {
47 this.fileTime = Objects.requireNonNull(fileTime);
48 }
49
50 public int compareTo(final FileTime other) {
51 return fileTime.compareTo(other);
52 }
53
54 @Override
55 public boolean equals(final Object obj) {
56 if (this == obj) {
57 return true;
58 }
59 if (!(obj instanceof SerializableFileTime)) {
60 return false;
61 }
62 final SerializableFileTime other = (SerializableFileTime) obj;
63 return Objects.equals(fileTime, other.fileTime);
64 }
65
66 @Override
67 public int hashCode() {
68 return fileTime.hashCode();
69 }
70
71 private void readObject(final ObjectInputStream ois) throws ClassNotFoundException, IOException {
72 this.fileTime = FileTime.from((Instant) ois.readObject());
73 }
74
75 long to(final TimeUnit unit) {
76 return fileTime.to(unit);
77 }
78
79 Instant toInstant() {
80 return fileTime.toInstant();
81 }
82
83 long toMillis() {
84 return fileTime.toMillis();
85 }
86
87 @Override
88 public String toString() {
89 return fileTime.toString();
90 }
91
92 FileTime unwrap() {
93 return fileTime;
94 }
95
96 private void writeObject(final ObjectOutputStream oos) throws IOException {
97 oos.writeObject(fileTime.toInstant());
98 }
99 }