001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.input;
019
020import java.io.IOException;
021import java.time.Duration;
022import java.time.Instant;
023
024import org.apache.commons.io.input.ObservableInputStream.Observer;
025
026/**
027 * An observer with timestamps.
028 * <p>
029 * For example:
030 * </p>
031 *
032 * <pre>
033 * final TimestampedObserver timestampedObserver = new TimestampedObserver();
034 * try (ObservableInputStream inputStream = new ObservableInputStream(...),
035 *     timestampedObserver)) {
036 *     ...
037 * }
038 * System.out.printf("IO duration: %s%n", timestampedObserver.getOpenToCloseDuration());
039 * </pre>
040 *
041 * @since 2.9.0
042 */
043public class TimestampedObserver extends Observer {
044
045    private volatile Instant closeInstant;
046    private final Instant openInstant = Instant.now();
047
048    /**
049     * Constructs a new instance.
050     */
051    public TimestampedObserver() {
052        // empty
053    }
054
055    @Override
056    public void closed() throws IOException {
057        closeInstant = Instant.now();
058    }
059
060    /**
061     * Gets the instant for when this instance was closed.
062     *
063     * @return the instant for when closed was called.
064     */
065    public Instant getCloseInstant() {
066        return closeInstant;
067    }
068
069    /**
070     * Gets the instant for when this instance was created.
071     *
072     * @return the instant for when this instance was created.
073     */
074    public Instant getOpenInstant() {
075        return openInstant;
076    }
077
078    /**
079     * Gets the Duration between creation and close.
080     *
081     * @return the Duration between creation and close.
082     */
083    public Duration getOpenToCloseDuration() {
084        return Duration.between(openInstant, closeInstant);
085    }
086
087    /**
088     * Gets the Duration between creation and now.
089     *
090     * @return the Duration between creation and now.
091     */
092    public Duration getOpenToNowDuration() {
093        return Duration.between(openInstant, Instant.now());
094    }
095
096    /**
097     * Tests whether {@link #closed()} has been called.
098     *
099     * @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}