View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Objects;
26  
27  /**
28   * An IOException based on a list of Throwable causes.
29   * <p>
30   * The first exception in the list is used as this exception's cause and is accessible with the usual
31   * {@link #getCause()} while the complete list is accessible with {@link #getCauseList()}.
32   * </p>
33   *
34   * @since 2.7
35   */
36  public class IOExceptionList extends IOException implements Iterable<Throwable> {
37  
38      private static final long serialVersionUID = 1L;
39  
40      /**
41       * Throws this exception if the list is not null or empty.
42       *
43       * @param causeList The list to test.
44       * @param message The detail message, see {@link #getMessage()}.
45       * @throws IOExceptionList if the list is not null or empty.
46       * @since 2.12.0
47       */
48      public static void checkEmpty(final List<? extends Throwable> causeList, final Object message) throws IOExceptionList {
49          if (!isEmpty(causeList)) {
50              throw new IOExceptionList(Objects.toString(message, null), causeList);
51          }
52      }
53  
54      private static boolean isEmpty(final List<? extends Throwable> causeList) {
55          return size(causeList) == 0;
56      }
57  
58      private static int size(final List<? extends Throwable> causeList) {
59          return causeList != null ? causeList.size() : 0;
60      }
61  
62      private static String toMessage(final List<? extends Throwable> causeList) {
63          return String.format("%,d exception(s): %s", size(causeList), causeList);
64      }
65  
66      /**
67       * List of causes.
68       */
69      private final List<? extends Throwable> causeList;
70  
71      /**
72       * Constructs a new exception caused by a list of exceptions.
73       *
74       * @param causeList a list of cause exceptions.
75       */
76      public IOExceptionList(final List<? extends Throwable> causeList) {
77          this(toMessage(causeList), causeList);
78      }
79  
80      /**
81       * Constructs a new exception caused by a list of exceptions.
82       *
83       * @param message The detail message, see {@link #getMessage()}.
84       * @param causeList a list of cause exceptions.
85       * @since 2.9.0
86       */
87      public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
88          super(message != null ? message : toMessage(causeList), isEmpty(causeList) ? null : causeList.get(0));
89          this.causeList = causeList == null ? Collections.emptyList() : causeList;
90      }
91  
92      /**
93       * Gets the cause exception at the given index.
94       *
95       * @param <T> type of exception to return.
96       * @param index index in the cause list.
97       * @return The list of causes.
98       */
99      public <T extends Throwable> T getCause(final int index) {
100         return (T) causeList.get(index);
101     }
102 
103     /**
104      * Gets the cause exception at the given index.
105      *
106      * @param <T> type of exception to return.
107      * @param index index in the cause list.
108      * @param clazz type of exception to return.
109      * @return The list of causes.
110      */
111     public <T extends Throwable> T getCause(final int index, final Class<T> clazz) {
112         return clazz.cast(getCause(index));
113     }
114 
115     /**
116      * Gets the cause list.
117      *
118      * @param <T> type of exception to return.
119      * @return The list of causes.
120      */
121     public <T extends Throwable> List<T> getCauseList() {
122         return (List<T>) new ArrayList<>(causeList);
123     }
124 
125     /**
126      * Works around Throwable and Generics, may fail at runtime depending on the argument value.
127      *
128      * @param <T> type of exception to return.
129      * @param clazz the target type
130      * @return The list of causes.
131      */
132     public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) {
133         return (List<T>) new ArrayList<>(causeList);
134     }
135 
136     @Override
137     public Iterator<Throwable> iterator() {
138         return getCauseList().iterator();
139     }
140 
141 }