View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
14   */
15  
16  package org.qedeq.kernel.se.common;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.qedeq.base.utility.EqualsUtility;
22  
23  
24  /**
25   * Type save {@link org.qedeq.kernel.se.common.SourceFileException} list.
26   *
27   * @author  Michael Meyling
28   */
29  public class SourceFileExceptionList extends Exception {
30  
31      /** List with parse exceptions. */
32      private final List exceptions = new ArrayList();
33  
34      /**
35       * Constructor.
36       */
37      public SourceFileExceptionList() {
38      }
39  
40      /**
41       * Constructor.
42       *
43       * @param   e   Wrap me.
44       */
45      public SourceFileExceptionList(final SourceFileException e) {
46          if (e != null) {
47              add(e);
48          }
49      }
50  
51      /**
52       * Constructor.
53       *
54       * @param   e   Wrap me.
55       */
56      public SourceFileExceptionList(final SourceFileExceptionList e) {
57          if (e != null) {
58              for (int i = 0; i < e.size(); i++) {
59                  add(e.get(i));
60              }
61          }
62      }
63  
64      /**
65       * Clear list.
66       */
67      public void clear() {
68          exceptions.clear();
69      }
70  
71      /**
72       * Add exception. If we get <code>null</code> we do nothing.
73       *
74       * @param   e   Exception to add.
75       */
76      public void add(final SourceFileException e) {
77          if (e == null) {
78              return;
79          }
80          if (size() == 0) {
81              initCause(e);
82          }
83          if (!exceptions.contains(e)) {
84              exceptions.add(e);
85          }
86      }
87  
88      /**
89       * Add exceptions of given list if they are not already included. If we get <code>null</code> we do nothing.
90       *
91       * @param   e   Add exceptions of this list.
92       */
93      public void add(final SourceFileExceptionList e) {
94          if (e == null) {
95              return;
96          }
97          for (int i = 0; i < e.size(); i++) {
98              if (size() == 0) {
99                  initCause(e.get(i));
100             }
101             if (!exceptions.contains(e.get(i))) {
102                 exceptions.add(e.get(i));
103             }
104         }
105     }
106 
107     /**
108      * Get number of collected exceptions.
109      *
110      * @return  Number of collected exceptions.
111      */
112     public final int size() {
113         return exceptions.size();
114     }
115 
116     /**
117      * Get <code>i</code>-th exception.
118      *
119      * @param   i   Starts with 0 and must be smaller than {@link #size()}.
120      * @return  Wanted exception.
121      */
122     public final SourceFileException get(final int i) {
123         return (SourceFileException) exceptions.get(i);
124     }
125 
126     /**
127      * Get all exceptions.
128      *
129      * @return  All exceptions.
130      */
131     public final SourceFileException[] toArray() {
132         return (SourceFileException[]) exceptions.toArray(
133             new SourceFileException[exceptions.size()]);
134     }
135 
136     public int hashCode() {
137         int result = 71;
138         for (int i = 0; i < size(); i++) {
139             result = result ^ (get(i) != null ? get(i).hashCode() : 0);
140         }
141         return result;
142     }
143 
144     public boolean equals(final Object object) {
145         if (!(object instanceof SourceFileExceptionList)) {
146             return false;
147         }
148         SourceFileExceptionList sfl = (SourceFileExceptionList) object;
149         if (size() != sfl.size()) {
150             return false;
151         }
152         for (int i = 0; i < size(); i++) {
153             if (!EqualsUtility.equals(get(i), sfl.get(i))) {
154                 return false;
155             }
156         }
157         return true;
158     }
159 
160     public String getMessage() {
161         final StringBuffer buffer = new StringBuffer();
162         for (int i = 0; i < size(); i++) {
163             if (i != 0) {
164                 buffer.append("\n");
165             }
166             final SourceFileException e = get(i);
167             buffer.append(i).append(": ");
168             buffer.append(e.toString());
169         }
170         return buffer.toString();
171     }
172 
173     public String toString() {
174         final StringBuffer buffer = new StringBuffer();
175         buffer.append(getClass().getName());
176         buffer.append("\n");
177         buffer.append(getMessage());
178         return buffer.toString();
179     }
180 
181 }