1 56 package org.objectstyle.cayenne.validation; 57 58 import java.io.Serializable ; 59 import java.util.ArrayList ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import org.objectstyle.cayenne.util.Util; 65 66 74 public class ValidationResult implements Serializable { 75 76 private List failures; 77 78 public ValidationResult() { 79 failures = new ArrayList (); 80 } 81 82 88 public void addFailure(ValidationFailure failure) { 89 if (failure == null) { 90 throw new IllegalArgumentException ("failure cannot be null."); 91 } 92 93 failures.add(failure); 94 } 95 96 99 public List getFailures() { 100 return Collections.unmodifiableList(failures); 101 } 102 103 110 public List getFailures(Object source) { 111 112 ArrayList matchingFailures = new ArrayList (5); 113 Iterator it = failures.iterator(); 114 while (it.hasNext()) { 115 ValidationFailure failure = (ValidationFailure) it.next(); 116 if (Util.nullSafeEquals(source, failure.getSource())) { 117 matchingFailures.add(failure); 118 } 119 } 120 121 return matchingFailures; 122 } 123 124 128 public boolean hasFailures() { 129 return !failures.isEmpty(); 130 } 131 132 137 public boolean hasFailures(Object source) { 138 Iterator it = failures.iterator(); 139 while (it.hasNext()) { 140 ValidationFailure failure = (ValidationFailure) it.next(); 141 if (Util.nullSafeEquals(source, failure.getSource())) { 142 return true; 143 } 144 } 145 146 return false; 147 } 148 149 public String toString() { 150 StringBuffer ret = new StringBuffer (); 151 String separator = System.getProperty("line.separator"); 152 153 Iterator it = getFailures().iterator(); 154 while (it.hasNext()) { 155 if (ret.length() > 0) { 156 ret.append(separator); 157 } 158 159 ret.append(it.next()); 160 } 161 162 return ret.toString(); 163 } 164 } | Popular Tags |