1 23 24 package org.infoglue.cms.util; 25 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 30 import org.infoglue.cms.exception.AccessConstraintException; 31 import org.infoglue.cms.exception.ConstraintException; 32 33 34 40 41 public class ConstraintExceptionBuffer 42 { 43 protected Set exceptions = new HashSet (); 46 47 50 public ConstraintExceptionBuffer() {} 51 52 57 public ConstraintExceptionBuffer(ConstraintException exception) { 58 add(exception); 59 } 60 61 62 63 65 70 public final boolean isEmpty() { 71 return this.exceptions.isEmpty(); 72 } 73 74 80 public final void add(ConstraintExceptionBuffer ceb) { 81 if(ceb != null) { 82 this.exceptions.addAll(ceb.exceptions); 83 } 84 } 85 86 92 public final void add(ConstraintException exception) { 93 if(exception != null) { 94 final String fieldName = exception.getFieldName(); 96 final String errorCode = exception.getErrorCode(); 97 ConstraintException ce = new ConstraintException(fieldName, errorCode); 98 this.exceptions.add(ce); 99 add(exception.getChainedException()); 100 } 101 } 102 103 108 public void throwIfNotEmpty() throws AccessConstraintException, ConstraintException { 109 if(!isEmpty()) { 110 throw toConstraintException(); 111 } 112 } 113 114 119 public ConstraintException toConstraintException() 120 { 121 ConstraintException rootException = null; 122 for(Iterator iterator = this.exceptions.iterator(); iterator.hasNext(); ) 123 { 124 ConstraintException ce = (ConstraintException) iterator.next(); 125 ce.setChainedException(rootException); 126 rootException = ce; 127 } 128 return rootException; 129 } 130 131 132 133 136 139 public String toString() { 140 final StringBuffer sb = new StringBuffer (); 141 142 sb.append("<ConstraintExceptionBuffer>[{ "); 143 for(Iterator iterator = this.exceptions.iterator(); iterator.hasNext(); ) { 144 sb.append(iterator.next() + (iterator.hasNext() ? ", " : "")); 145 } 146 sb.append("}]"); 147 return sb.toString(); 148 } 149 150 157 public boolean equals(Object o) { 158 if(o == null || !(o instanceof ConstraintExceptionBuffer)) { 159 return false; 160 } 161 final ConstraintExceptionBuffer ceb = (ConstraintExceptionBuffer) o; 162 return this.exceptions.equals(ceb.exceptions); 163 } 164 165 171 public int hashCode() { 172 return this.exceptions.hashCode(); 173 } 174 175 176 177 } | Popular Tags |