You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/09/13 00:20:25 UTC

[commons-io] 02/02: IOExceptionList implements Iterable.

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 5e0998b9f1ba3b3975cecd18b4285540b02a9749
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Sep 12 17:20:17 2022 -0700

    IOExceptionList implements Iterable.
---
 src/changes/changes.xml                                 |  3 +++
 .../java/org/apache/commons/io/IOExceptionList.java     |  8 +++++++-
 .../java/org/apache/commons/io/IOExceptionListTest.java | 17 +++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 9d950914..e235e294 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -442,6 +442,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add TimestampedObserver.isClosed().
       </action>
+      <action dev="ggregory" type="add" due-to="Gary Gregory">
+        IOExceptionList implements Iterable.
+      </action>
       <!-- UPDATE -->
       <action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
         Bump actions/cache from 2.1.6 to 3.0.8 #307, #337.
diff --git a/src/main/java/org/apache/commons/io/IOExceptionList.java b/src/main/java/org/apache/commons/io/IOExceptionList.java
index e4343576..16e13b6a 100644
--- a/src/main/java/org/apache/commons/io/IOExceptionList.java
+++ b/src/main/java/org/apache/commons/io/IOExceptionList.java
@@ -19,6 +19,7 @@ package org.apache.commons.io;
 
 import java.io.IOException;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Objects;
 
@@ -31,7 +32,7 @@ import java.util.Objects;
  *
  * @since 2.7
  */
-public class IOExceptionList extends IOException {
+public class IOExceptionList extends IOException implements Iterable<Throwable> {
 
     private static final long serialVersionUID = 1L;
 
@@ -124,4 +125,9 @@ public class IOExceptionList extends IOException {
         return (List<T>) causeList;
     }
 
+    @Override
+    public Iterator<Throwable> iterator() {
+        return getCauseList().iterator();
+    }
+
 }
diff --git a/src/test/java/org/apache/commons/io/IOExceptionListTest.java b/src/test/java/org/apache/commons/io/IOExceptionListTest.java
index 19275fa4..aad7de51 100644
--- a/src/test/java/org/apache/commons/io/IOExceptionListTest.java
+++ b/src/test/java/org/apache/commons/io/IOExceptionListTest.java
@@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import java.io.EOFException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -67,6 +68,22 @@ public class IOExceptionListTest {
         new IOExceptionList("foo", Collections.emptyList());
     }
 
+    @Test
+    public void testIterable() {
+        final EOFException cause = new EOFException();
+        final List<EOFException> list = Collections.singletonList(cause);
+        final IOExceptionList sqlExceptionList = new IOExceptionList("Hello", list);
+        //
+        assertEquals(list, sqlExceptionList.getCauseList());
+        // No CCE:
+        final List<EOFException> causeList = sqlExceptionList.getCauseList();
+        assertEquals(list, causeList);
+        //
+        final List<Throwable> list2 = new ArrayList<>();
+        sqlExceptionList.forEach(list2::add);
+        assertEquals(list2, causeList);
+    }
+
     @Test
     public void testMessageCause() {
         final EOFException cause = new EOFException();