    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.io;
    019
    020import java.io.IOException;
    021import java.util.Collections;
    022import java.util.List;
    023
    024/**
    025 * A IOException based on a list of Throwable causes.
    026 * <p>
    027 * The first exception in the list is used as this exception's cause and is accessible with the usual
    028 * {@link #getCause()} while the complete list is accessible with {@link #getCauseList()}.
    029 * </p>
    030 *
    031 * @since 2.7
    032 */
    033public class IOExceptionList extends IOException {
    034
    035    private static final long serialVersionUID = 1L;
    036    private final List<? extends Throwable> causeList;
    037
    038    /**
    039     * Creates a new exception caused by a list of exceptions.
    040     *
    041     * @param causeList a list of cause exceptions.
    042     */
    043    public IOExceptionList(final List<? extends Throwable> causeList) {
    044        this(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList), causeList);
    045    }
    046
    047    /**
    048     * Creates a new exception caused by a list of exceptions.
    049     *
    050     * @param message The detail message, see {@link #getMessage()}.
    051     * @param causeList a list of cause exceptions.
    052     * @since 2.9.0
    053     */
    054    public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
    055        super(message, causeList == null || causeList.isEmpty() ? null : causeList.get(0));
    056        this.causeList = causeList == null ? Collections.emptyList() : causeList;
    057    }
    058
    059    /**
    060     * Gets the cause exception at the given index.
    061     *
    062     * @param <T> type of exception to return.
    063     * @param index index in the cause list.
    064     * @return The list of causes.
    065     */
    066    public <T extends Throwable> T getCause(final int index) {
    067        return (T) causeList.get(index);
    068    }
    069
    070    /**
    071     * Gets the cause exception at the given index.
    072     *
    073     * @param <T> type of exception to return.
    074     * @param index index in the cause list.
    075     * @param clazz type of exception to return.
    076     * @return The list of causes.
    077     */
    078    public <T extends Throwable> T getCause(final int index, final Class<T> clazz) {
    079        return clazz.cast(causeList.get(index));
    080    }
    081
    082    /**
    083     * Gets the cause list.
    084     *
    085     * @param <T> type of exception to return.
    086     * @return The list of causes.
    087     */
    088    public <T extends Throwable> List<T> getCauseList() {
    089        return (List<T>) causeList;
    090    }
    091
    092    /**
    093     * Works around Throwable and Generics, may fail at runtime depending on the argument value.
    094     *
    095     * @param <T> type of exception to return.
    096     * @param clazz the target type
    097     * @return The list of causes.
    098     */
    099    public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) {
    100        return (List<T>) causeList;
    101    }
    102
    103}