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