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 "OR" Boolean logic (checks that at least one
23: * contained constraits is 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 OrConstraint extends BaseCompositeConstraint {
31: private boolean lazy = false;
32:
33: public OrConstraint() {
34: }
35:
36: /**
37: * @since 1.0.9
38: */
39: public OrConstraint(Constraint constraint) {
40: super (constraint);
41: }
42:
43: /**
44: * @param constraints Collection<Constraint>
45: * @since 1.0.9
46: */
47: public OrConstraint(Collection constraints) {
48: super (constraints);
49: }
50:
51: /**
52: * Checks that at least one contained constraits is satisfied.
53: */
54: public void validateConstraint() throws Exception {
55: boolean valid = false;
56:
57: for (Iterator i = constraints.iterator(); i.hasNext();) {
58: Constraint constraint = (Constraint) i.next();
59: valid = valid || constraint.validate();
60: addErrors(constraint.getErrors());
61: constraint.clearErrors();
62: if (valid && this .lazy)
63: break;
64: }
65:
66: if (valid) {
67: clearErrors();
68: }
69: }
70:
71: /**
72: * Sets whether this {@link OrConstraint} acts lazily, default is <code>false</code>.
73: * @param lazy
74: * @return this {@link OrConstraint}
75: */
76: public OrConstraint setLazy(boolean lazy) {
77: this.lazy = lazy;
78: return this;
79: }
80: }
|