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.lang.reflect.Array;
15: import java.util.Collection;
16: import java.util.Map;
17:
18: import net.sf.oval.Validator;
19: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
20: import net.sf.oval.context.OValContext;
21: import net.sf.oval.internal.CollectionFactoryHolder;
22:
23: /**
24: * @author Sebastian Thomschke
25: */
26: public class MinSizeCheck extends AbstractAnnotationCheck<MinSize> {
27: private static final long serialVersionUID = 1L;
28:
29: private int min;
30:
31: @Override
32: public void configure(final MinSize constraintAnnotation) {
33: super .configure(constraintAnnotation);
34: setMin(constraintAnnotation.value());
35: }
36:
37: @Override
38: public Map<String, String> getMessageVariables() {
39: final Map<String, String> messageVariables = CollectionFactoryHolder
40: .getFactory().createMap(2);
41: messageVariables.put("min", Integer.toString(min));
42: return messageVariables;
43: }
44:
45: /**
46: * @return the min
47: */
48: public int getMin() {
49: return min;
50: }
51:
52: public boolean isSatisfied(final Object validatedObject,
53: final Object value, final OValContext context,
54: final Validator validator) {
55: if (value == null)
56: return true;
57:
58: if (value instanceof Collection) {
59: final int size = ((Collection) value).size();
60: return size >= min;
61: }
62: if (value instanceof Map) {
63: final int size = ((Map) value).size();
64: return size >= min;
65: }
66: if (value.getClass().isArray()) {
67: final int size = Array.getLength(value);
68: return size >= min;
69: }
70: return false;
71: }
72:
73: /**
74: * @param min the min to set
75: */
76: public void setMin(final int min) {
77: this.min = min;
78: }
79: }
|