You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/12/10 19:01:24 UTC

[commons-io] branch master updated: Replace CloseShield* constructors with factory methods (#173)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 8a786ab  Replace CloseShield* constructors with factory methods (#173)
8a786ab is described below

commit 8a786abb5e2cc04568517f1f8c053578c3e313a6
Author: Rob Spoor <ro...@users.noreply.github.com>
AuthorDate: Thu Dec 10 20:01:15 2020 +0100

    Replace CloseShield* constructors with factory methods (#173)
    
    * Replace CloseShield* constructors with factory methods
    
    * Renamed CloseShield*.dontClose to wrap
    
    * Changed parameter names and tags as requested
---
 .../apache/commons/io/input/CloseShieldInputStream.java  | 16 ++++++++++++++++
 .../org/apache/commons/io/input/CloseShieldReader.java   | 16 ++++++++++++++++
 .../commons/io/output/CloseShieldOutputStream.java       | 16 ++++++++++++++++
 .../org/apache/commons/io/output/CloseShieldWriter.java  | 15 +++++++++++++++
 .../commons/io/input/CloseShieldInputStreamTest.java     |  2 +-
 .../apache/commons/io/input/CloseShieldReaderTest.java   |  2 +-
 .../commons/io/output/CloseShieldOutputStreamTest.java   | 11 +++--------
 .../apache/commons/io/output/CloseShieldWriterTest.java  | 11 +++--------
 8 files changed, 71 insertions(+), 18 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java b/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java
index 0463506..8b31603 100644
--- a/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java
@@ -35,7 +35,11 @@ public class CloseShieldInputStream extends ProxyInputStream {
      * closed.
      *
      * @param in underlying input stream
+     * @deprecated Using this constructor prevents IDEs from warning if
+     *             the underlying input stream is never closed.
+     *             Use {@link #wrap(InputStream)} instead.
      */
+    @Deprecated
     public CloseShieldInputStream(final InputStream in) {
         super(in);
     }
@@ -50,4 +54,16 @@ public class CloseShieldInputStream extends ProxyInputStream {
         in = ClosedInputStream.CLOSED_INPUT_STREAM;
     }
 
+    /**
+     * Creates a proxy that shields the given input stream from being
+     * closed.
+     *
+     * @param inputStream the input stream to wrap
+     * @return the created proxy
+     * @since 2.9.0
+     */
+    public static CloseShieldInputStream wrap(final InputStream inputStream) {
+        return new CloseShieldInputStream(inputStream);
+    }
+
 }
diff --git a/src/main/java/org/apache/commons/io/input/CloseShieldReader.java b/src/main/java/org/apache/commons/io/input/CloseShieldReader.java
index 612feb5..e1b33b2 100644
--- a/src/main/java/org/apache/commons/io/input/CloseShieldReader.java
+++ b/src/main/java/org/apache/commons/io/input/CloseShieldReader.java
@@ -35,7 +35,11 @@ public class CloseShieldReader extends ProxyReader {
      * closed.
      *
      * @param in underlying reader
+     * @deprecated Using this constructor prevents IDEs from warning if
+     *             the underlying reader is never closed.
+     *             Use {@link #wrap(Reader)} instead.
      */
+    @Deprecated
     public CloseShieldReader(final Reader in) {
         super(in);
     }
@@ -50,4 +54,16 @@ public class CloseShieldReader extends ProxyReader {
         in = ClosedReader.CLOSED_READER;
     }
 
+    /**
+     * Creates a proxy that shields the given reader from being
+     * closed.
+     *
+     * @param reader the reader to wrap
+     * @return the created proxy
+     * @since 2.9.0
+     */
+    public static CloseShieldReader wrap(final Reader reader) {
+        return new CloseShieldReader(reader);
+    }
+
 }
diff --git a/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java b/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java
index 8146533..6a0db0e 100644
--- a/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java
+++ b/src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java
@@ -35,7 +35,11 @@ public class CloseShieldOutputStream extends ProxyOutputStream {
      * closed.
      *
      * @param out underlying output stream
+     * @deprecated Using this constructor prevents IDEs from warning if
+     *             the underlying output stream is never closed.
+     *             Use {@link #wrap(OutputStream)} instead.
      */
+    @Deprecated
     public CloseShieldOutputStream(final OutputStream out) {
         super(out);
     }
@@ -50,4 +54,16 @@ public class CloseShieldOutputStream extends ProxyOutputStream {
         out = ClosedOutputStream.CLOSED_OUTPUT_STREAM;
     }
 
+    /**
+     * Creates a proxy that shields the given output stream from being
+     * closed.
+     *
+     * @param outputStream the output stream to wrap
+     * @return the created proxy
+     * @since 2.9.0
+     */
+    public static CloseShieldOutputStream wrap(final OutputStream outputStream) {
+        return new CloseShieldOutputStream(outputStream);
+    }
+
 }
diff --git a/src/main/java/org/apache/commons/io/output/CloseShieldWriter.java b/src/main/java/org/apache/commons/io/output/CloseShieldWriter.java
index 73a351b..4a1efa1 100644
--- a/src/main/java/org/apache/commons/io/output/CloseShieldWriter.java
+++ b/src/main/java/org/apache/commons/io/output/CloseShieldWriter.java
@@ -33,7 +33,11 @@ public class CloseShieldWriter extends ProxyWriter {
      * Creates a proxy that shields the given writer from being closed.
      *
      * @param out underlying writer
+     * @deprecated Using this constructor prevents IDEs from warning if
+     *             the underlying writer is never closed.
+     *             Use {@link #wrap(Writer)} instead.
      */
+    @Deprecated
     public CloseShieldWriter(final Writer out) {
         super(out);
     }
@@ -47,4 +51,15 @@ public class CloseShieldWriter extends ProxyWriter {
         out = ClosedWriter.CLOSED_WRITER;
     }
 
+    /**
+     * Creates a proxy that shields the given writer from being closed.
+     *
+     * @param writer the writer to wrap
+     * @return the created proxy
+     * @since 2.9.0
+     */
+    public static CloseShieldWriter wrap(final Writer writer) {
+        return new CloseShieldWriter(writer);
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/input/CloseShieldInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CloseShieldInputStreamTest.java
index 62b6cda..4a09310 100644
--- a/src/test/java/org/apache/commons/io/input/CloseShieldInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/CloseShieldInputStreamTest.java
@@ -48,7 +48,7 @@ public class CloseShieldInputStreamTest {
                 closed = true;
             }
         };
-        shielded = new CloseShieldInputStream(original);
+        shielded = CloseShieldInputStream.wrap(original);
         closed = false;
     }
 
diff --git a/src/test/java/org/apache/commons/io/input/CloseShieldReaderTest.java b/src/test/java/org/apache/commons/io/input/CloseShieldReaderTest.java
index dfc54ca..59a9959 100644
--- a/src/test/java/org/apache/commons/io/input/CloseShieldReaderTest.java
+++ b/src/test/java/org/apache/commons/io/input/CloseShieldReaderTest.java
@@ -42,7 +42,7 @@ public class CloseShieldReaderTest {
     public void setUp() {
         data = "xyz";
         original = spy(new CharSequenceReader(data));
-        shielded = new CloseShieldReader(original);
+        shielded = CloseShieldReader.wrap(original);
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/io/output/CloseShieldOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/CloseShieldOutputStreamTest.java
index 0a563a0..bd9d45f 100644
--- a/src/test/java/org/apache/commons/io/output/CloseShieldOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/output/CloseShieldOutputStreamTest.java
@@ -18,7 +18,7 @@ package org.apache.commons.io.output;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -45,7 +45,7 @@ public class CloseShieldOutputStreamTest {
                 closed = true;
             }
         };
-        shielded = new CloseShieldOutputStream(original);
+        shielded = CloseShieldOutputStream.wrap(original);
         closed = false;
     }
 
@@ -53,12 +53,7 @@ public class CloseShieldOutputStreamTest {
     public void testClose() throws IOException {
         shielded.close();
         assertFalse(closed, "closed");
-        try {
-            shielded.write('x');
-            fail("write(b)");
-        } catch (final IOException ignore) {
-            // expected
-        }
+        assertThrows(IOException.class, () -> shielded.write('x'), "write(b)");
         original.write('y');
         assertEquals(1, original.size());
         assertEquals('y', original.toByteArray()[0]);
diff --git a/src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java b/src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java
index 3fb3eae..a792868 100644
--- a/src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java
+++ b/src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java
@@ -17,7 +17,7 @@
 package org.apache.commons.io.output;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
@@ -40,19 +40,14 @@ public class CloseShieldWriterTest {
     @BeforeEach
     public void setUp() {
         original = spy(new StringBuilderWriter());
-        shielded = new CloseShieldWriter(original);
+        shielded = CloseShieldWriter.wrap(original);
     }
 
     @Test
     public void testClose() throws IOException {
         shielded.close();
         verify(original, never()).close();
-        try {
-            shielded.write('x');
-            fail("write(c)");
-        } catch (final IOException ignore) {
-            // expected
-        }
+        assertThrows(IOException.class, () -> shielded.write('x'), "write(c)");
         original.write('y');
         assertEquals(1, original.getBuilder().length());
         assertEquals('y', original.toString().charAt(0));