01: /*******************************************************************************
02: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
03: * Thomschke.
04: *
05: * All Rights Reserved. This program and the accompanying materials
06: * are made available under the terms of the Eclipse Public License v1.0
07: * which accompanies this distribution, and is available at
08: * http://www.eclipse.org/legal/epl-v10.html
09: *
10: * Contributors:
11: * Sebastian Thomschke - initial implementation.
12: *******************************************************************************/package net.sf.oval.constraint;
13:
14: import java.util.Map;
15:
16: import net.sf.oval.Validator;
17: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
18: import net.sf.oval.context.OValContext;
19: import net.sf.oval.internal.CollectionFactoryHolder;
20:
21: /**
22: * @author Sebastian Thomschke
23: */
24: public class NotEqualCheck extends AbstractAnnotationCheck<NotEqual> {
25: private static final long serialVersionUID = 1L;
26:
27: private boolean ignoreCase;
28: private String testString;
29: private transient String testStringLowerCase;
30:
31: @Override
32: public void configure(final NotEqual constraintAnnotation) {
33: super .configure(constraintAnnotation);
34: setIgnoreCase(constraintAnnotation.ignoreCase());
35: setTestString(constraintAnnotation.value());
36: }
37:
38: @Override
39: public Map<String, String> getMessageVariables() {
40: final Map<String, String> messageVariables = CollectionFactoryHolder
41: .getFactory().createMap(2);
42: messageVariables
43: .put("ignoreCase", Boolean.toString(ignoreCase));
44: messageVariables.put("testString", testString);
45: return messageVariables;
46: }
47:
48: /**
49: * @return the testString
50: */
51: public String getTestString() {
52: return testString;
53: }
54:
55: private String getTestStringLowerCase() {
56: if (testStringLowerCase == null && testString != null)
57: testStringLowerCase = testString.toLowerCase();
58: return testStringLowerCase;
59: }
60:
61: /**
62: * @return the ignoreCase
63: */
64: public boolean isIgnoreCase() {
65: return ignoreCase;
66: }
67:
68: public boolean isSatisfied(final Object validatedObject,
69: final Object value, final OValContext context,
70: final Validator validator) {
71: if (value == null)
72: return true;
73:
74: if (ignoreCase)
75: return !value.toString().toLowerCase().equals(
76: getTestStringLowerCase());
77:
78: return !value.toString().equals(testString);
79: }
80:
81: /**
82: * @param ignoreCase the ignoreCase to set
83: */
84: public void setIgnoreCase(final boolean ignoreCase) {
85: this .ignoreCase = ignoreCase;
86: }
87:
88: /**
89: * @param testString the testString to set
90: */
91: public void setTestString(final String testString) {
92: this.testString = testString;
93: }
94: }
|