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 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
72
73
74
75
76
77
78 private void readObject(final ObjectInputStream in) throws ClassNotFoundException, IOException {
79 this.fileTime = FileTime.from((Instant) in.readObject());
80 }
81
82 long to(final TimeUnit unit) {
83 return fileTime.to(unit);
84 }
85
86 Instant toInstant() {
87 return fileTime.toInstant();
88 }
89
90 long toMillis() {
91 return fileTime.toMillis();
92 }
93
94 @Override
95 public String toString() {
96 return fileTime.toString();
97 }
98
99 FileTime unwrap() {
100 return fileTime;
101 }
102
103 private void writeObject(final ObjectOutputStream oos) throws IOException {
104 oos.writeObject(fileTime.toInstant());
105 }
106 }