2
* Copyright (C) 2009 The Guava Authors
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.google.common.testing;
19
import com.google.common.annotations.GwtCompatible;
20
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Collection;
23
import java.util.Collections;
26
* An {@link ClusterException} is a data structure that allows for some code to "throw multiple
27
* exceptions", or something close to it. The prototypical code that calls for this class is
31
* void runManyThings({@literal List<ThingToRun>} thingsToRun) {
32
* for (ThingToRun thingToRun : thingsToRun) {
33
* thingToRun.run(); // say this may throw an exception, but you want to
34
* // always run all thingsToRun
39
* <p>This is what the code would become:
42
* void runManyThings({@literal List<ThingToRun>} thingsToRun) {
43
* {@literal List<Exception>} exceptions = Lists.newArrayList();
44
* for (ThingToRun thingToRun : thingsToRun) {
47
* } catch (Exception e) {
51
* if (exceptions.size() > 0) {
52
* throw ClusterException.create(exceptions);
57
* <p>See semantic details at {@link #create(Collection)}.
59
* @author Luiz-Otavio Zorzella
62
@ElementTypesAreNonnullByDefault
63
final class ClusterException extends RuntimeException {
65
final Collection<? extends Throwable> exceptions;
67
private ClusterException(Collection<? extends Throwable> exceptions) {
69
exceptions.size() + " exceptions were thrown. The first exception is listed as a cause.",
70
exceptions.iterator().next());
71
ArrayList<? extends Throwable> temp = new ArrayList<>(exceptions);
72
this.exceptions = Collections.unmodifiableCollection(temp);
75
/** See {@link #create(Collection)}. */
76
static RuntimeException create(Throwable... exceptions) {
77
ArrayList<Throwable> temp = new ArrayList<>(Arrays.asList(exceptions));
82
* Given a collection of exceptions, returns a {@link RuntimeException}, with the following rules:
85
* <li>If {@code exceptions} has a single exception and that exception is a {@link
86
* RuntimeException}, return it
87
* <li>If {@code exceptions} has a single exceptions and that exceptions is <em>not</em> a
88
* {@link RuntimeException}, return a simple {@code RuntimeException} that wraps it
89
* <li>Otherwise, return an instance of {@link ClusterException} that wraps the first exception
90
* in the {@code exceptions} collection.
93
* <p>Though this method takes any {@link Collection}, it often makes most sense to pass a {@link
94
* java.util.List} or some other collection that preserves the order in which the exceptions got
97
* @throws NullPointerException if {@code exceptions} is null
98
* @throws IllegalArgumentException if {@code exceptions} is empty
100
static RuntimeException create(Collection<? extends Throwable> exceptions) {
101
if (exceptions.size() == 0) {
102
throw new IllegalArgumentException("Can't create an ExceptionCollection with no exceptions");
104
if (exceptions.size() == 1) {
105
Throwable temp = exceptions.iterator().next();
106
if (temp instanceof RuntimeException) {
107
return (RuntimeException) temp;
109
return new RuntimeException(temp);
112
return new ClusterException(exceptions);