Add IOExceptionList.checkEmpty(List, Object).
MODIFY
src/changes/changes.xml
src/changes/changes.xml
@@ -281,6 +281,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">
Add PathUtils.readAttributes(Path, Class, LinkOption...). #290
</action>
+ <action dev="ggregory" type="add" due-to="Gary Gregory">
+ Add IOExceptionList.checkEmpty(List, Object).
+ </action>
<!-- UPDATE -->
<action dev="ggregory" type="add" due-to="Gary Gregory">
Update FileEntry to use FileTime instead of long for file time stamps.

MODIFY
src/main/java/org/apache/commons/io/FileUtils.java
src/main/java/org/apache/commons/io/FileUtils.java
@@ -342,7 +342,7 @@ public static long checksumCRC32(final File file) throws IOException {
public static void cleanDirectory(final File directory) throws IOException {
final File[] files = listFiles(directory, null);
- final List<Exception> causeList = new ArrayList<>();
+ final List<IOException> causeList = new ArrayList<>();
for (final File file : files) {
try {
forceDelete(file);
@@ -350,10 +350,7 @@ public static void cleanDirectory(final File directory) throws IOException {
causeList.add(ioe);
}
}
-
- if (!causeList.isEmpty()) {
- throw new IOExceptionList(directory.toString(), causeList);
- }
+ IOExceptionList.checkEmpty(causeList, directory);
}
/**
@@ -368,7 +365,7 @@ public static void cleanDirectory(final File directory) throws IOException {
private static void cleanDirectoryOnExit(final File directory) throws IOException {
final File[] files = listFiles(directory, null);
- final List<Exception> causeList = new ArrayList<>();
+ final List<IOException> causeList = new ArrayList<>();
for (final File file : files) {
try {
forceDeleteOnExit(file);
@@ -376,10 +373,7 @@ private static void cleanDirectoryOnExit(final File directory) throws IOExceptio
causeList.add(ioe);
}
}
-
- if (!causeList.isEmpty()) {
- throw new IOExceptionList(causeList);
- }
+ IOExceptionList.checkEmpty(causeList, directory);
}
/**

MODIFY
src/main/java/org/apache/commons/io/IOExceptionList.java
src/main/java/org/apache/commons/io/IOExceptionList.java
import java.io.IOException;
import java.util.Collections;
import java.util.List;
+import java.util.Objects;
/**
* A IOException based on a list of Throwable causes.
public class IOExceptionList extends IOException {
private static final long serialVersionUID = 1L;
+
+ /**
+ * Throws this exception if the list is not null or empty.
+ *
+ * @param causeList The list to test.
+ * @param message The detail message, see {@link #getMessage()}.
+ * @throws IOExceptionList if the list is not null or empty.
+ * @since 2.12.0
+ */
+ public static void checkEmpty(final List<? extends Throwable> causeList, final Object message) throws IOExceptionList {
+ if (!isEmpty(causeList)) {
+ throw new IOExceptionList(Objects.toString(message, null), causeList);
+ }
+ }
+
+ private static boolean isEmpty(final List<? extends Throwable> causeList) {
+ return causeList == null || causeList.isEmpty();
+ }
+
private static String toMessage(final List<? extends Throwable> causeList) {
return String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList);
}
@@ -56,7 +76,7 @@ public IOExceptionList(final List<? extends Throwable> causeList) {
* @since 2.9.0
*/
public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
- super(message != null ? message : toMessage(causeList), causeList == null || causeList.isEmpty() ? null : causeList.get(0));
+ super(message != null ? message : toMessage(causeList), isEmpty(causeList) ? null : causeList.get(0));
this.causeList = causeList == null ? Collections.emptyList() : causeList;
}

MODIFY
src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
@@ -81,7 +81,7 @@ protected FilterCollectionWriter(final Writer... writers) {
* @param e The cause.
* @return the given list or a new list on null input.
*/
- private List<Exception> add(List<Exception> causeList, final int i, final IOException e) {
+ private List<IOException> add(List<IOException> causeList, final int i, final IOException e) {
if (causeList == null) {
causeList = new ArrayList<>();
}
@@ -91,7 +91,7 @@ protected FilterCollectionWriter(final Writer... writers) {
@Override
public Writer append(final char c) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -103,15 +103,13 @@ public Writer append(final char c) throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("append", causeList);
- }
+ IOExceptionList.checkEmpty(causeList, "append(char)");
return this;
}
@Override
public Writer append(final CharSequence csq) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -123,16 +121,14 @@ public Writer append(final CharSequence csq) throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("append", causeList);
- }
+ IOExceptionList.checkEmpty(causeList, "append(CharSequence)");
return this;
}
@Override
public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -144,15 +140,13 @@ public Writer append(final CharSequence csq, final int start, final int end) thr
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("append", causeList);
- }
+ IOExceptionList.checkEmpty(causeList, "append(CharSequence, int, int)");
return this;
}
@Override
public void close() throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -164,10 +158,7 @@ public void close() throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("close", causeList);
- }
-
+ IOExceptionList.checkEmpty(causeList, "close()");
}
/**
@@ -177,7 +168,7 @@ public void close() throws IOException {
*/
@Override
public void flush() throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -189,25 +180,12 @@ public void flush() throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("flush", causeList);
- }
-
- }
-
- /**
- * Tests if the given list is empty in a null-safe manner.
- *
- * @param causeList the list to test.
- * @return true if empty or null.
- */
- private boolean notEmpty(final List<Exception> causeList) {
- return causeList != null && !causeList.isEmpty();
+ IOExceptionList.checkEmpty(causeList, "flush()");
}
@Override
public void write(final char[] cbuf) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -219,9 +197,7 @@ public void write(final char[] cbuf) throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("write", causeList);
- }
+ IOExceptionList.checkEmpty(causeList, "write(char[])");
}
/**
@@ -235,7 +211,7 @@ public void write(final char[] cbuf) throws IOException {
*/
@Override
public void write(final char[] cbuf, final int off, final int len) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -247,9 +223,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("write", causeList);
- }
+ IOExceptionList.checkEmpty(causeList, "write(char[], int, int)");
}
/**
@@ -259,7 +233,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
*/
@Override
public void write(final int c) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -271,14 +245,12 @@ public void write(final int c) throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("write", causeList);
- }
+ IOExceptionList.checkEmpty(causeList, "write(int)");
}
@Override
public void write(final String str) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -290,10 +262,7 @@ public void write(final String str) throws IOException {
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("write", causeList);
- }
-
+ IOExceptionList.checkEmpty(causeList, "write(String)");
}
/**
@@ -307,7 +276,7 @@ public void write(final String str) throws IOException {
*/
@Override
public void write(final String str, final int off, final int len) throws IOException {
- List<Exception> causeList = null;
+ List<IOException> causeList = null;
int i = 0;
for (final Writer w : writers) {
if (w != null) {
@@ -319,10 +288,7 @@ public void write(final String str, final int off, final int len) throws IOExcep
}
i++;
}
- if (notEmpty(causeList)) {
- throw new IOExceptionList("write", causeList);
- }
-
+ IOExceptionList.checkEmpty(causeList, "write(String, int, int)");
}
}

MODIFY
src/test/java/org/apache/commons/io/IOExceptionListTest.java
src/test/java/org/apache/commons/io/IOExceptionListTest.java
package org.apache.commons.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.EOFException;
@@ -50,6 +51,16 @@ public void testCause() {
assertEquals(list, causeList);
}
+ @Test
+ public void testCheckEmpty() throws IOExceptionList {
+ IOExceptionList.checkEmpty(null, "");
+ IOExceptionList.checkEmpty(null, null);
+ IOExceptionList.checkEmpty(Collections.emptyList(), "");
+ IOExceptionList.checkEmpty(Collections.emptyList(), null);
+ assertThrows(IOExceptionList.class, () -> IOExceptionList.checkEmpty(Collections.singletonList(new Exception()), ""));
+ assertThrows(IOExceptionList.class, () -> IOExceptionList.checkEmpty(Collections.singletonList(new Exception()), null));
+ }
+
@Test
public void testEmptyList() {
new IOExceptionList(Collections.emptyList());

Add IOExceptionList.checkEmpty(List, Object).