01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.validator;
16:
17: import org.apache.commons.validator.GenericValidator;
18:
19: /**
20: * Validator which ensures that value is within a given range. Uses Commons Validator
21: * <code>GenericValidator.isInRange()</code>. Uses converted value, so must be used in conjuction
22: * with a <code>Converter</code>
23: *
24: * @author Phil Zoio
25: */
26: public class IntegerRangeValidator extends IntegerValidator implements
27: Validator<Integer> {
28:
29: private int min = Integer.MIN_VALUE;
30:
31: private int max = Integer.MAX_VALUE;
32:
33: public IntegerRangeValidator() {
34: super ();
35: }
36:
37: /**
38: * Uses <code>GenericValidator.isInRange()</code> to determine whether value is within given
39: * range. If not required and no value specified, then returns true
40: *
41: */
42: public boolean validate(Integer value) {
43: boolean ok = super .validate(value);
44:
45: if (!ok)
46: return false;
47: return GenericValidator.isInRange(value.intValue(), min, max);
48: }
49:
50: /**
51: * Sets the maximum value in range. Defaults to <code>Integer.MAX_VALUE</code>
52: */
53: public void setMax(int max) {
54: this .max = max;
55: }
56:
57: /**
58: * Sets the minimum value in range. Defaults to <code>Integer.MIN_VALUE</code>
59: */
60: public void setMin(int min) {
61: this .min = min;
62: }
63:
64: public int getMax() {
65: return max;
66: }
67:
68: public int getMin() {
69: return min;
70: }
71:
72: }
|