1 19 20 package org.apache.cayenne.validation; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.cayenne.util.Util; 29 30 38 public class ValidationResult implements Serializable { 39 40 private List failures; 41 42 public ValidationResult() { 43 failures = new ArrayList (); 44 } 45 46 52 public void addFailure(ValidationFailure failure) { 53 if (failure == null) { 54 throw new IllegalArgumentException ("failure cannot be null."); 55 } 56 57 failures.add(failure); 58 } 59 60 63 public List getFailures() { 64 return Collections.unmodifiableList(failures); 65 } 66 67 74 public List getFailures(Object source) { 75 76 ArrayList matchingFailures = new ArrayList (5); 77 Iterator it = failures.iterator(); 78 while (it.hasNext()) { 79 ValidationFailure failure = (ValidationFailure) it.next(); 80 if (Util.nullSafeEquals(source, failure.getSource())) { 81 matchingFailures.add(failure); 82 } 83 } 84 85 return matchingFailures; 86 } 87 88 92 public boolean hasFailures() { 93 return !failures.isEmpty(); 94 } 95 96 101 public boolean hasFailures(Object source) { 102 Iterator it = failures.iterator(); 103 while (it.hasNext()) { 104 ValidationFailure failure = (ValidationFailure) it.next(); 105 if (Util.nullSafeEquals(source, failure.getSource())) { 106 return true; 107 } 108 } 109 110 return false; 111 } 112 113 public String toString() { 114 StringBuffer ret = new StringBuffer (); 115 String separator = System.getProperty("line.separator"); 116 117 Iterator it = getFailures().iterator(); 118 while (it.hasNext()) { 119 if (ret.length() > 0) { 120 ret.append(separator); 121 } 122 123 ret.append(it.next()); 124 } 125 126 return ret.toString(); 127 } 128 } 129 | Popular Tags |