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 net.sf.oval.Validator;
15: import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
16: import net.sf.oval.context.OValContext;
17:
18: /**
19: * @author Sebastian Thomschke
20: */
21: public class NotNegativeCheck extends
22: AbstractAnnotationCheck<NotNegative> {
23: private static final long serialVersionUID = 1L;
24:
25: public boolean isSatisfied(final Object validatedObject,
26: final Object value, final OValContext context,
27: final Validator validator) {
28: if (value == null)
29: return true;
30:
31: if (value instanceof Number) {
32: if (value instanceof Float || value instanceof Double) {
33: return ((Number) value).doubleValue() >= 0;
34: }
35: return ((Number) value).longValue() >= 0;
36: }
37:
38: final String stringValue = value.toString();
39: try {
40: return Double.parseDouble(stringValue) >= 0;
41: } catch (NumberFormatException e) {
42: return false;
43: }
44: }
45: }
|