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 timetampedObserver = new TimestampedObserver();
34   * try (final ObservableInputStream inputStream = new ObservableInputStream(...),
35   *     timetampedObserver)) {
36   *     ...
37   * }
38   * System.out.printf("IO duration: %s%n", timetampedObserver.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      @Override
49      public void closed() throws IOException {
50          closeInstant = Instant.now();
51      }
52  
53      /**
54       * Gets the instant for when this instance was closed.
55       *
56       * @return the instant for when closed was called.
57       */
58      public Instant getCloseInstant() {
59          return closeInstant;
60      }
61  
62      /**
63       * Gets the Duration between creation and close.
64       *
65       * @return the Duration between creation and close.
66       */
67      public Duration getOpenToCloseDuration() {
68          return Duration.between(openInstant, closeInstant);
69      }
70  
71      /**
72       * Gets the Duration between creation and now.
73       *
74       * @return the Duration between creation and now.
75       */
76      public Duration getOpenToNowDuration() {
77          return Duration.between(openInstant, Instant.now());
78      }
79  
80      /**
81       * Gets the instant for when this instance was created.
82       *
83       * @return the instant for when this instance was created.
84       */
85      public Instant getOpenInstant() {
86          return openInstant;
87      }
88  
89      @Override
90      public String toString() {
91          return "TimestampedObserver [openInstant=" + openInstant + ", closeInstant=" + closeInstant + "]";
92      }
93  
94  }