Use Objects.requireNonNull()
Javadoc. Reuse own API.
MODIFY
src/main/java/org/apache/commons/io/LineIterator.java
src/main/java/org/apache/commons/io/LineIterator.java
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 static void closeQuietly(final LineIterator iterator) {
* @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 {

MODIFY
src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
src/main/java/org/apache/commons/io/output/DeferredFileOutputStream.java
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 DeferredFileOutputStream(final int threshold, final int initialBufferSize
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 DeferredFileOutputStream(final int threshold, final int initialBufferSize
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 void close() throws IOException {
}
/**
- * 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 void close() throws IOException {
}
/**
- * 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 File getFile() {
}
/**
- * 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 @@ protected OutputStream getStream() throws IOException {
}
/**
- * 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 @@ protected void thresholdReached() throws IOException {
}
/**
- * 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>

MODIFY
src/test/java/org/apache/commons/io/LineIteratorTest.java
src/test/java/org/apache/commons/io/LineIteratorTest.java
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 @@ private void assertLines(final List<String> lines, final LineIterator iterator)
}
assertFalse(iterator.hasNext(), "No more expected");
} finally {
- try {
- IOUtils.close(iterator);
- } catch (final IOException ignored) {
- // Ignored
- }
+ IOUtils.closeQuietly(iterator);
}
}
@@ -105,9 +100,6 @@ private void assertLines(final List<String> lines, final LineIterator iterator)
return lines;
}
-
- // -----------------------------------------------------------------------
-
/**
* Utility method to create and test a file with a specified number of lines.
*
@@ -166,21 +158,17 @@ public void testCloseEarly() throws Exception {
@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 @@ protected boolean isValidLine(final String line) {
// 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 void testFilteringBufferedReader() throws Exception {
this.testFiltering(lines, reader);
}
- // -----------------------------------------------------------------------
@Test
public void testFilteringFileReader() throws Exception {
final String encoding = "UTF-8";

MODIFY
src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
src/test/java/org/apache/commons/io/output/DeferredFileOutputStreamTest.java
@@ -244,7 +244,7 @@ public void testTempFileError() throws Exception {
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());
}
/**

Use Objects.requireNonNull()

Javadoc. Reuse own API.