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;
017    
018    import org.springframework.rules.constraint.Constraint;
019    import org.springframework.rules.closure.BinaryConstraint;
020    import org.springframework.rules.closure.StringLength;
021    import org.springframework.rules.reporting.TypeResolvable;
022    import org.springframework.util.Assert;
023    
024    /**
025     * Constraint to validate an object's string length.
026     * 
027     * @author Keith Donald
028     */
029    public class StringLengthConstraint extends AbstractConstraint implements TypeResolvable {
030    
031            private static final long serialVersionUID = 1L;
032    
033            private Constraint lengthConstraint;
034        
035        private String type;
036    
037        /**
038         * Constructs a maxlength constraint of the specified length.
039         * 
040         * @param length
041         *            the max string length
042         */
043        public StringLengthConstraint(int length) {
044            this(RelationalOperator.LESS_THAN_EQUAL_TO, length);
045        }
046    
047        /**
048         * Constructs a maxlength constraint of the specified length.
049         * 
050         * @param length
051         *            the max string length
052         */
053        public StringLengthConstraint(int length, String type) {
054            this(RelationalOperator.LESS_THAN_EQUAL_TO, length, type);
055        }
056        
057        /**
058         * Constructs a string length constraint with the specified operator and
059         * length constraint.
060         * 
061         * @param operator
062         *            the operator (one of ==, >, >=, <, <=)
063         * @param length
064         *            the length constraint
065         */
066        public StringLengthConstraint(RelationalOperator operator, int length) {
067            this(operator, length, null);
068        }
069    
070        /**
071         * Constructs a string length constraint with the specified operator and
072         * length constraint.
073         * 
074         * @param operator
075         *            the operator (one of ==, >, >=, <, <=)
076         * @param length
077         *            the length constraint
078         * @param type
079         *            Type used to resolve messages.
080         */
081        public StringLengthConstraint(RelationalOperator operator, int length, String type) {
082            Assert.notNull(operator, "The relational operator is required");
083            Assert.isTrue(length > 0, "length is required");
084            BinaryConstraint comparer = operator.getConstraint();
085            Constraint lengthConstraint = bind(comparer, length);
086            this.lengthConstraint = testResultOf(StringLength.instance(),
087                    lengthConstraint);
088            this.type = type;
089        }
090        
091        /**
092         * Constructs a string length range constraint.
093         * 
094         * @param min
095         *            The minimum edge of the range
096         * @param max
097         *            the maximum edge of the range
098         */
099        public StringLengthConstraint(int min, int max) {
100            this(min, max, null);
101        }
102        
103        /**
104         * Constructs a string length range constraint.
105         * 
106         * @param min
107         *            The minimum edge of the range
108         * @param max
109         *            the maximum edge of the range
110         */
111        public StringLengthConstraint(int min, int max, String type) {
112            Constraint rangeConstraint = new Range(min, max);
113            this.lengthConstraint = testResultOf(StringLength.instance(),
114                    rangeConstraint);
115            this.type = type;
116        }
117    
118        /**
119         * Tests that the string form of this argument falls within the length
120         * constraint.
121         * 
122         * @see Constraint#test(java.lang.Object)
123         */
124        public boolean test(Object argument) {
125            return this.lengthConstraint.test(argument);
126        }
127    
128        public Constraint getPredicate() {
129            return lengthConstraint;
130        }
131    
132        public String toString() {
133            return lengthConstraint.toString();
134        }
135    
136            public String getType() {
137                    return type;
138            }
139    
140    }