1 7 package com.inversoft.error; 8 9 10 import java.io.Serializable ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 18 29 public class ErrorList implements Serializable { 30 31 private List list; 32 private Map mappings; 33 34 35 38 public ErrorList() { 39 40 mappings = new HashMap (); 41 list = new ArrayList (); 42 } 43 44 49 public ErrorList(List list) { 50 this(); 51 52 Iterator iter = list.iterator(); 53 Object error; 54 55 while (iter.hasNext()) { 56 error = iter.next(); 57 if (error instanceof BasicError) { 58 addError((BasicError) error); 59 } 60 } 61 } 62 63 69 public ErrorList(ErrorList errorList) { 70 this(); 71 addErrorList(errorList); 72 } 73 74 75 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 95 public void addError(PropertyError error) { 96 list.add(error); 97 98 String property = error.getProperty(); 99 List propList = (List ) mappings.get(property); 100 if (propList == null) { 101 propList = new ArrayList (); 102 mappings.put(property, propList); 103 } 104 105 propList.add(error); 106 } 107 108 115 public void addError(String error) { 116 addError( new BasicError(error) ); 117 } 118 119 128 public void addError(String property, String error) { 129 addError( new PropertyError(property, error) ); 130 } 131 132 135 public void addErrorList(ErrorList errorList) { 136 137 if (errorList != null && !errorList.isEmpty()) { 139 Iterator iter = errorList.list.iterator(); 140 while (iter.hasNext()) { 141 addError((BasicError) iter.next()); 142 } 143 } 144 } 145 146 153 public List getAllErrors() { 154 return new ArrayList (list); 155 } 156 157 164 public List getBasicErrors() { 165 List retValue = new ArrayList (); 166 Iterator iter = list.iterator(); 167 Object 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 186 public List getPropertyErrors() { 187 List list = new ArrayList (); 188 if (mappings.size() > 0) { 189 Iterator iter = mappings.keySet().iterator(); 190 while (iter.hasNext()) { 191 list.addAll((List ) mappings.get(iter.next())); 192 } 193 } 194 195 return list; 196 } 197 198 207 public List getPropertyErrors(String property) { 208 List list = (List ) mappings.get(property); 209 if (list == null) { 210 list = new ArrayList (); 211 } else { 212 list = new ArrayList (list); 213 } 214 215 return list; 216 } 217 218 226 public PropertyError getFirstPropertyError(String property) { 227 List 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 244 public BasicError getError(int index) { 245 return (BasicError) list.get(index); 246 } 247 248 254 public Iterator iterator() { 255 return list.iterator(); 256 } 257 258 261 public void removeAllErrors() { 262 list.clear(); 263 mappings.clear(); 264 } 265 266 269 public void clear() { 270 removeAllErrors(); 271 } 272 273 280 public BasicError removeError(int index) { 281 return (BasicError) list.remove(index); 282 } 283 284 290 public List removeErrors(String property) { 291 292 assert (property != null) : "property == null"; 293 294 List propList = (List ) mappings.remove(property); 295 if (propList != null) { 296 Iterator iter = propList.iterator(); 297 while (iter.hasNext()) { 298 list.remove(iter.next()); 299 } 300 } 301 302 return propList; 303 } 304 305 308 public boolean isEmpty() { 309 return (list.size() == 0); 310 } 311 312 316 public boolean hasBasicErrors() { 317 List basicErrors = new ArrayList (list); 318 319 basicErrors.removeAll(mappings.values()); 320 return !basicErrors.isEmpty(); 321 } 322 323 326 public boolean hasPropertyErrors() { 327 return !mappings.isEmpty(); 328 } 329 330 334 public boolean hasPropertyErrors(String property) { 335 return (mappings.get(property) != null); 336 } 337 } | Popular Tags |