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.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025import java.util.Objects;
026
027/**
028 * An IOException based on a list of Throwable causes.
029 * <p>
030 * The first exception in the list is used as this exception's cause and is accessible with the usual
031 * {@link #getCause()} while the complete list is accessible with {@link #getCauseList()}.
032 * </p>
033 *
034 * @since 2.7
035 */
036public class IOExceptionList extends IOException implements Iterable<Throwable> {
037
038    private static final long serialVersionUID = 1L;
039
040    /**
041     * Throws this exception if the list is not null or empty.
042     *
043     * @param causeList The list to test.
044     * @param message The detail message, see {@link #getMessage()}.
045     * @throws IOExceptionList if the list is not null or empty.
046     * @since 2.12.0
047     */
048    public static void checkEmpty(final List<? extends Throwable> causeList, final Object message) throws IOExceptionList {
049        if (!isEmpty(causeList)) {
050            throw new IOExceptionList(Objects.toString(message, null), causeList);
051        }
052    }
053
054    private static boolean isEmpty(final List<? extends Throwable> causeList) {
055        return size(causeList) == 0;
056    }
057
058    private static int size(final List<? extends Throwable> causeList) {
059        return causeList != null ? causeList.size() : 0;
060    }
061
062    private static String toMessage(final List<? extends Throwable> causeList) {
063        return String.format("%,d exception(s): %s", size(causeList), causeList);
064    }
065
066    private final List<? extends Throwable> causeList;
067
068    /**
069     * Constructs a new exception caused by a list of exceptions.
070     *
071     * @param causeList a list of cause exceptions.
072     */
073    public IOExceptionList(final List<? extends Throwable> causeList) {
074        this(toMessage(causeList), causeList);
075    }
076
077    /**
078     * Constructs a new exception caused by a list of exceptions.
079     *
080     * @param message The detail message, see {@link #getMessage()}.
081     * @param causeList a list of cause exceptions.
082     * @since 2.9.0
083     */
084    public IOExceptionList(final String message, final List<? extends Throwable> causeList) {
085        super(message != null ? message : toMessage(causeList), isEmpty(causeList) ? null : causeList.get(0));
086        this.causeList = causeList == null ? Collections.emptyList() : causeList;
087    }
088
089    /**
090     * Gets the cause exception at the given index.
091     *
092     * @param <T> type of exception to return.
093     * @param index index in the cause list.
094     * @return The list of causes.
095     */
096    public <T extends Throwable> T getCause(final int index) {
097        return (T) causeList.get(index);
098    }
099
100    /**
101     * Gets the cause exception at the given index.
102     *
103     * @param <T> type of exception to return.
104     * @param index index in the cause list.
105     * @param clazz type of exception to return.
106     * @return The list of causes.
107     */
108    public <T extends Throwable> T getCause(final int index, final Class<T> clazz) {
109        return clazz.cast(getCause(index));
110    }
111
112    /**
113     * Gets the cause list.
114     *
115     * @param <T> type of exception to return.
116     * @return The list of causes.
117     */
118    public <T extends Throwable> List<T> getCauseList() {
119        return (List<T>) new ArrayList<>(causeList);
120    }
121
122    /**
123     * Works around Throwable and Generics, may fail at runtime depending on the argument value.
124     *
125     * @param <T> type of exception to return.
126     * @param clazz the target type
127     * @return The list of causes.
128     */
129    public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) {
130        return (List<T>) new ArrayList<>(causeList);
131    }
132
133    @Override
134    public Iterator<Throwable> iterator() {
135        return getCauseList().iterator();
136    }
137
138}