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 of
006     * 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 under
014     * the License.
015     */
016    package org.springframework.rules.constraint.property;
017    
018    import org.springframework.rules.constraint.Constraint;
019    import org.springframework.rules.closure.BinaryConstraint;
020    import org.springframework.rules.constraint.ParameterizedBinaryConstraint;
021    
022    /**
023     * A constraint that returns the result of a <code>boolean</code>
024     * expression that tests a variable bean property value against a constant
025     * parameter value. For example: <code>pet.age > 5</code>
026     * 
027     * @author Keith Donald
028     */
029    public class ParameterizedPropertyConstraint implements PropertyConstraint {
030            private PropertyValueConstraint parameterizedExpression;
031    
032            /**
033             * Creates a BeanPropertyExpressionTester.
034             * 
035             * @param propertyName
036             *            The property participating in the expression.
037             * @param expression
038             *            The expression predicate (tester).
039             * @param parameter
040             *            The constant parameter value participating in the expression.
041             */
042            public ParameterizedPropertyConstraint(String propertyName, BinaryConstraint expression, Object parameter) {
043                    this(propertyName, new ParameterizedBinaryConstraint(expression, parameter));
044            }
045    
046            public ParameterizedPropertyConstraint(String propertyName, Constraint parameterizedExpression) {
047                    this.parameterizedExpression = new PropertyValueConstraint(propertyName, parameterizedExpression);
048            }
049    
050            public String getPropertyName() {
051                    return parameterizedExpression.getPropertyName();
052            }
053    
054            public boolean isDependentOn(String propertyName) {
055                    return parameterizedExpression.isDependentOn(propertyName);
056            }
057    
058            public boolean isCompoundRule() {
059                    return parameterizedExpression.isCompoundRule();
060            }
061    
062            public BinaryConstraint getConstraint() {
063                    return getParameterizedBinaryConstraint().getConstraint();
064            }
065    
066            public Object getParameter() {
067                    return getParameterizedBinaryConstraint().getParameter();
068            }
069    
070            private ParameterizedBinaryConstraint getParameterizedBinaryConstraint() {
071                    return (ParameterizedBinaryConstraint)this.parameterizedExpression.getConstraint();
072            }
073    
074            /**
075             * Tests the value of the configured propertyName for this bean against the
076             * configured parameter value using the configured binary predicate.
077             * 
078             * @see org.springframework.rules.constraint.Constraint#test(java.lang.Object)
079             */
080            public boolean test(Object bean) {
081                    return parameterizedExpression.test(bean);
082            }
083    
084            public String toString() {
085                    return getPropertyName() + " " + parameterizedExpression.toString();
086            }
087    }