IOExceptionList.java

  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. package org.apache.commons.io;

  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Objects;

  24. /**
  25.  * An 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 implements Iterable<Throwable> {

  34.     private static final long serialVersionUID = 1L;

  35.     /**
  36.      * Throws this exception if the list is not null or empty.
  37.      *
  38.      * @param causeList The list to test.
  39.      * @param message The detail message, see {@link #getMessage()}.
  40.      * @throws IOExceptionList if the list is not null or empty.
  41.      * @since 2.12.0
  42.      */
  43.     public static void checkEmpty(final List<? extends Throwable> causeList, final Object message) throws IOExceptionList {
  44.         if (!isEmpty(causeList)) {
  45.             throw new IOExceptionList(Objects.toString(message, null), causeList);
  46.         }
  47.     }

  48.     private static boolean isEmpty(final List<? extends Throwable> causeList) {
  49.         return size(causeList) == 0;
  50.     }

  51.     private static int size(final List<? extends Throwable> causeList) {
  52.         return causeList != null ? causeList.size() : 0;
  53.     }

  54.     private static String toMessage(final List<? extends Throwable> causeList) {
  55.         return String.format("%,d exception(s): %s", size(causeList), causeList);
  56.     }

  57.     /**
  58.      * List of causes.
  59.      */
  60.     private final List<? extends Throwable> causeList;

  61.     /**
  62.      * Constructs a new exception caused by a list of exceptions.
  63.      *
  64.      * @param causeList a list of cause exceptions.
  65.      */
  66.     public IOExceptionList(final List<? extends Throwable> causeList) {
  67.         this(toMessage(causeList), causeList);
  68.     }

  69.     /**
  70.      * Constructs a new exception caused by a list of exceptions.
  71.      *
  72.      * @param message The detail message, see {@link #getMessage()}.
  73.      * @param causeList a list of cause exceptions.
  74.      * @since 2.9.0
  75.      */
  76.     public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
  77.         super(message != null ? message : toMessage(causeList), isEmpty(causeList) ? null : causeList.get(0));
  78.         this.causeList = causeList == null ? Collections.emptyList() : causeList;
  79.     }

  80.     /**
  81.      * Gets the cause exception at the given index.
  82.      *
  83.      * @param <T> type of exception to return.
  84.      * @param index index in the cause list.
  85.      * @return The list of causes.
  86.      */
  87.     public <T extends Throwable> T getCause(final int index) {
  88.         return (T) causeList.get(index);
  89.     }

  90.     /**
  91.      * Gets the cause exception at the given index.
  92.      *
  93.      * @param <T> type of exception to return.
  94.      * @param index index in the cause list.
  95.      * @param clazz type of exception to return.
  96.      * @return The list of causes.
  97.      */
  98.     public <T extends Throwable> T getCause(final int index, final Class<T> clazz) {
  99.         return clazz.cast(getCause(index));
  100.     }

  101.     /**
  102.      * Gets the cause list.
  103.      *
  104.      * @param <T> type of exception to return.
  105.      * @return The list of causes.
  106.      */
  107.     public <T extends Throwable> List<T> getCauseList() {
  108.         return (List<T>) new ArrayList<>(causeList);
  109.     }

  110.     /**
  111.      * Works around Throwable and Generics, may fail at runtime depending on the argument value.
  112.      *
  113.      * @param <T> type of exception to return.
  114.      * @param clazz the target type
  115.      * @return The list of causes.
  116.      */
  117.     public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) {
  118.         return (List<T>) new ArrayList<>(causeList);
  119.     }

  120.     @Override
  121.     public Iterator<Throwable> iterator() {
  122.         return getCauseList().iterator();
  123.     }

  124. }