1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils;
\r
14 import java.util.Collection;
\r
17 * GroupRuntimeException is a RuntimeException container for a set of exceptions.
\r
20 * Use {@link #group(Collection)} or {@link #group(Exception[])} to construct
\r
23 * @author Toni Kalajainen
\r
25 public class GroupRuntimeException extends RuntimeException {
\r
27 private static final long serialVersionUID = 1L;
\r
29 private RuntimeException errors[];
\r
31 private GroupRuntimeException(RuntimeException[] errors) {
\r
32 super(GroupRuntimeException.getAsText(errors));
\r
33 this.errors = errors;
\r
36 public static RuntimeException group(RuntimeException... errors) {
\r
37 if (errors.length == 0)
\r
38 throw new IllegalArgumentException("zero exceptions for GroupRuntimeException");
\r
39 if (errors.length == 1)
\r
41 return new GroupRuntimeException(errors);
\r
44 public static RuntimeException group(Collection<RuntimeException> collection) {
\r
45 if (collection.size() == 0)
\r
46 throw new IllegalArgumentException("zero exceptions for GroupRuntimeException");
\r
47 if (collection.size() == 1)
\r
48 return collection.iterator().next();
\r
49 return new GroupRuntimeException(collection.toArray(new RuntimeException[collection.size()]));
\r
52 private static String getAsText(Throwable errors[]) {
\r
54 for (Throwable error : errors) {
\r
55 result += error.getClass().getName()+": "+error.getMessage() + "\n";
\r
60 public RuntimeException[] getErrors() {
\r