Skip to content

Commit d3b043a

Browse files
author
Gary Gregory
committed
Add IOExceptionList.checkEmpty(List, Object).
1 parent 03ebf60 commit d3b043a

5 files changed

Lines changed: 61 additions & 67 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ The <action> type attribute can be add,update,fix,remove.
281281
<action dev="ggregory" type="add" due-to="Gary Gregory">
282282
Add PathUtils.readAttributes(Path, Class, LinkOption...). #290
283283
</action>
284+
<action dev="ggregory" type="add" due-to="Gary Gregory">
285+
Add IOExceptionList.checkEmpty(List, Object).
286+
</action>
284287
<!-- UPDATE -->
285288
<action dev="ggregory" type="add" due-to="Gary Gregory">
286289
Update FileEntry to use FileTime instead of long for file time stamps.

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,15 @@ public static long checksumCRC32(final File file) throws IOException {
342342
public static void cleanDirectory(final File directory) throws IOException {
343343
final File[] files = listFiles(directory, null);
344344

345-
final List<Exception> causeList = new ArrayList<>();
345+
final List<IOException> causeList = new ArrayList<>();
346346
for (final File file : files) {
347347
try {
348348
forceDelete(file);
349349
} catch (final IOException ioe) {
350350
causeList.add(ioe);
351351
}
352352
}
353-
354-
if (!causeList.isEmpty()) {
355-
throw new IOExceptionList(directory.toString(), causeList);
356-
}
353+
IOExceptionList.checkEmpty(causeList, directory);
357354
}
358355

359356
/**
@@ -368,18 +365,15 @@ public static void cleanDirectory(final File directory) throws IOException {
368365
private static void cleanDirectoryOnExit(final File directory) throws IOException {
369366
final File[] files = listFiles(directory, null);
370367

371-
final List<Exception> causeList = new ArrayList<>();
368+
final List<IOException> causeList = new ArrayList<>();
372369
for (final File file : files) {
373370
try {
374371
forceDeleteOnExit(file);
375372
} catch (final IOException ioe) {
376373
causeList.add(ioe);
377374
}
378375
}
379-
380-
if (!causeList.isEmpty()) {
381-
throw new IOExceptionList(causeList);
382-
}
376+
IOExceptionList.checkEmpty(causeList, directory);
383377
}
384378

385379
/**

src/main/java/org/apache/commons/io/IOExceptionList.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.Objects;
2324

2425
/**
2526
* A IOException based on a list of Throwable causes.
@@ -33,6 +34,25 @@
3334
public class IOExceptionList extends IOException {
3435

3536
private static final long serialVersionUID = 1L;
37+
38+
/**
39+
* Throws this exception if the list is not null or empty.
40+
*
41+
* @param causeList The list to test.
42+
* @param message The detail message, see {@link #getMessage()}.
43+
* @throws IOExceptionList if the list is not null or empty.
44+
* @since 2.12.0
45+
*/
46+
public static void checkEmpty(final List<? extends Throwable> causeList, final Object message) throws IOExceptionList {
47+
if (!isEmpty(causeList)) {
48+
throw new IOExceptionList(Objects.toString(message, null), causeList);
49+
}
50+
}
51+
52+
private static boolean isEmpty(final List<? extends Throwable> causeList) {
53+
return causeList == null || causeList.isEmpty();
54+
}
55+
3656
private static String toMessage(final List<? extends Throwable> causeList) {
3757
return String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList);
3858
}
@@ -56,7 +76,7 @@ public IOExceptionList(final List<? extends Throwable> causeList) {
5676
* @since 2.9.0
5777
*/
5878
public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
59-
super(message != null ? message : toMessage(causeList), causeList == null || causeList.isEmpty() ? null : causeList.get(0));
79+
super(message != null ? message : toMessage(causeList), isEmpty(causeList) ? null : causeList.get(0));
6080
this.causeList = causeList == null ? Collections.emptyList() : causeList;
6181
}
6282

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

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected FilterCollectionWriter(final Writer... writers) {
8181
* @param e The cause.
8282
* @return the given list or a new list on null input.
8383
*/
84-
private List<Exception> add(List<Exception> causeList, final int i, final IOException e) {
84+
private List<IOException> add(List<IOException> causeList, final int i, final IOException e) {
8585
if (causeList == null) {
8686
causeList = new ArrayList<>();
8787
}
@@ -91,7 +91,7 @@ private List<Exception> add(List<Exception> causeList, final int i, final IOExce
9191

9292
@Override
9393
public Writer append(final char c) throws IOException {
94-
List<Exception> causeList = null;
94+
List<IOException> causeList = null;
9595
int i = 0;
9696
for (final Writer w : writers) {
9797
if (w != null) {
@@ -103,15 +103,13 @@ public Writer append(final char c) throws IOException {
103103
}
104104
i++;
105105
}
106-
if (notEmpty(causeList)) {
107-
throw new IOExceptionList("append", causeList);
108-
}
106+
IOExceptionList.checkEmpty(causeList, "append(char)");
109107
return this;
110108
}
111109

112110
@Override
113111
public Writer append(final CharSequence csq) throws IOException {
114-
List<Exception> causeList = null;
112+
List<IOException> causeList = null;
115113
int i = 0;
116114
for (final Writer w : writers) {
117115
if (w != null) {
@@ -123,16 +121,14 @@ public Writer append(final CharSequence csq) throws IOException {
123121
}
124122
i++;
125123
}
126-
if (notEmpty(causeList)) {
127-
throw new IOExceptionList("append", causeList);
128-
}
124+
IOExceptionList.checkEmpty(causeList, "append(CharSequence)");
129125
return this;
130126
}
131127

132128
@Override
133129
public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
134130

135-
List<Exception> causeList = null;
131+
List<IOException> causeList = null;
136132
int i = 0;
137133
for (final Writer w : writers) {
138134
if (w != null) {
@@ -144,15 +140,13 @@ public Writer append(final CharSequence csq, final int start, final int end) thr
144140
}
145141
i++;
146142
}
147-
if (notEmpty(causeList)) {
148-
throw new IOExceptionList("append", causeList);
149-
}
143+
IOExceptionList.checkEmpty(causeList, "append(CharSequence, int, int)");
150144
return this;
151145
}
152146

153147
@Override
154148
public void close() throws IOException {
155-
List<Exception> causeList = null;
149+
List<IOException> causeList = null;
156150
int i = 0;
157151
for (final Writer w : writers) {
158152
if (w != null) {
@@ -164,10 +158,7 @@ public void close() throws IOException {
164158
}
165159
i++;
166160
}
167-
if (notEmpty(causeList)) {
168-
throw new IOExceptionList("close", causeList);
169-
}
170-
161+
IOExceptionList.checkEmpty(causeList, "close()");
171162
}
172163

173164
/**
@@ -177,7 +168,7 @@ public void close() throws IOException {
177168
*/
178169
@Override
179170
public void flush() throws IOException {
180-
List<Exception> causeList = null;
171+
List<IOException> causeList = null;
181172
int i = 0;
182173
for (final Writer w : writers) {
183174
if (w != null) {
@@ -189,25 +180,12 @@ public void flush() throws IOException {
189180
}
190181
i++;
191182
}
192-
if (notEmpty(causeList)) {
193-
throw new IOExceptionList("flush", causeList);
194-
}
195-
196-
}
197-
198-
/**
199-
* Tests if the given list is empty in a null-safe manner.
200-
*
201-
* @param causeList the list to test.
202-
* @return true if empty or null.
203-
*/
204-
private boolean notEmpty(final List<Exception> causeList) {
205-
return causeList != null && !causeList.isEmpty();
183+
IOExceptionList.checkEmpty(causeList, "flush()");
206184
}
207185

208186
@Override
209187
public void write(final char[] cbuf) throws IOException {
210-
List<Exception> causeList = null;
188+
List<IOException> causeList = null;
211189
int i = 0;
212190
for (final Writer w : writers) {
213191
if (w != null) {
@@ -219,9 +197,7 @@ public void write(final char[] cbuf) throws IOException {
219197
}
220198
i++;
221199
}
222-
if (notEmpty(causeList)) {
223-
throw new IOExceptionList("write", causeList);
224-
}
200+
IOExceptionList.checkEmpty(causeList, "write(char[])");
225201
}
226202

227203
/**
@@ -235,7 +211,7 @@ public void write(final char[] cbuf) throws IOException {
235211
*/
236212
@Override
237213
public void write(final char[] cbuf, final int off, final int len) throws IOException {
238-
List<Exception> causeList = null;
214+
List<IOException> causeList = null;
239215
int i = 0;
240216
for (final Writer w : writers) {
241217
if (w != null) {
@@ -247,9 +223,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
247223
}
248224
i++;
249225
}
250-
if (notEmpty(causeList)) {
251-
throw new IOExceptionList("write", causeList);
252-
}
226+
IOExceptionList.checkEmpty(causeList, "write(char[], int, int)");
253227
}
254228

255229
/**
@@ -259,7 +233,7 @@ public void write(final char[] cbuf, final int off, final int len) throws IOExce
259233
*/
260234
@Override
261235
public void write(final int c) throws IOException {
262-
List<Exception> causeList = null;
236+
List<IOException> causeList = null;
263237
int i = 0;
264238
for (final Writer w : writers) {
265239
if (w != null) {
@@ -271,14 +245,12 @@ public void write(final int c) throws IOException {
271245
}
272246
i++;
273247
}
274-
if (notEmpty(causeList)) {
275-
throw new IOExceptionList("write", causeList);
276-
}
248+
IOExceptionList.checkEmpty(causeList, "write(int)");
277249
}
278250

279251
@Override
280252
public void write(final String str) throws IOException {
281-
List<Exception> causeList = null;
253+
List<IOException> causeList = null;
282254
int i = 0;
283255
for (final Writer w : writers) {
284256
if (w != null) {
@@ -290,10 +262,7 @@ public void write(final String str) throws IOException {
290262
}
291263
i++;
292264
}
293-
if (notEmpty(causeList)) {
294-
throw new IOExceptionList("write", causeList);
295-
}
296-
265+
IOExceptionList.checkEmpty(causeList, "write(String)");
297266
}
298267

299268
/**
@@ -307,7 +276,7 @@ public void write(final String str) throws IOException {
307276
*/
308277
@Override
309278
public void write(final String str, final int off, final int len) throws IOException {
310-
List<Exception> causeList = null;
279+
List<IOException> causeList = null;
311280
int i = 0;
312281
for (final Writer w : writers) {
313282
if (w != null) {
@@ -319,10 +288,7 @@ public void write(final String str, final int off, final int len) throws IOExcep
319288
}
320289
i++;
321290
}
322-
if (notEmpty(causeList)) {
323-
throw new IOExceptionList("write", causeList);
324-
}
325-
291+
IOExceptionList.checkEmpty(causeList, "write(String, int, int)");
326292
}
327293

328294
}

src/test/java/org/apache/commons/io/IOExceptionListTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
package org.apache.commons.io;
1919

2020
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.assertNull;
2221
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

2526
import java.io.EOFException;
@@ -50,6 +51,16 @@ public void testCause() {
5051
assertEquals(list, causeList);
5152
}
5253

54+
@Test
55+
public void testCheckEmpty() throws IOExceptionList {
56+
IOExceptionList.checkEmpty(null, "");
57+
IOExceptionList.checkEmpty(null, null);
58+
IOExceptionList.checkEmpty(Collections.emptyList(), "");
59+
IOExceptionList.checkEmpty(Collections.emptyList(), null);
60+
assertThrows(IOExceptionList.class, () -> IOExceptionList.checkEmpty(Collections.singletonList(new Exception()), ""));
61+
assertThrows(IOExceptionList.class, () -> IOExceptionList.checkEmpty(Collections.singletonList(new Exception()), null));
62+
}
63+
5364
@Test
5465
public void testEmptyList() {
5566
new IOExceptionList(Collections.emptyList());

0 commit comments

Comments
 (0)