001    /*******************************************************************************
002     * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
003     * Thomschke.
004     * 
005     * All Rights Reserved. This program and the accompanying materials
006     * are made available under the terms of the Eclipse Public License v1.0
007     * which accompanies this distribution, and is available at
008     * http://www.eclipse.org/legal/epl-v10.html
009     * 
010     * Contributors:
011     *     Sebastian Thomschke - initial implementation.
012     *******************************************************************************/
013    package net.sf.oval.constraint;
014    
015    import static net.sf.oval.Validator.getCollectionFactory;
016    
017    import java.util.Locale;
018    import java.util.Map;
019    
020    import net.sf.oval.ConstraintTarget;
021    import net.sf.oval.Validator;
022    import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
023    import net.sf.oval.context.OValContext;
024    
025    /**
026     * @author Sebastian Thomschke
027     */
028    public class NotEqualCheck extends AbstractAnnotationCheck<NotEqual>
029    {
030            private static final long serialVersionUID = 1L;
031    
032            private boolean ignoreCase;
033            private String testString;
034            private transient String testStringLowerCase;
035    
036            /**
037             * {@inheritDoc}
038             */
039            @Override
040            public void configure(final NotEqual constraintAnnotation)
041            {
042                    super.configure(constraintAnnotation);
043                    setIgnoreCase(constraintAnnotation.ignoreCase());
044                    setTestString(constraintAnnotation.value());
045            }
046    
047            /**
048             * {@inheritDoc}
049             */
050            @Override
051            protected Map<String, String> createMessageVariables()
052            {
053                    final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
054                    messageVariables.put("ignoreCase", Boolean.toString(ignoreCase));
055                    messageVariables.put("testString", testString);
056                    return messageVariables;
057            }
058    
059            /**
060             * {@inheritDoc}
061             */
062            @Override
063            protected ConstraintTarget[] getAppliesToDefault()
064            {
065                    return new ConstraintTarget[]{ConstraintTarget.VALUES};
066            }
067    
068            /**
069             * @return the testString
070             */
071            public String getTestString()
072            {
073                    return testString;
074            }
075    
076            private String getTestStringLowerCase()
077            {
078                    if (testStringLowerCase == null && testString != null)
079                            testStringLowerCase = testString.toLowerCase(Locale.getDefault());
080                    return testStringLowerCase;
081            }
082    
083            /**
084             * @return the ignoreCase
085             */
086            public boolean isIgnoreCase()
087            {
088                    return ignoreCase;
089            }
090    
091            /**
092             * {@inheritDoc}
093             */
094            public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context,
095                            final Validator validator)
096            {
097                    if (valueToValidate == null) return true;
098    
099                    if (ignoreCase)
100                            return !valueToValidate.toString().toLowerCase(Locale.getDefault()).equals(getTestStringLowerCase());
101    
102                    return !valueToValidate.toString().equals(testString);
103            }
104    
105            /**
106             * @param ignoreCase the ignoreCase to set
107             */
108            public void setIgnoreCase(final boolean ignoreCase)
109            {
110                    this.ignoreCase = ignoreCase;
111                    requireMessageVariablesRecreation();
112            }
113    
114            /**
115             * @param testString the testString to set
116             */
117            public void setTestString(final String testString)
118            {
119                    this.testString = testString;
120                    requireMessageVariablesRecreation();
121            }
122    }