Add TimestampedObserver.isClosed().
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -433,6 +433,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add IOConsumer.asConsumer().
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add TimestampedObserver.isClosed().
+ </action>
<!-- UPDATE -->
<action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
Bump actions/cache from 2.1.6 to 3.0.7 #307, #337.

MODIFY
src/main/java/org/apache/commons/io/input/TimestampedObserver.java
src/main/java/org/apache/commons/io/input/TimestampedObserver.java
@@ -86,6 +86,16 @@ public Duration getOpenToNowDuration() {
return Duration.between(openInstant, Instant.now());
}
+ /**
+ * Tests whether {@link #closed()} has been called.
+ *
+ * @return whether {@link #closed()} has been called.
+ * @since 2.12.0
+ */
+ public boolean isClosed() {
+ return closeInstant != null;
+ }
+
@Override
public String toString() {
return "TimestampedObserver [openInstant=" + openInstant + ", closeInstant=" + closeInstant + "]";

MODIFY
src/test/java/org/apache/commons/io/input/TimestampedObserverTest.java
src/test/java/org/apache/commons/io/input/TimestampedObserverTest.java
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -39,6 +40,7 @@ public void test() throws IOException, InterruptedException {
// Some OS' clock granularity may be high.
Thread.sleep(20);
final TimestampedObserver timestampedObserver = new TimestampedObserver();
+ assertFalse(timestampedObserver.isClosed());
// Java 8 instant resolution is not great.
Thread.sleep(20);
// toString() should not blow up before close().
@@ -46,11 +48,14 @@ public void test() throws IOException, InterruptedException {
assertTrue(timestampedObserver.getOpenInstant().isAfter(before));
assertTrue(timestampedObserver.getOpenToNowDuration().toNanos() > 0);
assertNull(timestampedObserver.getCloseInstant());
+ assertFalse(timestampedObserver.isClosed());
final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
try (ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer), timestampedObserver)) {
assertTrue(timestampedObserver.getOpenInstant().isAfter(before));
assertTrue(timestampedObserver.getOpenToNowDuration().toNanos() > 0);
+ assertFalse(timestampedObserver.isClosed());
}
+ assertTrue(timestampedObserver.isClosed());
assertTrue(timestampedObserver.getOpenInstant().isAfter(before));
assertTrue(timestampedObserver.getOpenToNowDuration().toNanos() > 0);
assertTrue(timestampedObserver.getCloseInstant().isAfter(timestampedObserver.getOpenInstant()));

Add TimestampedObserver.isClosed().