]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/GroupException.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / GroupException.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils;
13
14 import java.util.Collection;
15
16 /**
17  * GroupException is a Exception container for a set of exceptions.
18  * 
19  * <p>
20  * Use {@link #group(Collection)} or {@link #group(Exception...)} to construct
21  * it.
22  * 
23  * @author Toni Kalajainen
24  */
25 public class GroupException extends Exception {
26
27     private static final long serialVersionUID = 1L;
28
29     private final Throwable errors[];
30
31     private GroupException(Throwable[] errors) {
32         super(GroupException.getAsText(errors));
33         this.errors = errors;
34     }
35
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)
40             return errors[0];
41         return new GroupException(errors);
42     }
43
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()]));
50     }
51
52     private static String getAsText(Throwable errors[]) {
53         String result = "";
54         for (Throwable error : errors) {
55             result += error.getClass().getName()+": "+error.getMessage() + "\n";
56         }
57         return result;
58     }
59
60     public Throwable[] getErrors() {
61         return errors;
62     }
63
64 }