SourceFileExceptionList.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.se.common;
017 
018 import java.util.ArrayList;
019 import java.util.List;
020 
021 import org.qedeq.base.utility.EqualsUtility;
022 
023 
024 /**
025  * Type save {@link org.qedeq.kernel.se.common.SourceFileException} list.
026  *
027  @author  Michael Meyling
028  */
029 public class SourceFileExceptionList extends Exception {
030 
031     /** List with parse exceptions. */
032     private final List exceptions = new ArrayList();
033 
034     /**
035      * Constructor.
036      */
037     public SourceFileExceptionList() {
038     }
039 
040     /**
041      * Constructor.
042      *
043      @param   e   Wrap me.
044      */
045     public SourceFileExceptionList(final SourceFileException e) {
046         if (e != null) {
047             add(e);
048         }
049     }
050 
051     /**
052      * Constructor.
053      *
054      @param   e   Wrap me.
055      */
056     public SourceFileExceptionList(final SourceFileExceptionList e) {
057         if (e != null) {
058             for (int i = 0; i < e.size(); i++) {
059                 add(e.get(i));
060             }
061         }
062     }
063 
064     /**
065      * Clear list.
066      */
067     public void clear() {
068         exceptions.clear();
069     }
070 
071     /**
072      * Add exception. If we get <code>null</code> we do nothing.
073      *
074      @param   e   Exception to add.
075      */
076     public void add(final SourceFileException e) {
077         if (e == null) {
078             return;
079         }
080         if (size() == 0) {
081             initCause(e);
082         }
083         if (!exceptions.contains(e)) {
084             exceptions.add(e);
085         }
086     }
087 
088     /**
089      * Add exceptions of given list if they are not already included. If we get <code>null</code> we do nothing.
090      *
091      @param   e   Add exceptions of this list.
092      */
093     public void add(final SourceFileExceptionList e) {
094         if (e == null) {
095             return;
096         }
097         for (int i = 0; i < e.size(); i++) {
098             if (size() == 0) {
099                 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 (SourceFileExceptionexceptions.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 = (SourceFileExceptionListobject;
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 }