]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/GroupException.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / GroupException.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils;\r
13 \r
14 import java.util.Collection;\r
15 \r
16 /**\r
17  * GroupException is a Exception container for a set of exceptions.\r
18  * \r
19  * <p>\r
20  * Use {@link #group(Collection)} or {@link #group(Exception...)} to construct\r
21  * it.\r
22  * \r
23  * @author Toni Kalajainen\r
24  */\r
25 public class GroupException extends Exception {\r
26 \r
27     private static final long serialVersionUID = 1L;\r
28 \r
29     private final Throwable errors[];\r
30 \r
31     private GroupException(Throwable[] errors) {\r
32         super(GroupException.getAsText(errors));\r
33         this.errors = errors;\r
34     }\r
35 \r
36     public static Exception group(Exception... errors) {\r
37         if (errors.length == 0)\r
38             throw new IllegalArgumentException("zero exceptions for GroupException");\r
39         if (errors.length == 1)\r
40             return errors[0];\r
41         return new GroupException(errors);\r
42     }\r
43 \r
44     public static Throwable group(Collection<? extends Throwable> collection) {\r
45         if (collection.size() == 0)\r
46             throw new IllegalArgumentException("zero exceptions for GroupException");\r
47         if (collection.size() == 1)\r
48             return collection.iterator().next();\r
49         return new GroupException(collection.toArray(new Exception[collection.size()]));\r
50     }\r
51 \r
52     private static String getAsText(Throwable errors[]) {\r
53         String result = "";\r
54         for (Throwable error : errors) {\r
55             result += error.getClass().getName()+": "+error.getMessage() + "\n";\r
56         }\r
57         return result;\r
58     }\r
59 \r
60     public Throwable[] getErrors() {\r
61         return errors;\r
62     }\r
63 \r
64 }\r