01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.uilib.form.constraint;
16:
17: import java.util.Collection;
18: import java.util.Iterator;
19: import org.araneaframework.uilib.form.Constraint;
20:
21: /**
22: * This constraint implements "AND" Boolean logic (checks that all
23: * contained constraits are satisfied). It is eager by default, but can
24: * be set to act lazily, (note that subconstraints produce error messages
25: * as they are being validated, unless some custom error message has been set,
26: * it makes often sense to process all subconstraints).
27: *
28: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
29: */
30: public class AndConstraint extends BaseCompositeConstraint {
31: private boolean lazy = false;
32:
33: public AndConstraint() {
34: }
35:
36: /**
37: * @since 1.0.9
38: */
39: public AndConstraint(Constraint constraint) {
40: super (constraint);
41: }
42:
43: /**
44: * @param constraints Collection<Constraint>
45: * @since 1.0.9
46: */
47: public AndConstraint(Collection constraints) {
48: super (constraints);
49: }
50:
51: /**
52: * Checks that all contained constraits are satisfied.
53: */
54: public void validateConstraint() throws Exception {
55: for (Iterator i = constraints.iterator(); i.hasNext();) {
56: Constraint constraint = (Constraint) i.next();
57: boolean valid = constraint.validate();
58: addErrors(constraint.getErrors());
59: constraint.clearErrors();
60: if (!valid && this .lazy)
61: break;
62: }
63: }
64:
65: /**
66: * Sets whether this {@link AndConstraint} acts lazily, default is <code>false</code>.
67: * @param lazy
68: * @return this {@link AndConstraint}
69: */
70: public AndConstraint setLazy(boolean lazy) {
71: this.lazy = lazy;
72: return this;
73: }
74: }
|