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.core.enums.StringCodedLabeledEnum;
020    import org.springframework.util.StringUtils;
021    
022    /**
023     * A like constraint, supporting "starts with%", "%ends with", and "%contains%".
024     *
025     * @author Keith Donald
026     */
027    public class Like implements Constraint {
028    
029            public static final LikeType STARTS_WITH = new LikeType("startsWith");
030    
031            public static final LikeType ENDS_WITH = new LikeType("endsWith");
032    
033            public static final LikeType CONTAINS = new LikeType("contains");
034    
035            private LikeType type;
036    
037            private String stringToMatch;
038    
039            public Like(LikeType type, String likeString) {
040                    this.type = type;
041                    this.stringToMatch = likeString;
042            }
043    
044            public Like(String encodedLikeString) {
045                    if (encodedLikeString.startsWith("%")) {
046                            if (encodedLikeString.endsWith("%")) {
047                                    this.type = CONTAINS;
048                            }
049                            else {
050                                    this.type = ENDS_WITH;
051                            }
052                    }
053                    else if (encodedLikeString.endsWith("%")) {
054                            this.type = STARTS_WITH;
055                    }
056                    else {
057                            this.type = CONTAINS;
058                    }
059                    stringToMatch = StringUtils.deleteAny(encodedLikeString, "%");
060            }
061    
062            public boolean test(Object argument) {
063                    String value = String.valueOf(argument);
064                    if (type == STARTS_WITH) {
065                            return value.startsWith(stringToMatch);
066                    }
067                    else if (type == ENDS_WITH) {
068                            return value.endsWith(stringToMatch);
069                    }
070                    else {
071                            return value.indexOf(stringToMatch) != -1;
072                    }
073            }
074    
075            public LikeType getType() {
076                    return type;
077            }
078    
079            public String getString() {
080                    return stringToMatch;
081            }
082        
083        public static class LikeType extends StringCodedLabeledEnum {
084            private LikeType(String code) {
085                super(code, null);
086            }
087        }
088    
089    }