| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package org.apache.commons.io; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Collections; |
| 23 |
import java.util.Iterator; |
| 24 |
import java.util.List; |
| 25 |
import java.util.Objects; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class IOExceptionList extends IOException implements Iterable<Throwable> { |
| 37 |
|
| 38 |
private static final long serialVersionUID = 1L; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static void checkEmpty(final List<? extends Throwable> causeList, final Object message) throws IOExceptionList { |
| 49 |
if (!isEmpty(causeList)) { |
| 50 |
throw new IOExceptionList(Objects.toString(message, null), causeList); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
private static boolean isEmpty(final List<? extends Throwable> causeList) { |
| 55 |
return size(causeList) == 0; |
| 56 |
} |
| 57 |
|
| 58 |
private static int size(final List<? extends Throwable> causeList) { |
| 59 |
return causeList != null ? causeList.size() : 0; |
| 60 |
} |
| 61 |
|
| 62 |
private static String toMessage(final List<? extends Throwable> causeList) { |
| 63 |
return String.format("%,d exception(s): %s", size(causeList), causeList); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private final List<? extends Throwable> causeList; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public IOExceptionList(final List<? extends Throwable> causeList) { |
| 77 |
this(toMessage(causeList), causeList); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public IOExceptionList(final String message, final List<? extends Throwable> causeList) { |
| 88 |
super(message != null ? message : toMessage(causeList), isEmpty(causeList) ? null : causeList.get(0)); |
| 89 |
this.causeList = causeList == null ? Collections.emptyList() : causeList; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public <T extends Throwable> T getCause(final int index) { |
| 100 |
return (T) causeList.get(index); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public <T extends Throwable> T getCause(final int index, final Class<T> clazz) { |
| 112 |
return clazz.cast(getCause(index)); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public <T extends Throwable> List<T> getCauseList() { |
| 122 |
return (List<T>) new ArrayList<>(causeList); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public <T extends Throwable> List<T> getCauseList(final Class<T> clazz) { |
| 133 |
return (List<T>) new ArrayList<>(causeList); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public Iterator<Throwable> iterator() { |
| 138 |
return getCauseList().iterator(); |
| 139 |
} |
| 140 |
|
| 141 |
} |