    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        super(String.format("%,d exceptions: %s", causeList == null ? 0 : causeList.size(), causeList),
    045                causeList == null ? null : causeList.get(0));
    046        this.causeList = causeList == null ? Collections.emptyList() : causeList;
    047    }
    048
    049    /**
    050     * Gets the cause list.
    051     *
    052     * @param <T> type of exception to return.
    053     * @return The list of causes.
    054     */
    055    public <T extends Throwable> List<T> getCauseList() {
    056        return (List<T>) causeList;
    057    }
    058
    059    /**
    060     * Gets the cause list.
    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 list.
    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 (T) causeList.get(index);
    080    }
    081
    082    /**
    083     * Works around Throwable and Generics, may fail at runtime depending on the argument value.
    084     *
    085     * @param <T> type of exception to return.
    086     * @param clazz the target type
    087     * @return The list of causes.
    088     */
    089    public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) {
    090        return (List<T>) causeList;
    091    }
    092
    093}