]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.utils/src/org/simantics/utils/GroupException.java b/bundles/org.simantics.utils/src/org/simantics/utils/GroupException.java
new file mode 100644 (file)
index 0000000..af44a13
--- /dev/null
@@ -0,0 +1,64 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils;\r
+\r
+import java.util.Collection;\r
+\r
+/**\r
+ * GroupException is a Exception container for a set of exceptions.\r
+ * \r
+ * <p>\r
+ * Use {@link #group(Collection)} or {@link #group(Exception...)} to construct\r
+ * it.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class GroupException extends Exception {\r
+\r
+    private static final long serialVersionUID = 1L;\r
+\r
+    private final Throwable errors[];\r
+\r
+    private GroupException(Throwable[] errors) {\r
+        super(GroupException.getAsText(errors));\r
+        this.errors = errors;\r
+    }\r
+\r
+    public static Exception group(Exception... errors) {\r
+        if (errors.length == 0)\r
+            throw new IllegalArgumentException("zero exceptions for GroupException");\r
+        if (errors.length == 1)\r
+            return errors[0];\r
+        return new GroupException(errors);\r
+    }\r
+\r
+    public static Throwable group(Collection<? extends Throwable> collection) {\r
+        if (collection.size() == 0)\r
+            throw new IllegalArgumentException("zero exceptions for GroupException");\r
+        if (collection.size() == 1)\r
+            return collection.iterator().next();\r
+        return new GroupException(collection.toArray(new Exception[collection.size()]));\r
+    }\r
+\r
+    private static String getAsText(Throwable errors[]) {\r
+        String result = "";\r
+        for (Throwable error : errors) {\r
+            result += error.getClass().getName()+": "+error.getMessage() + "\n";\r
+        }\r
+        return result;\r
+    }\r
+\r
+    public Throwable[] getErrors() {\r
+        return errors;\r
+    }\r
+\r
+}\r