View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.core.util;
17  
18  import java.io.PrintStream;
19  import java.io.PrintWriter;
20  
21  /**
22   * Collects multiple illegal state exceptions.
23   */
24  public class MultiCauseIllegalStateException extends IllegalStateException
25  {
26    // ********************************* Fields *********************************
27  
28    // --- constants ------------------------------------------------------------
29  
30    /**
31     * The class version identifier.
32     */
33    private static final long serialVersionUID = 1L;
34  
35    // --- members --------------------------------------------------------------
36  
37    /**
38     * The root causes of this exception.
39     *
40     * @serial
41     */
42    private final Throwable[] rootCauses;
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    /**
49     * Default constructor.
50     *
51     * @param message the detailed message.
52     * @param rootCauses the root causes, may be <code>null</code>.
53     */
54    public MultiCauseIllegalStateException(final String message,
55        final Throwable... rootCauses)
56    {
57      super(message);
58      this.rootCauses = rootCauses;
59    }
60  
61    // ****************************** Inner Classes *****************************
62  
63    // ********************************* Methods ********************************
64  
65    // --- init -----------------------------------------------------------------
66  
67    // --- get&set --------------------------------------------------------------
68  
69    // --- business -------------------------------------------------------------
70  
71    @Override
72    public void printStackTrace(final PrintStream s)
73    {
74      synchronized (s)
75      {
76        super.printStackTrace(s);
77  
78        if (rootCauses != null)
79        {
80          final int size = rootCauses.length;
81          if (size > 0)
82          {
83            s.println("\tThis has " + size + " causes:");
84            for (final Throwable rootCause : rootCauses)
85            {
86              rootCause.printStackTrace(s);
87            }
88          }
89        }
90      }
91    }
92  
93    @Override
94    public void printStackTrace(final PrintWriter s)
95    {
96      synchronized (s)
97      {
98        super.printStackTrace(s);
99  
100       if (rootCauses != null)
101       {
102         final int size = rootCauses.length;
103         if (size > 0)
104         {
105           s.println("\tThis has " + size + " causes:");
106           for (final Throwable rootCause : rootCauses)
107           {
108             rootCause.printStackTrace(s);
109           }
110         }
111       }
112     }
113   }
114 
115   // --- object basics --------------------------------------------------------
116 
117 }