Debian Codegraph @

src/org/apache/commons/io/monitor/SerializableFileTime.java

raw symbols SCIP
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 * https://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 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 * Deserializes an instance from an ObjectInputStream.
73 *
74 * @param in The source ObjectInputStream.
75 * @throws IOException Any of the usual Input/Output related exceptions.
76 * @throws ClassNotFoundException A class of a serialized object cannot be found.
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 }