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 MaxLengthCheck extends AbstractAnnotationCheck<MaxLength> {
25: private static final long serialVersionUID = 1L;
26:
27: private int max;
28:
29: @Override
30: public void configure(final MaxLength constraintAnnotation) {
31: super .configure(constraintAnnotation);
32: setMax(constraintAnnotation.value());
33: }
34:
35: /**
36: * @return the max
37: */
38: public int getMax() {
39: return max;
40: }
41:
42: @Override
43: public Map<String, String> getMessageVariables() {
44: final Map<String, String> messageVariables = CollectionFactoryHolder
45: .getFactory().createMap(2);
46: messageVariables.put("max", Integer.toString(max));
47: return messageVariables;
48: }
49:
50: public boolean isSatisfied(final Object validatedObject,
51: final Object value, final OValContext context,
52: final Validator validator) {
53: if (value == null)
54: return true;
55:
56: final int len = value.toString().length();
57: return len <= max;
58: }
59:
60: /**
61: * @param max the max to set
62: */
63: public void setMax(final int max) {
64: this.max = max;
65: }
66: }
|