KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > error > ErrorList


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.error;
8
9
10 import java.io.Serializable JavaDoc;
11 import java.util.ArrayList JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17
18 /**
19  * This class is a storage device for Errors. These Errors can
20  * be any type of errors that are castable to BasicError.
21  * Directly, this class has methods for supporting PropertyErrors
22  * as well. These include retrieval PropertyErrors by property
23  * name.
24  *
25  * @author Brian Pontarelli
26  * @since 2.0
27  * @version 2.0
28  */

29 public class ErrorList implements Serializable JavaDoc {
30
31     private List JavaDoc list;
32     private Map JavaDoc mappings;
33
34
35     /**
36      * Constructs an empty error list
37      */

38     public ErrorList() {
39
40         mappings = new HashMap JavaDoc();
41         list = new ArrayList JavaDoc();
42     }
43
44     /**
45      * Constructs a new error list and copies all the errors into the new list.
46      * If any objects in the list are not instances of or sub-classes of
47      * BasicError, they are discarded
48      */

49     public ErrorList(List JavaDoc list) {
50         this();
51
52         Iterator JavaDoc iter = list.iterator();
53         Object JavaDoc error;
54
55         while (iter.hasNext()) {
56             error = iter.next();
57             if (error instanceof BasicError) {
58                 addError((BasicError) error);
59             }
60         }
61     }
62
63     /**
64      * Copy constructor that copies over all the errors from the given ErrorList
65      * as well as all the mappings for PropertyErrors
66      *
67      * @param errorList The ErrorList to copy from
68      */

69     public ErrorList(ErrorList errorList) {
70         this();
71         addErrorList(errorList);
72     }
73
74
75     /**
76      * Adds a new error to the list. If the error is a PropertyError, adds the
77      * error to the list of errors associated with that property.
78      *
79      * @param error The error to add to the list. This can be any type of
80      * error that is a descedent of BasicError
81      */

82     public void addError(BasicError error) {
83         if (error instanceof PropertyError) {
84             addError((PropertyError) error);
85         } else {
86             list.add(error);
87         }
88     }
89
90     /**
91      * Adds a new property error to the list.
92      *
93      * @param error The property error to add to the list.
94      */

95     public void addError(PropertyError error) {
96         list.add(error);
97
98         String JavaDoc property = error.getProperty();
99         List JavaDoc propList = (List JavaDoc) mappings.get(property);
100         if (propList == null) {
101             propList = new ArrayList JavaDoc();
102             mappings.put(property, propList);
103         }
104
105         propList.add(error);
106     }
107
108     /**
109      * Adds the given String as a BasicError to this ErrorList. This is basically
110      * a convience method so that callers do not have to construct BasicErrors
111      * in order to add an error to this list.
112      *
113      * @param error The String which will be added as a BasicError
114      */

115     public void addError(String JavaDoc error) {
116         addError( new BasicError(error) );
117     }
118
119     /**
120      * Adds the given String as a PropertyError with the property given being
121      * the property it is associated with in this ErrorList. This is basically
122      * a convience method so that callers do not have to construct PropertyErrors
123      * in order to add an error to this list.
124      *
125      * @param property The property to associate the new PropertyError with
126      * @param error The String which will be added as a BasicError
127      */

128     public void addError(String JavaDoc property, String JavaDoc error) {
129         addError( new PropertyError(property, error) );
130     }
131
132     /**
133      * Adds all the errors from the error list given to this error list
134      */

135     public void addErrorList(ErrorList errorList) {
136
137         // Copy over all the errors and mappings, one by one for safety
138
if (errorList != null && !errorList.isEmpty()) {
139             Iterator JavaDoc iter = errorList.list.iterator();
140             while (iter.hasNext()) {
141                 addError((BasicError) iter.next());
142             }
143         }
144     }
145
146     /**
147      * Returns the LIVE list, changes made to this list effect the state of
148      * the error list
149      *
150      * @return The List of all the errors. This List is not live. Changes to it
151      * do not effect this class.
152      */

153     public List JavaDoc getAllErrors() {
154         return new ArrayList JavaDoc(list);
155     }
156
157     /**
158      * Returns all the errors in the list the DO NOT have an associated property
159      * or an empty list if all the errors in the list have an associated property
160      *
161      * @return The list of basic errors or an empty list if there are none. This
162      * List is not live
163      */

164     public List JavaDoc getBasicErrors() {
165         List JavaDoc retValue = new ArrayList JavaDoc();
166         Iterator JavaDoc iter = list.iterator();
167         Object JavaDoc next;
168
169         while (iter.hasNext()) {
170             next = iter.next();
171             if (next.getClass() == BasicError.class) {
172                 retValue.add(next);
173             }
174         }
175
176         return retValue;
177     }
178
179     /**
180      * Returns all the errors in the list the have an associated property or an
181      * list if there are no errors in the list have an associated property
182      *
183      * @return The list of property errors or an empty list if there are none.
184      * This List is not live
185      */

186     public List JavaDoc getPropertyErrors() {
187         List JavaDoc list = new ArrayList JavaDoc();
188         if (mappings.size() > 0) {
189             Iterator JavaDoc iter = mappings.keySet().iterator();
190             while (iter.hasNext()) {
191                 list.addAll((List JavaDoc) mappings.get(iter.next()));
192             }
193         }
194
195         return list;
196     }
197
198     /**
199      * Returns all the errors associated with the given property name or an empty
200      * list if there are none.
201      *
202      * @param property The property name to look up the PropertyErrors for
203      * @return The List of all the PropertyError objects for the given property
204      * or an empty list if there are none. This list is NOT live. Adding
205      * errors to it, has no effect on the internal state of this ErrorList
206      */

207     public List JavaDoc getPropertyErrors(String JavaDoc property) {
208         List JavaDoc list = (List JavaDoc) mappings.get(property);
209         if (list == null) {
210             list = new ArrayList JavaDoc();
211         } else {
212             list = new ArrayList JavaDoc(list);
213         }
214
215         return list;
216     }
217
218     /**
219      * Returns the first error that was added for the given property name. If
220      * none were ever added, this returns null.
221      *
222      * @param property The property name to look up the first PropertyError for
223      * @return The first PropertyError that was added for the given property or
224      * null if there isn't one
225      */

226     public PropertyError getFirstPropertyError(String JavaDoc property) {
227         List JavaDoc list = getPropertyErrors(property);
228         PropertyError error = null;
229         if (list.size() != 0) {
230             error = (PropertyError) list.get(0);
231         }
232
233         return error;
234     }
235
236     /**
237      * Returns the error at the given indices in the list
238      *
239      * @param index The indices of the error
240      * @return The error at that indices of null if the indices is out of range
241      * @throws IndexOutOfBoundsException If the index is out of bounds of the
242      * list size
243      */

244     public BasicError getError(int index) {
245         return (BasicError) list.get(index);
246     }
247
248     /**
249      * Returns an iterator for the list of all the errors contained in this list
250      *
251      * @return An Iterator over the list of errors. This Iterator is live and
252      * changes made to the List via the Iterator will effect this class
253      */

254     public Iterator JavaDoc iterator() {
255         return list.iterator();
256     }
257
258     /**
259      * Clears out the entire list of errors
260      */

261     public void removeAllErrors() {
262         list.clear();
263         mappings.clear();
264     }
265
266     /**
267      * Shortcut for the removeAllErrors method
268      */

269     public void clear() {
270         removeAllErrors();
271     }
272
273     /**
274      * Removes the error at the indices given
275      *
276      * @return The element removed
277      * @throws IndexOutOfBoundsException If the index is out of bounds of the
278      * list size
279      */

280     public BasicError removeError(int index) {
281         return (BasicError) list.remove(index);
282     }
283
284     /**
285      * Removes the property errors associated with the given property name
286      *
287      * @param property The name of the property to remove the errors for
288      * @return A List of all the property error objects removed
289      */

290     public List JavaDoc removeErrors(String JavaDoc property) {
291
292         assert (property != null) : "property == null";
293
294         List JavaDoc propList = (List JavaDoc) mappings.remove(property);
295         if (propList != null) {
296             Iterator JavaDoc iter = propList.iterator();
297             while (iter.hasNext()) {
298                 list.remove(iter.next());
299             }
300         }
301
302         return propList;
303     }
304
305     /**
306      * Returns true if the list is empty, false otherwise
307      */

308     public boolean isEmpty() {
309         return (list.size() == 0);
310     }
311
312     /**
313      * Returns true if this list contains basic errors which have not associated
314      * property
315      */

316     public boolean hasBasicErrors() {
317         List JavaDoc basicErrors = new ArrayList JavaDoc(list);
318
319         basicErrors.removeAll(mappings.values());
320         return !basicErrors.isEmpty();
321     }
322
323     /**
324      * Returns true if this list contains property errors
325      */

326     public boolean hasPropertyErrors() {
327         return !mappings.isEmpty();
328     }
329
330     /**
331      * Returns true if this list contains property errors for the given property
332      * name
333      */

334     public boolean hasPropertyErrors(String JavaDoc property) {
335         return (mappings.get(property) != null);
336     }
337 }
Popular Tags