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 * GroupException is a Exception container for a set of exceptions.
20 * Use {@link #group(Collection)} or {@link #group(Exception...)} to construct
23 * @author Toni Kalajainen
25 public class GroupException extends Exception {
27 private static final long serialVersionUID = 1L;
29 private final Throwable errors[];
31 private GroupException(Throwable[] errors) {
32 super(GroupException.getAsText(errors));
36 public static Exception group(Exception... errors) {
37 if (errors.length == 0)
38 throw new IllegalArgumentException("zero exceptions for GroupException");
39 if (errors.length == 1)
41 return new GroupException(errors);
44 public static Throwable group(Collection<? extends Throwable> collection) {
45 if (collection.size() == 0)
46 throw new IllegalArgumentException("zero exceptions for GroupException");
47 if (collection.size() == 1)
48 return collection.iterator().next();
49 return new GroupException(collection.toArray(new Exception[collection.size()]));
52 private static String getAsText(Throwable errors[]) {
54 for (Throwable error : errors) {
55 result += error.getClass().getName()+": "+error.getMessage() + "\n";
60 public Throwable[] getErrors() {