1 23 24 package org.infoglue.cms.applications.common.actions; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 33 38 public final class Errors { 39 42 private final Map errors = new HashMap (); 44 45 46 47 51 54 public final boolean hasErrors() { 55 return !this.errors.isEmpty(); 56 } 57 58 61 public final boolean hasErrors(String fieldName) { 62 return this.errors.containsKey(fieldName); 63 } 64 65 68 public final Collection getErrors(String fieldName) { 69 return (Collection ) this.errors.get(fieldName); 70 } 71 72 75 public final Collection getAllErrors() { 76 return (Collection ) this.errors.values(); 77 } 78 79 80 83 public final void addError(String fieldName, String errorMessage) { 84 if(getErrors(fieldName) == null) { 85 this.errors.put(fieldName, new ArrayList ()); 86 } 87 getErrors(fieldName).add(errorMessage); 88 } 89 90 91 92 95 98 public String toString() { 99 final StringBuffer sb = new StringBuffer ("<Errors>[ "); 100 101 for(Iterator fieldNames = this.errors.keySet().iterator(); fieldNames.hasNext(); ) { 102 final String fieldName = (String ) fieldNames.next(); 103 final Iterator errorMessages = getErrors(fieldName).iterator(); 104 105 sb.append(fieldName + "=> { "); 106 sb.append(toString(errorMessages)); 107 sb.append("}" + (fieldNames.hasNext() ? ", " : " ")); 108 } 109 sb.append("]"); 110 return sb.toString(); 111 } 112 113 114 115 118 121 private String toString(Iterator errorMessages) { 122 final StringBuffer sb = new StringBuffer (); 123 124 while(errorMessages.hasNext()) { 125 final String errorMessage = (String ) errorMessages.next(); 126 sb.append("\"" + errorMessage + "\"" + (errorMessages.hasNext() ? ", " : " ")); 127 } 128 return sb.toString(); 129 } 130 131 132 133 } | Popular Tags |