001    /*
002     * The Spring Framework is published under the terms of the Apache Software
003     * License.
004     */
005    package org.springframework.rules.constraint.property;
006    
007    import org.springframework.binding.PropertyAccessStrategy;
008    import org.springframework.binding.support.BeanPropertyAccessStrategy;
009    import org.springframework.util.Assert;
010    
011    /**
012     * Convenience superclass for bean property expressions.
013     *
014     * @author Keith Donald
015     */
016    public abstract class AbstractPropertyConstraint implements PropertyConstraint {
017    
018            private String propertyName;
019    
020            protected AbstractPropertyConstraint() {
021            }
022    
023            protected AbstractPropertyConstraint(String propertyName) {
024                    setPropertyName(propertyName);
025            }
026    
027            public String getPropertyName() {
028                    return propertyName;
029            }
030    
031            public boolean isDependentOn(String propertyName) {
032                    return getPropertyName().equals(propertyName);
033            }
034            
035            public boolean isCompoundRule() {
036                    return false;
037            }
038    
039            protected void setPropertyName(String propertyName) {
040                    Assert.notNull(propertyName, "The propertyName to constrain is required");
041                    this.propertyName = propertyName;
042            }
043    
044            public boolean test(Object o) {
045                    if (o instanceof PropertyAccessStrategy)
046                            return test((PropertyAccessStrategy)o);
047    
048            return test(new BeanPropertyAccessStrategy(o));
049            }
050    
051            protected abstract boolean test(PropertyAccessStrategy domainObjectAccessStrategy);
052    
053            public String toString() {
054                    return getPropertyName();
055            }
056    
057    }