TimestampedObserver.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.input;

  18. import java.io.IOException;
  19. import java.time.Duration;
  20. import java.time.Instant;

  21. import org.apache.commons.io.input.ObservableInputStream.Observer;

  22. /**
  23.  * An observer with timestamps.
  24.  * <p>
  25.  * For example:
  26.  * </p>
  27.  *
  28.  * <pre>
  29.  * final TimestampedObserver timestampedObserver = new TimestampedObserver();
  30.  * try (ObservableInputStream inputStream = new ObservableInputStream(...),
  31.  *     timestampedObserver)) {
  32.  *     ...
  33.  * }
  34.  * System.out.printf("IO duration: %s%n", timestampedObserver.getOpenToCloseDuration());
  35.  * </pre>
  36.  *
  37.  * @since 2.9.0
  38.  */
  39. public class TimestampedObserver extends Observer {

  40.     private volatile Instant closeInstant;
  41.     private final Instant openInstant = Instant.now();

  42.     /**
  43.      * Constructs a new instance.
  44.      */
  45.     public TimestampedObserver() {
  46.         // empty
  47.     }

  48.     @Override
  49.     public void closed() throws IOException {
  50.         closeInstant = Instant.now();
  51.     }

  52.     /**
  53.      * Gets the instant for when this instance was closed.
  54.      *
  55.      * @return the instant for when closed was called.
  56.      */
  57.     public Instant getCloseInstant() {
  58.         return closeInstant;
  59.     }

  60.     /**
  61.      * Gets the instant for when this instance was created.
  62.      *
  63.      * @return the instant for when this instance was created.
  64.      */
  65.     public Instant getOpenInstant() {
  66.         return openInstant;
  67.     }

  68.     /**
  69.      * Gets the Duration between creation and close.
  70.      *
  71.      * @return the Duration between creation and close.
  72.      */
  73.     public Duration getOpenToCloseDuration() {
  74.         return Duration.between(openInstant, closeInstant);
  75.     }

  76.     /**
  77.      * Gets the Duration between creation and now.
  78.      *
  79.      * @return the Duration between creation and now.
  80.      */
  81.     public Duration getOpenToNowDuration() {
  82.         return Duration.between(openInstant, Instant.now());
  83.     }

  84.     /**
  85.      * Tests whether {@link #closed()} has been called.
  86.      *
  87.      * @return whether {@link #closed()} has been called.
  88.      * @since 2.12.0
  89.      */
  90.     public boolean isClosed() {
  91.         return closeInstant != null;
  92.     }

  93.     @Override
  94.     public String toString() {
  95.         return "TimestampedObserver [openInstant=" + openInstant + ", closeInstant=" + closeInstant + "]";
  96.     }

  97. }