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  
18  package org.apache.commons.io.input;
19  
20  import java.io.IOException;
21  import java.time.Duration;
22  import java.time.Instant;
23  
24  import org.apache.commons.io.input.ObservableInputStream.Observer;
25  
26  /**
27   * An observer with timestamps.
28   * <p>
29   * For example:
30   * </p>
31   *
32   * <pre>
33   * final TimestampedObserver timestampedObserver = new TimestampedObserver();
34   * try (ObservableInputStream inputStream = new ObservableInputStream(...),
35   *     timestampedObserver)) {
36   *     ...
37   * }
38   * System.out.printf("IO duration: %s%n", timestampedObserver.getOpenToCloseDuration());
39   * </pre>
40   *
41   * @since 2.9.0
42   */
43  public class TimestampedObserver extends Observer {
44  
45      private volatile Instant closeInstant;
46      private final Instant openInstant = Instant.now();
47  
48      /**
49       * Constructs a new instance.
50       */
51      public TimestampedObserver() {
52          // empty
53      }
54  
55      @Override
56      public void closed() throws IOException {
57          closeInstant = Instant.now();
58      }
59  
60      /**
61       * Gets the instant for when this instance was closed.
62       *
63       * @return the instant for when closed was called.
64       */
65      public Instant getCloseInstant() {
66          return closeInstant;
67      }
68  
69      /**
70       * Gets the instant for when this instance was created.
71       *
72       * @return the instant for when this instance was created.
73       */
74      public Instant getOpenInstant() {
75          return openInstant;
76      }
77  
78      /**
79       * Gets the Duration between creation and close.
80       *
81       * @return the Duration between creation and close.
82       */
83      public Duration getOpenToCloseDuration() {
84          return Duration.between(openInstant, closeInstant);
85      }
86  
87      /**
88       * Gets the Duration between creation and now.
89       *
90       * @return the Duration between creation and now.
91       */
92      public Duration getOpenToNowDuration() {
93          return Duration.between(openInstant, Instant.now());
94      }
95  
96      /**
97       * Tests whether {@link #closed()} has been called.
98       *
99       * @return whether {@link #closed()} has been called.
100      * @since 2.12.0
101      */
102     public boolean isClosed() {
103         return closeInstant != null;
104     }
105 
106     @Override
107     public String toString() {
108         return "TimestampedObserver [openInstant=" + openInstant + ", closeInstant=" + closeInstant + "]";
109     }
110 
111 }