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 MaxCheck extends AbstractAnnotationCheck<Max> {
25: private static final long serialVersionUID = 1L;
26:
27: private long max;
28:
29: @Override
30: public void configure(final Max constraintAnnotation) {
31: super .configure(constraintAnnotation);
32: setMax(constraintAnnotation.value());
33: }
34:
35: /**
36: * @return the max
37: */
38: public long 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", Long.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: if (value instanceof Number) {
57: if (value instanceof Float || value instanceof Double) {
58: final double doubleValue = ((Number) value)
59: .doubleValue();
60: return doubleValue <= max;
61: }
62: final long longValue = ((Number) value).longValue();
63: return longValue <= max;
64: }
65:
66: final String stringValue = value.toString();
67: try {
68: final double doubleValue = Double.parseDouble(stringValue);
69: return doubleValue <= max;
70: } catch (NumberFormatException e) {
71: return false;
72: }
73: }
74:
75: /**
76: * @param max the max to set
77: */
78: public void setMax(final long max) {
79: this.max = max;
80: }
81: }
|