DefaultSourceFileExceptionList.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  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  * Type save {@link org.qedeq.kernel.se.common.SourceFileException} list.
025  * TODO m31 20080109: shouldn't this list have some informations about the source being parsed?
026  *
027  @author  Michael Meyling
028  */
029 public class DefaultSourceFileExceptionList extends SourceFileExceptionList {
030 
031     /** List with parse exceptions. */
032     private final List exceptions = new ArrayList();
033 
034     /**
035      * Constructor.
036      */
037     public DefaultSourceFileExceptionList() {
038     }
039 
040     /**
041      * Constructor.
042      *
043      @param   e   Wrap me.
044      */
045     public DefaultSourceFileExceptionList(final SourceFileException e) {
046         add(e);
047     }
048 
049     /**
050      * Constructor.
051      *
052      @param   e   Wrap me.
053      */
054     public DefaultSourceFileExceptionList(final SourceFileExceptionList e) {
055         if (e != null) {
056             for (int i = 0; i < e.size(); i++) {
057                 add(e.get(i));
058             }
059         }
060     }
061 
062     /**
063      * Clear list.
064      */
065     public void clear() {
066         exceptions.clear();
067     }
068 
069     /**
070      * Add exception.
071      *
072      @param   e   Exception to add.
073      */
074     public void add(final SourceFileException e) {
075         if (size() == 0) {
076             initCause(e);
077         }
078         if (!exceptions.contains(e)) {
079             exceptions.add(e);
080         }
081     }
082 
083     /**
084      * Add exceptions of given list if they are not already included.
085      *
086      @param   e   Add exceptions of this list.
087      */
088     public void add(final SourceFileExceptionList e) {
089         if (e == null) {
090             return;
091         }
092         for (int i = 0; i < e.size(); i++) {
093             if (size() == 0) {
094                 initCause(e.get(i));
095             }
096             if (!exceptions.contains(e.get(i))) {
097                 exceptions.add(e.get(i));
098             }
099         }
100     }
101 
102     /**
103      * Get number of collected exceptions.
104      *
105      @return  Number of collected exceptions.
106      */
107     public final int size() {
108         return exceptions.size();
109     }
110 
111     /**
112      * Get <code>i</code>-th exception.
113      *
114      @param   i   Starts with 0 and must be smaller than {@link #size()}.
115      @return  Wanted exception.
116      */
117     public final SourceFileException get(final int i) {
118         return (SourceFileExceptionexceptions.get(i);
119     }
120 
121     /**
122      * Get all exceptions.
123      *
124      @return  All exceptions.
125      */
126     public final SourceFileException[] toArray() {
127         return (SourceFileException[]) exceptions.toArray(
128             new SourceFileException[exceptions.size()]);
129     }
130 
131     public int hashCode() {
132         int result = 71;
133         for (int i = 0; i < size(); i++) {
134             result = result ^ (get(i!= null ? get(i).hashCode() 0);
135         }
136         return result;
137     }
138 
139     public boolean equals(final Object object) {
140         if (!(object instanceof SourceFileExceptionList)) {
141             return false;
142         }
143         SourceFileExceptionList sfl = (SourceFileExceptionListobject;
144         if (size() != sfl.size()) {
145             return false;
146         }
147         for (int i = 0; i < size(); i++) {
148             if (!EqualsUtility.equals(get(i), sfl.get(i))) {
149                 return false;
150             }
151         }
152         return true;
153     }
154 
155     public String getMessage() {
156         final StringBuffer buffer = new StringBuffer();
157         for (int i = 0; i < size(); i++) {
158             if (i != 0) {
159                 buffer.append("\n");
160             }
161             final SourceFileException e = get(i);
162             buffer.append(i).append(": ");
163             buffer.append(e.toString());
164         }
165         return buffer.toString();
166     }
167 
168     public String toString() {
169         final StringBuffer buffer = new StringBuffer();
170         buffer.append(getClass().getName());
171         buffer.append("\n");
172         buffer.append(getMessage());
173         return buffer.toString();
174     }
175 
176 }