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
commit fffa8406955ca838a53e5092a6300df4d579baa9 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Jun 21 09:30:12 2022 -0400 Use Objects.requireNonNull() Javadoc. Reuse own API. --- .../java/org/apache/commons/io/LineIterator.java | 5 ++-- .../io/output/DeferredFileOutputStream.java | 19 ++++++------- .../org/apache/commons/io/LineIteratorTest.java | 31 +++++----------------- .../io/output/DeferredFileOutputStreamTest.java | 2 +- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/apache/commons/io/LineIterator.java b/src/main/java/org/apache/commons/io/LineIterator.java index 67fe5728..b1b05d5c 100644 --- a/src/main/java/org/apache/commons/io/LineIterator.java +++ b/src/main/java/org/apache/commons/io/LineIterator.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.io.Reader; import java.util.Iterator; import java.util.NoSuchElementException; +import java.util.Objects; /** * An Iterator over the lines in a {@link Reader}. @@ -78,9 +79,7 @@ public class LineIterator implements Iterator<String>, Closeable { * @throws IllegalArgumentException if the reader is null */ public LineIterator(final Reader reader) throws IllegalArgumentException { - if (reader == null) { - throw new IllegalArgumentException("Reader must not be null"); - } + Objects.requireNonNull(reader, "reader"); if (reader instanceof BufferedReader) { bufferedReader = (BufferedReader) reader; } else { diff --git a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java index 143587a4..4e419fab 100644 --- a/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java +++ b/src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java @@ -22,6 +22,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Objects; import java.util.function.Supplier; import org.apache.commons.io.file.PathUtils; @@ -140,9 +141,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { public DeferredFileOutputStream(final int threshold, final int initialBufferSize, final String prefix, final String suffix, final File directory) { this(threshold, null, prefix, suffix, directory, initialBufferSize); - if (prefix == null) { - throw new IllegalArgumentException("Temporary file prefix is missing"); - } + Objects.requireNonNull("prefix", prefix); if (initialBufferSize < 0) { throw new IllegalArgumentException("Initial buffer size must be atleast 0."); } @@ -163,9 +162,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { public DeferredFileOutputStream(final int threshold, final String prefix, final String suffix, final File directory) { this(threshold, null, prefix, suffix, directory, AbstractByteArrayOutputStream.DEFAULT_SIZE); - if (prefix == null) { - throw new IllegalArgumentException("Temporary file prefix is missing"); - } + Objects.requireNonNull(prefix, "prefix"); } /** @@ -180,7 +177,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { } /** - * Returns the data for this output stream as an array of bytes, assuming that the data has been retained in memory. + * Gets the data for this output stream as an array of bytes, assuming that the data has been retained in memory. * If the data was written to disk, this method returns {@code null}. * * @return The data for this output stream, or {@code null} if no such data is available. @@ -190,7 +187,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { } /** - * Returns either the output file specified in the constructor or the temporary file created or null. + * Gets either the output file specified in the constructor or the temporary file created or null. * <p> * If the constructor specifying the file is used then it returns that same output file, even when threshold has not * been reached. @@ -205,7 +202,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { } /** - * Returns the current output stream. This may be memory based or disk based, depending on the current state with + * Gets the current output stream. This may be memory based or disk based, depending on the current state with * respect to the threshold. * * @return The underlying output stream. @@ -218,7 +215,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { } /** - * Determines whether or not the data for this output stream has been retained in memory. + * Tests whether or not the data for this output stream has been retained in memory. * * @return {@code true} if the data is available in memory; {@code false} otherwise. */ @@ -251,7 +248,7 @@ public class DeferredFileOutputStream extends ThresholdingOutputStream { } /** - * Gets the current contents of this byte stream as an {@link InputStream}. + * Converts the current contents of this byte stream to an {@link InputStream}. * If the data for this output stream has been retained in memory, the * returned stream is backed by buffers of {@code this} stream, * avoiding memory allocation and copy, thus saving space and time.<br> diff --git a/src/test/java/org/apache/commons/io/LineIteratorTest.java b/src/test/java/org/apache/commons/io/LineIteratorTest.java index e6cb8fde..a8c1ea5f 100644 --- a/src/test/java/org/apache/commons/io/LineIteratorTest.java +++ b/src/test/java/org/apache/commons/io/LineIteratorTest.java @@ -38,8 +38,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; /** - * This is used to test LineIterator for correctness. - * + * Tests {@link LineIterator}. */ public class LineIteratorTest { @@ -54,11 +53,7 @@ public class LineIteratorTest { } assertFalse(iterator.hasNext(), "No more expected"); } finally { - try { - IOUtils.close(iterator); - } catch (final IOException ignored) { - // Ignored - } + IOUtils.closeQuietly(iterator); } } @@ -105,9 +100,6 @@ public class LineIteratorTest { return lines; } - - // ----------------------------------------------------------------------- - /** * Utility method to create and test a file with a specified number of lines. * @@ -166,21 +158,17 @@ public class LineIteratorTest { @Test public void testConstructor() { - assertThrows(IllegalArgumentException.class, () -> { - try (LineIterator li = new LineIterator(null)) { - } - }); + assertThrows(NullPointerException.class, () -> new LineIterator(null)); } - private void testFiltering(final List<String> lines, final Reader reader) { - final LineIterator iterator = new LineIterator(reader) { + private void testFiltering(final List<String> lines, final Reader reader) throws IOException { + try (LineIterator iterator = new LineIterator(reader) { @Override protected boolean isValidLine(final String line) { final char c = line.charAt(line.length() - 1); return (c - 48) % 3 != 1; } - }; - try { + }) { assertThrows(UnsupportedOperationException.class, iterator::remove); int idx = 0; @@ -202,12 +190,6 @@ public class LineIteratorTest { // try calling next() after file processed assertThrows(NoSuchElementException.class, iterator::next); assertThrows(NoSuchElementException.class, iterator::nextLine); - } finally { - try { - IOUtils.close(iterator); - } catch (final IOException ignored) { - // Ignored - } } } @@ -223,7 +205,6 @@ public class LineIteratorTest { this.testFiltering(lines, reader); } - // ----------------------------------------------------------------------- @Test public void testFilteringFileReader() throws Exception { final String encoding = "UTF-8"; diff --git a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java index 30a8ac30..0dfb4ecf 100644 --- a/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java +++ b/src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java @@ -244,7 +244,7 @@ public class DeferredFileOutputStreamTest { final String prefix = null; final String suffix = ".out"; final File tempDir = FileUtils.current(); - assertThrows(IllegalArgumentException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close()); + assertThrows(NullPointerException.class, () -> new DeferredFileOutputStream(testBytes.length - 5, prefix, suffix, tempDir).close()); } /**