]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/GroupRuntimeException.java
Changing existing log4j logging to use slf4j
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / GroupRuntimeException.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  * GroupRuntimeException is a RuntimeException 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 GroupRuntimeException extends RuntimeException {\r
26 \r
27     private static final long serialVersionUID = 1L;\r
28 \r
29     private RuntimeException errors[];\r
30     \r
31     private GroupRuntimeException(RuntimeException[] errors) {\r
32         super(GroupRuntimeException.getAsText(errors));\r
33         this.errors = errors;\r
34     }\r
35 \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
40             return errors[0];\r
41         return new GroupRuntimeException(errors);\r
42     }\r
43 \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
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 RuntimeException[] getErrors() {\r
61         return errors;\r
62     }\r
63 \r
64 }\r