001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy
006     * of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations
014     * under the License.
015     */
016    package org.springframework.rules.constraint.property;
017    
018    import java.util.Iterator;
019    
020    import org.springframework.rules.constraint.Constraint;
021    import org.springframework.rules.constraint.CompoundConstraint;
022    import org.springframework.rules.constraint.AbstractConstraint;
023    
024    /**
025     * Abstract base class for unary predicates which compose other predicates.
026     * 
027     * @author Keith Donald
028     */
029    public class CompoundPropertyConstraint implements PropertyConstraint {
030    
031            private CompoundConstraint compoundConstraint;
032    
033            public String getPropertyName() {
034                    PropertyConstraint e = (PropertyConstraint)compoundConstraint.iterator().next();
035                    return e.getPropertyName();
036            }
037    
038            public boolean isDependentOn(final String propertyName) {
039                    return new AbstractConstraint() {
040                            public boolean test(Object o) {
041                                    return ((PropertyConstraint)o).isDependentOn(propertyName);
042                            }
043                    }.anyTrue(compoundConstraint.iterator());
044            }
045            
046            public boolean isCompoundRule() {
047                    return true;
048            }
049    
050            public Constraint getPredicate() {
051                    return compoundConstraint;
052            }
053    
054            /**
055             * Constructs a compound predicate with no initial members. It is expected
056             * the client will call "add" to add individual predicates.
057             */
058            public CompoundPropertyConstraint(CompoundConstraint compoundConstraint) {
059                    this.compoundConstraint = compoundConstraint;
060                    this.compoundConstraint.validateTypeSafety(PropertyConstraint.class);
061            }
062    
063            /**
064             * Add the specified predicate to the set of predicates aggregated by this
065             * compound predicate.
066             * 
067             * @param predicate
068             *            the predicate to add
069             * @return A reference to this, to support chaining.
070             */
071            public CompoundPropertyConstraint add(PropertyConstraint constraint) {
072                    this.compoundConstraint.add(constraint);
073                    return this;
074            }
075    
076            /**
077             * Return an iterator over the aggregated predicates.
078             * 
079             * @return An iterator
080             */
081            public Iterator iterator() {
082                    return compoundConstraint.iterator();
083            }
084    
085            public boolean test(Object bean) {
086                    return compoundConstraint.test(bean);
087            }
088    
089            public String toString() {
090                    return compoundConstraint.toString();
091            }
092    
093    }