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