View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Wraps a {@link FileTime} and allows it to be Serializable.
32   *
33   * <h2>Deprecating Serialization</h2>
34   * <p>
35   * <em>Serialization is deprecated and will be removed in 3.0.</em>
36   * </p>
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  }