View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2015 Sebastian
3    * Thomschke.
4    *
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    *
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.constraint;
14  
15  import static net.sf.oval.Validator.*;
16  
17  import java.util.Map;
18  
19  import net.sf.oval.ConstraintTarget;
20  import net.sf.oval.Validator;
21  import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
22  import net.sf.oval.context.OValContext;
23  
24  /**
25   * @author Sebastian Thomschke
26   */
27  public class HasSubstringCheck extends AbstractAnnotationCheck<HasSubstring>
28  {
29  	private static final long serialVersionUID = 1L;
30  
31  	private boolean ignoreCase;
32  
33  	private String substring;
34  	private transient String substringLowerCase;
35  
36  	/**
37  	 * {@inheritDoc}
38  	 */
39  	@Override
40  	public void configure(final HasSubstring constraintAnnotation)
41  	{
42  		super.configure(constraintAnnotation);
43  		setIgnoreCase(constraintAnnotation.ignoreCase());
44  		setSubstring(constraintAnnotation.value());
45  	}
46  
47  	/**
48  	 * {@inheritDoc}
49  	 */
50  	@Override
51  	protected Map<String, String> createMessageVariables()
52  	{
53  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
54  		messageVariables.put("ignoreCase", Boolean.toString(ignoreCase));
55  		messageVariables.put("substring", substring);
56  		return messageVariables;
57  	}
58  
59  	/**
60  	 * {@inheritDoc}
61  	 */
62  	@Override
63  	protected ConstraintTarget[] getAppliesToDefault()
64  	{
65  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
66  	}
67  
68  	/**
69  	 * @return the substring
70  	 */
71  	public String getSubstring()
72  	{
73  		return substring;
74  	}
75  
76  	private String getSubstringLowerCase()
77  	{
78  		if (substringLowerCase == null && substring != null)
79  		{
80  			substringLowerCase = substring.toLowerCase(Validator.getLocaleProvider().getLocale());
81  		}
82  		return substringLowerCase;
83  	}
84  
85  	/**
86  	 * @return the ignoreCase
87  	 */
88  	public boolean isIgnoreCase()
89  	{
90  		return ignoreCase;
91  	}
92  
93  	/**
94  	 * {@inheritDoc}
95  	 */
96  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
97  			final Validator validator)
98  	{
99  		if (valueToValidate == null) return true;
100 
101 		if (ignoreCase)
102 			return valueToValidate.toString().toLowerCase(Validator.getLocaleProvider().getLocale()).indexOf(getSubstringLowerCase()) > -1;
103 
104 		return valueToValidate.toString().indexOf(substring) > -1;
105 	}
106 
107 	/**
108 	 * @param ignoreCase the ignoreCase to set
109 	 */
110 	public void setIgnoreCase(final boolean ignoreCase)
111 	{
112 		this.ignoreCase = ignoreCase;
113 		requireMessageVariablesRecreation();
114 	}
115 
116 	/**
117 	 * @param substring the substring to set
118 	 */
119 	public void setSubstring(final String substring)
120 	{
121 		this.substring = substring;
122 		requireMessageVariablesRecreation();
123 	}
124 }