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 d3b043a Add IOExceptionList.checkEmpty(List, Object). d3b043a is described below commit d3b043a2ce926509fe4ed04160db7a73339a2964 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Sat Nov 6 09:35:58 2021 -0400 Add IOExceptionList.checkEmpty(List, Object). --- src/changes/changes.xml | 3 + src/main/java/org/apache/commons/io/FileUtils.java | 14 ++-- .../org/apache/commons/io/IOExceptionList.java | 22 ++++++- .../commons/io/output/FilterCollectionWriter.java | 76 ++++++---------------- .../org/apache/commons/io/IOExceptionListTest.java | 13 +++- 5 files changed, 61 insertions(+), 67 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 33d75cb..9e6ad1d 100644 --- a/src/changes/changes.xml +++ b/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. diff --git a/src/main/java/org/apache/commons/io/FileUtils.java b/src/main/java/org/apache/commons/io/FileUtils.java index 0a4ba1b..ced2eaa 100644 --- a/src/main/java/org/apache/commons/io/FileUtils.java +++ b/src/main/java/org/apache/commons/io/FileUtils.java @@ -342,7 +342,7 @@ public class FileUtils { 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 class FileUtils { causeList.add(ioe); } } - - if (!causeList.isEmpty()) { - throw new IOExceptionList(directory.toString(), causeList); - } + IOExceptionList.checkEmpty(causeList, directory); } /** @@ -368,7 +365,7 @@ public class FileUtils { 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 @@ public class FileUtils { causeList.add(ioe); } } - - if (!causeList.isEmpty()) { - throw new IOExceptionList(causeList); - } + IOExceptionList.checkEmpty(causeList, directory); } /** diff --git a/src/main/java/org/apache/commons/io/IOExceptionList.java b/src/main/java/org/apache/commons/io/IOExceptionList.java index 6e394e9..fb6a11a 100644 --- a/src/main/java/org/apache/commons/io/IOExceptionList.java +++ b/src/main/java/org/apache/commons/io/IOExceptionList.java @@ -20,6 +20,7 @@ package org.apache.commons.io; 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. @@ -33,6 +34,25 @@ import java.util.List; 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 class IOExceptionList extends IOException { * @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; } diff --git a/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java b/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java index 82f01f7..58400b7 100644 --- a/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java +++ b/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java @@ -81,7 +81,7 @@ public class FilterCollectionWriter extends Writer { * @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 @@ public class FilterCollectionWriter extends Writer { @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 class FilterCollectionWriter extends Writer { } 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 class FilterCollectionWriter extends Writer { } 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 class FilterCollectionWriter extends Writer { } 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 class FilterCollectionWriter extends Writer { } i++; } - if (notEmpty(causeList)) { - throw new IOExceptionList("close", causeList); - } - + IOExceptionList.checkEmpty(causeList, "close()"); } /** @@ -177,7 +168,7 @@ public class FilterCollectionWriter extends Writer { */ @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 class FilterCollectionWriter extends Writer { } 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 class FilterCollectionWriter extends Writer { } i++; } - if (notEmpty(causeList)) { - throw new IOExceptionList("write", causeList); - } + IOExceptionList.checkEmpty(causeList, "write(char[])"); } /** @@ -235,7 +211,7 @@ public class FilterCollectionWriter extends Writer { */ @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 class FilterCollectionWriter extends Writer { } i++; } - if (notEmpty(causeList)) { - throw new IOExceptionList("write", causeList); - } + IOExceptionList.checkEmpty(causeList, "write(char[], int, int)"); } /** @@ -259,7 +233,7 @@ public class FilterCollectionWriter extends Writer { */ @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 class FilterCollectionWriter extends Writer { } 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 class FilterCollectionWriter extends Writer { } i++; } - if (notEmpty(causeList)) { - throw new IOExceptionList("write", causeList); - } - + IOExceptionList.checkEmpty(causeList, "write(String)"); } /** @@ -307,7 +276,7 @@ public class FilterCollectionWriter extends Writer { */ @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 class FilterCollectionWriter extends Writer { } i++; } - if (notEmpty(causeList)) { - throw new IOExceptionList("write", causeList); - } - + IOExceptionList.checkEmpty(causeList, "write(String, int, int)"); } } diff --git a/src/test/java/org/apache/commons/io/IOExceptionListTest.java b/src/test/java/org/apache/commons/io/IOExceptionListTest.java index 7a41ee3..059ce14 100644 --- a/src/test/java/org/apache/commons/io/IOExceptionListTest.java +++ b/src/test/java/org/apache/commons/io/IOExceptionListTest.java @@ -18,8 +18,9 @@ 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; @@ -51,6 +52,16 @@ public class IOExceptionListTest { } @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()); new IOExceptionList("foo", Collections.emptyList());