KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > ConstraintExceptionBuffer


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.infoglue.cms.exception.AccessConstraintException;
31 import org.infoglue.cms.exception.ConstraintException;
32
33
34 /**
35  *
36  * Any duplicate (ConstraintException.equals()) will be silently discarded.
37  *
38  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
39  */

40
41 public class ConstraintExceptionBuffer
42 {
43     // Note! The chainedException field for all exceptions is set to null.
44
protected Set JavaDoc exceptions = new HashSet JavaDoc(); // type: <ConstraintException>
45

46
47   /**
48    * Constructs a ConstraintExceptionBuffer with no exceptions in it.
49    */

50   public ConstraintExceptionBuffer() {}
51
52   /**
53    * Constructs a ConstraintExceptionBuffer containing the specified exception(s).
54    *
55    * @param exception the initial exception(s) to add.
56    */

57   public ConstraintExceptionBuffer(ConstraintException exception) {
58     add(exception);
59   }
60
61
62
63   // --- [Public] --------------------------------------------------------------
64

65   /**
66    * Returns true if this buffer contains no exceptions; false otherwise.
67    *
68    * @return true if this buffer contains no exceptions; false otherwise.
69    */

70   public final boolean isEmpty() {
71     return this.exceptions.isEmpty();
72   }
73
74   /**
75    * Adds the exceptions of the specified buffer to this buffer.
76    * Any duplicate (ConstraintException.equals()) will be silently discarded.
77    *
78    * @param ceb the buffer to add from (empty buffers legal).
79    */

80   public final void add(ConstraintExceptionBuffer ceb) {
81     if(ceb != null) {
82       this.exceptions.addAll(ceb.exceptions);
83     }
84   }
85
86   /**
87    * Adds the specified exception to the exceptions of this buffer.
88    * Any duplicate (ConstraintException.equals()) will be silently discarded.
89    *
90    * @param exception the exception to add (chained exceptions legal).
91    */

92   public final void add(ConstraintException exception) {
93     if(exception != null) {
94       // set chained to null but don't mess with the parameter.
95
final String JavaDoc fieldName = exception.getFieldName();
96       final String JavaDoc errorCode = exception.getErrorCode();
97         ConstraintException ce = new ConstraintException(fieldName, errorCode);
98       this.exceptions.add(ce);
99       add(exception.getChainedException());
100     }
101   }
102
103   /**
104    * Throws the root exception; if this buffer contains no exceptions, nothing happens.
105    *
106    * @throws org.infoglue.cms.exception.ConstraintException if this buffer contains any exceptions.
107    */

108   public void throwIfNotEmpty() throws AccessConstraintException, ConstraintException {
109     if(!isEmpty()) {
110       throw toConstraintException();
111     }
112   }
113
114   /**
115    * Converts the exceptions of this buffer to chained constraint exceptions.
116    *
117    * @return the root exception of the chain.
118    */

119   public ConstraintException toConstraintException()
120   {
121     ConstraintException rootException = null;
122     for(Iterator JavaDoc 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   // --- [X implementation] ----------------------------------------------------
134
// --- [java.lang.Object Overrides] ------------------------------------------
135

136   /**
137    *
138    */

139   public String JavaDoc toString() {
140     final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141
142     sb.append("<ConstraintExceptionBuffer>[{ ");
143     for(Iterator JavaDoc iterator = this.exceptions.iterator(); iterator.hasNext(); ) {
144       sb.append(iterator.next() + (iterator.hasNext() ? ", " : ""));
145     }
146     sb.append("}]");
147     return sb.toString();
148   }
149
150   /**
151    * Returns true if the specified object is a buffer containing exactly the same
152    * exceptions as this one.
153    *
154    * @param o the reference object with which to compare.
155    * @return true if this object is the same as the specified object; false otherwise.
156    */

157   public boolean equals(Object JavaDoc 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   /**
166    * Note! It is generally necessary to override the hashCode method whenever the equals() method is overridden.
167    * Returns a hash code value for the object.
168    *
169    * @return a hash code value for this object.
170    */

171   public int hashCode() {
172     return this.exceptions.hashCode();
173   }
174
175
176
177   // --- [Package protected] ---------------------------------------------------
178
// --- [Private] -------------------------------------------------------------
179
// --- [Protected] -----------------------------------------------------------
180
// --- [Inner classes] -------------------------------------------------------
181
}
Popular Tags