SerializableFileTime.java

  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. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.io.Serializable;
  22. import java.nio.file.attribute.FileTime;
  23. import java.time.Instant;
  24. import java.util.Objects;
  25. import java.util.concurrent.TimeUnit;

  26. import org.apache.commons.io.file.attribute.FileTimes;

  27. /**
  28.  * Wraps a {@link FileTime} and allows it to be Serializable.
  29.  *
  30.  * <h2>Deprecating Serialization</h2>
  31.  * <p>
  32.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  33.  * </p>
  34.  */
  35. final class SerializableFileTime implements Serializable {

  36.     static final SerializableFileTime EPOCH = new SerializableFileTime(FileTimes.EPOCH);

  37.     private static final long serialVersionUID = 1L;

  38.     private FileTime fileTime;

  39.     SerializableFileTime(final FileTime fileTime) {
  40.         this.fileTime = Objects.requireNonNull(fileTime);
  41.     }

  42.     public int compareTo(final FileTime other) {
  43.         return fileTime.compareTo(other);
  44.     }

  45.     @Override
  46.     public boolean equals(final Object obj) {
  47.         if (this == obj) {
  48.             return true;
  49.         }
  50.         if (!(obj instanceof SerializableFileTime)) {
  51.             return false;
  52.         }
  53.         final SerializableFileTime other = (SerializableFileTime) obj;
  54.         return Objects.equals(fileTime, other.fileTime);
  55.     }

  56.     @Override
  57.     public int hashCode() {
  58.         return fileTime.hashCode();
  59.     }

  60.     /**
  61.      * Deserializes an instance from an ObjectInputStream.
  62.      *
  63.      * @param in The source ObjectInputStream.
  64.      * @throws IOException            Any of the usual Input/Output related exceptions.
  65.      * @throws ClassNotFoundException A class of a serialized object cannot be found.
  66.      */
  67.     private void readObject(final ObjectInputStream in) throws ClassNotFoundException, IOException {
  68.         this.fileTime = FileTime.from((Instant) in.readObject());
  69.     }

  70.     long to(final TimeUnit unit) {
  71.         return fileTime.to(unit);
  72.     }

  73.     Instant toInstant() {
  74.         return fileTime.toInstant();
  75.     }

  76.     long toMillis() {
  77.         return fileTime.toMillis();
  78.     }

  79.     @Override
  80.     public String toString() {
  81.         return fileTime.toString();
  82.     }

  83.     FileTime unwrap() {
  84.         return fileTime;
  85.     }

  86.     private void writeObject(final ObjectOutputStream oos) throws IOException {
  87.         oos.writeObject(fileTime.toInstant());
  88.     }
  89. }