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.Collections;
22  import java.util.List;
23  
24  /**
25   * A IOException based on a list of Throwable causes.
26   * <p>
27   * The first exception in the list is used as this exception's cause and is accessible with the usual
28   * {@link #getCause()} while the complete list is accessible with {@link #getCauseList()}.
29   * </p>
30   *
31   * @since 2.7
32   */
33  public class IOExceptionList extends IOException {
34  
35      private static final long serialVersionUID = 1L;
36      private final List<? extends Throwable> causeList;
37  
38      /**
39       * Creates a new exception caused by a list of exceptions.
40       *
41       * @param causeList a list of cause exceptions.
42       */
43      public IOExceptionList(final List<? extends Throwable> causeList) {
44          this(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList), causeList);
45      }
46  
47      /**
48       * Creates a new exception caused by a list of exceptions.
49       *
50       * @param message The detail message, see {@link #getMessage()}.
51       * @param causeList a list of cause exceptions.
52       * @since 2.9.0
53       */
54      public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
55          super(message, causeList == null || causeList.isEmpty() ? null : causeList.get(0));
56          this.causeList = causeList == null ? Collections.emptyList() : causeList;
57      }
58  
59      /**
60       * Gets the cause exception at the given index.
61       *
62       * @param <T> type of exception to return.
63       * @param index index in the cause list.
64       * @return The list of causes.
65       */
66      public <T extends Throwable> T getCause(final int index) {
67          return (T) causeList.get(index);
68      }
69  
70      /**
71       * Gets the cause exception at the given index.
72       *
73       * @param <T> type of exception to return.
74       * @param index index in the cause list.
75       * @param clazz type of exception to return.
76       * @return The list of causes.
77       */
78      public <T extends Throwable> T getCause(final int index, final Class<T> clazz) {
79          return clazz.cast(causeList.get(index));
80      }
81  
82      /**
83       * Gets the cause list.
84       *
85       * @param <T> type of exception to return.
86       * @return The list of causes.
87       */
88      public <T extends Throwable> List<T> getCauseList() {
89          return (List<T>) causeList;
90      }
91  
92      /**
93       * Works around Throwable and Generics, may fail at runtime depending on the argument value.
94       *
95       * @param <T> type of exception to return.
96       * @param clazz the target type
97       * @return The list of causes.
98       */
99      public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) {
100         return (List<T>) causeList;
101     }
102 
103 }