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 NotEqualCheck extends AbstractAnnotationCheck<NotEqual>
28  {
29  	private static final long serialVersionUID = 1L;
30  
31  	private boolean ignoreCase;
32  	private String testString;
33  	private transient String testStringLowerCase;
34  
35  	/**
36  	 * {@inheritDoc}
37  	 */
38  	@Override
39  	public void configure(final NotEqual constraintAnnotation)
40  	{
41  		super.configure(constraintAnnotation);
42  		setIgnoreCase(constraintAnnotation.ignoreCase());
43  		setTestString(constraintAnnotation.value());
44  	}
45  
46  	/**
47  	 * {@inheritDoc}
48  	 */
49  	@Override
50  	protected Map<String, String> createMessageVariables()
51  	{
52  		final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
53  		messageVariables.put("ignoreCase", Boolean.toString(ignoreCase));
54  		messageVariables.put("testString", testString);
55  		return messageVariables;
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	@Override
62  	protected ConstraintTarget[] getAppliesToDefault()
63  	{
64  		return new ConstraintTarget[]{ConstraintTarget.VALUES};
65  	}
66  
67  	/**
68  	 * @return the testString
69  	 */
70  	public String getTestString()
71  	{
72  		return testString;
73  	}
74  
75  	private String getTestStringLowerCase()
76  	{
77  		if (testStringLowerCase == null && testString != null)
78  		{
79  			testStringLowerCase = testString.toLowerCase(Validator.getLocaleProvider().getLocale());
80  		}
81  		return testStringLowerCase;
82  	}
83  
84  	/**
85  	 * @return the ignoreCase
86  	 */
87  	public boolean isIgnoreCase()
88  	{
89  		return ignoreCase;
90  	}
91  
92  	/**
93  	 * {@inheritDoc}
94  	 */
95  	public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
96  			final Validator validator)
97  	{
98  		if (valueToValidate == null) return true;
99  
100 		if (ignoreCase)
101 			return !valueToValidate.toString().toLowerCase(Validator.getLocaleProvider().getLocale()).equals(getTestStringLowerCase());
102 
103 		return !valueToValidate.toString().equals(testString);
104 	}
105 
106 	/**
107 	 * @param ignoreCase the ignoreCase to set
108 	 */
109 	public void setIgnoreCase(final boolean ignoreCase)
110 	{
111 		this.ignoreCase = ignoreCase;
112 		requireMessageVariablesRecreation();
113 	}
114 
115 	/**
116 	 * @param testString the testString to set
117 	 */
118 	public void setTestString(final String testString)
119 	{
120 		this.testString = testString;
121 		requireMessageVariablesRecreation();
122 	}
123 }