1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of Java-Common.
5 *
6 * Java-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Java-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.util;
21
22 import java.io.PrintStream;
23
24 import java.util.ArrayList;
25
26
27 /**
28 * <p>
29 * <code>CombinedException</code> holds together association of exception,
30 * which was for some reason thrown together. For example when a operation is
31 * made on set of object in same hirarchical level and some of them throw
32 * exception. These exceptions should be collected and associated and
33 * reethrown as single <code>CombinedException</code>.
34 * </p>
35 *
36 * <p>
37 * <b>NOTE!</b> instances of this class are not synchronized. In case more
38 * threads is addind exceptions, htan programmer must provide own
39 * synchronization.
40 * </p>
41 *
42 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
43 * @version $id$
44 *
45 * @see com.cosylab.util.CommonException
46 */
47 public class CombinedException extends CommonException
48 {
49 /*
50 * For storing collected exceptions.
51 */
52 protected ArrayList collected = new ArrayList();
53
54 /**
55 * Create a new instance of <code>CombinedException</code>. This
56 * constructor should normally not be used since it carries no additional
57 * exception data.
58 */
59 protected CombinedException()
60 {
61 super();
62 }
63
64 /**
65 * Create an instance of <code>CombinedException</code> with a with a
66 * specified message string. The construction is delegated to
67 * <code>super</code>.
68 *
69 * @param instance identifier of the instance throwing this exception
70 * @param s message to be printed together with the exception type when the
71 * <code>toString()</code> is called.
72 */
73 public CombinedException(Object instance, String s)
74 {
75 super(instance, s);
76 }
77
78 /**
79 * Create an instance of <code>CombinedException</code> by specifying both
80 * the string message and a <code>Throwable</code> object that represents
81 * the nested exception.
82 *
83 * @param instance identifier of the instance throwing this exception
84 * @param message a string message passed to <code>super</code>
85 * @param t an instance of <code>Throwable</code> that caused this to be
86 * thrown, can be <code>null</code>
87 */
88 public CombinedException(Object instance, String message, Throwable t)
89 {
90 super(instance, message, t);
91 }
92
93 /**
94 * Associates parameter <code>throwable</code> to this exception.
95 *
96 * @param throwble exception to be associated
97 */
98 public void associateException(Throwable throwble)
99 {
100 collected.add(throwble);
101 }
102
103 /**
104 * Returns exceptions associated with this object.
105 *
106 * @return exceptions associated with this object
107 */
108 public Throwable[] getAssociatedExceptions()
109 {
110 Throwable[] t = new Throwable[collected.size()];
111 collected.toArray(t);
112
113 return t;
114 }
115
116 /**
117 * DOCUMENT ME!
118 *
119 * @param s DOCUMENT ME!
120 */
121 public void printStackTrace(PrintStream s)
122 {
123 super.printStackTrace(s);
124 s.println("Associated exceptions (" + collected.size() + " items):");
125
126 Throwable[] t = getAssociatedExceptions();
127
128 for (int i = 0; i < t.length; i++) {
129 s.print("[" + i + "]: ");
130 t[i].printStackTrace(s);
131 }
132 }
133 }
134
135 /* __oOo__ */