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 MinCheck extends AbstractAnnotationCheck<Min> {
25: private static final long serialVersionUID = 1L;
26:
27: private long min;
28:
29: @Override
30: public void configure(final Min constraintAnnotation) {
31: super .configure(constraintAnnotation);
32: setMin(constraintAnnotation.value());
33: }
34:
35: @Override
36: public Map<String, String> getMessageVariables() {
37: final Map<String, String> messageVariables = CollectionFactoryHolder
38: .getFactory().createMap(2);
39: messageVariables.put("min", Long.toString(min));
40: return messageVariables;
41: }
42:
43: /**
44: * @return the min
45: */
46: public long getMin() {
47: return min;
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: if (value instanceof Number) {
57: if (value instanceof Float || value instanceof Double) {
58: final double doubleValue = ((Number) value)
59: .doubleValue();
60: return doubleValue >= min;
61: }
62: final long longValue = ((Number) value).longValue();
63: return longValue >= min;
64: }
65:
66: final String stringValue = value.toString();
67: try {
68: final double doubleValue = Double.parseDouble(stringValue);
69: return doubleValue >= min;
70: } catch (NumberFormatException e) {
71: return false;
72: }
73: }
74:
75: /**
76: * @param min the min to set
77: */
78: public void setMin(final long min) {
79: this.min = min;
80: }
81: }
|