Skip to content

Commit 8a786ab

Browse files
authored
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
1 parent 167effd commit 8a786ab

8 files changed

Lines changed: 71 additions & 18 deletions

File tree

src/main/java/org/apache/commons/io/input/CloseShieldInputStream.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public class CloseShieldInputStream extends ProxyInputStream {
3535
* closed.
3636
*
3737
* @param in underlying input stream
38+
* @deprecated Using this constructor prevents IDEs from warning if
39+
* the underlying input stream is never closed.
40+
* Use {@link #wrap(InputStream)} instead.
3841
*/
42+
@Deprecated
3943
public CloseShieldInputStream(final InputStream in) {
4044
super(in);
4145
}
@@ -50,4 +54,16 @@ public void close() {
5054
in = ClosedInputStream.CLOSED_INPUT_STREAM;
5155
}
5256

57+
/**
58+
* Creates a proxy that shields the given input stream from being
59+
* closed.
60+
*
61+
* @param inputStream the input stream to wrap
62+
* @return the created proxy
63+
* @since 2.9.0
64+
*/
65+
public static CloseShieldInputStream wrap(final InputStream inputStream) {
66+
return new CloseShieldInputStream(inputStream);
67+
}
68+
5369
}

src/main/java/org/apache/commons/io/input/CloseShieldReader.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public class CloseShieldReader extends ProxyReader {
3535
* closed.
3636
*
3737
* @param in underlying reader
38+
* @deprecated Using this constructor prevents IDEs from warning if
39+
* the underlying reader is never closed.
40+
* Use {@link #wrap(Reader)} instead.
3841
*/
42+
@Deprecated
3943
public CloseShieldReader(final Reader in) {
4044
super(in);
4145
}
@@ -50,4 +54,16 @@ public void close() {
5054
in = ClosedReader.CLOSED_READER;
5155
}
5256

57+
/**
58+
* Creates a proxy that shields the given reader from being
59+
* closed.
60+
*
61+
* @param reader the reader to wrap
62+
* @return the created proxy
63+
* @since 2.9.0
64+
*/
65+
public static CloseShieldReader wrap(final Reader reader) {
66+
return new CloseShieldReader(reader);
67+
}
68+
5369
}

src/main/java/org/apache/commons/io/output/CloseShieldOutputStream.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ public class CloseShieldOutputStream extends ProxyOutputStream {
3535
* closed.
3636
*
3737
* @param out underlying output stream
38+
* @deprecated Using this constructor prevents IDEs from warning if
39+
* the underlying output stream is never closed.
40+
* Use {@link #wrap(OutputStream)} instead.
3841
*/
42+
@Deprecated
3943
public CloseShieldOutputStream(final OutputStream out) {
4044
super(out);
4145
}
@@ -50,4 +54,16 @@ public void close() {
5054
out = ClosedOutputStream.CLOSED_OUTPUT_STREAM;
5155
}
5256

57+
/**
58+
* Creates a proxy that shields the given output stream from being
59+
* closed.
60+
*
61+
* @param outputStream the output stream to wrap
62+
* @return the created proxy
63+
* @since 2.9.0
64+
*/
65+
public static CloseShieldOutputStream wrap(final OutputStream outputStream) {
66+
return new CloseShieldOutputStream(outputStream);
67+
}
68+
5369
}

src/main/java/org/apache/commons/io/output/CloseShieldWriter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public class CloseShieldWriter extends ProxyWriter {
3333
* Creates a proxy that shields the given writer from being closed.
3434
*
3535
* @param out underlying writer
36+
* @deprecated Using this constructor prevents IDEs from warning if
37+
* the underlying writer is never closed.
38+
* Use {@link #wrap(Writer)} instead.
3639
*/
40+
@Deprecated
3741
public CloseShieldWriter(final Writer out) {
3842
super(out);
3943
}
@@ -47,4 +51,15 @@ public void close() {
4751
out = ClosedWriter.CLOSED_WRITER;
4852
}
4953

54+
/**
55+
* Creates a proxy that shields the given writer from being closed.
56+
*
57+
* @param writer the writer to wrap
58+
* @return the created proxy
59+
* @since 2.9.0
60+
*/
61+
public static CloseShieldWriter wrap(final Writer writer) {
62+
return new CloseShieldWriter(writer);
63+
}
64+
5065
}

src/test/java/org/apache/commons/io/input/CloseShieldInputStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void close() {
4848
closed = true;
4949
}
5050
};
51-
shielded = new CloseShieldInputStream(original);
51+
shielded = CloseShieldInputStream.wrap(original);
5252
closed = false;
5353
}
5454

src/test/java/org/apache/commons/io/input/CloseShieldReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class CloseShieldReaderTest {
4242
public void setUp() {
4343
data = "xyz";
4444
original = spy(new CharSequenceReader(data));
45-
shielded = new CloseShieldReader(original);
45+
shielded = CloseShieldReader.wrap(original);
4646
}
4747

4848
@Test

src/test/java/org/apache/commons/io/output/CloseShieldOutputStreamTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020
import static org.junit.jupiter.api.Assertions.assertFalse;
21-
import static org.junit.jupiter.api.Assertions.fail;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2222

2323
import java.io.IOException;
2424
import java.io.OutputStream;
@@ -45,20 +45,15 @@ public void close() {
4545
closed = true;
4646
}
4747
};
48-
shielded = new CloseShieldOutputStream(original);
48+
shielded = CloseShieldOutputStream.wrap(original);
4949
closed = false;
5050
}
5151

5252
@Test
5353
public void testClose() throws IOException {
5454
shielded.close();
5555
assertFalse(closed, "closed");
56-
try {
57-
shielded.write('x');
58-
fail("write(b)");
59-
} catch (final IOException ignore) {
60-
// expected
61-
}
56+
assertThrows(IOException.class, () -> shielded.write('x'), "write(b)");
6257
original.write('y');
6358
assertEquals(1, original.size());
6459
assertEquals('y', original.toByteArray()[0]);

src/test/java/org/apache/commons/io/output/CloseShieldWriterTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.apache.commons.io.output;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20-
import static org.junit.jupiter.api.Assertions.fail;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
2121
import static org.mockito.Mockito.never;
2222
import static org.mockito.Mockito.spy;
2323
import static org.mockito.Mockito.verify;
@@ -40,19 +40,14 @@ public class CloseShieldWriterTest {
4040
@BeforeEach
4141
public void setUp() {
4242
original = spy(new StringBuilderWriter());
43-
shielded = new CloseShieldWriter(original);
43+
shielded = CloseShieldWriter.wrap(original);
4444
}
4545

4646
@Test
4747
public void testClose() throws IOException {
4848
shielded.close();
4949
verify(original, never()).close();
50-
try {
51-
shielded.write('x');
52-
fail("write(c)");
53-
} catch (final IOException ignore) {
54-
// expected
55-
}
50+
assertThrows(IOException.class, () -> shielded.write('x'), "write(c)");
5651
original.write('y');
5752
assertEquals(1, original.getBuilder().length());
5853
assertEquals('y', original.toString().charAt(0));

0 commit comments

Comments
 (0)