001: /*
002: * $Id: IntegerValidator.java 459710 2006-03-09 08:57:47Z jonl $
003: * $Revision: 459710 $ $Date: 2006-03-09 09:57:47 +0100 (Thu, 09 Mar 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.markup.html.form.validation;
019:
020: import java.util.Map;
021:
022: import wicket.markup.html.form.FormComponent;
023: import wicket.util.string.Strings;
024:
025: /**
026: * Ensures that the form component has a numeric value in a given range. The
027: * range static factory method constructs a IntegerValidator with minimum and
028: * maximum values specified as Java longs. Convenience fields exist for INT,
029: * POSITIVE_INT, LONG and POSITIVE_LONG which match the appropriate ranges of
030: * numbers.
031: *
032: * @author Jonathan Locke
033: * @deprecated @see {@link NumberValidator}
034: */
035: public class IntegerValidator extends StringValidator {
036: private static final long serialVersionUID = 1L;
037:
038: /**
039: * Validator that ensures int value.
040: */
041: public static final IntegerValidator INT = new IntegerValidator(
042: Integer.MIN_VALUE, Integer.MAX_VALUE);
043:
044: /**
045: * Validator that ensures long value.
046: */
047: public static final IntegerValidator LONG = new IntegerValidator(
048: Long.MIN_VALUE, Long.MAX_VALUE);
049:
050: /**
051: * Validator that ensures positive int value.
052: */
053: public static final IntegerValidator POSITIVE_INT = new IntegerValidator(
054: 0, Integer.MAX_VALUE);
055:
056: /**
057: * Validator that ensures positive long value.
058: */
059: public static final IntegerValidator POSITIVE_LONG = new IntegerValidator(
060: 0, Long.MAX_VALUE);
061:
062: /** Upper bound on valid decimal number. */
063: private final long max;
064:
065: /** Lower bound on valid decimal number. */
066: private final long min;
067:
068: /**
069: * Gets a decimal validator with a given range.
070: *
071: * @param min
072: * Lower bound on valid decimal number
073: * @param max
074: * Upper bound on valid decimal number
075: * @return Validator object
076: */
077: public final static IntegerValidator range(final long min,
078: final long max) {
079: return new IntegerValidator(min, max);
080: }
081:
082: /**
083: * Protected constructor forces use of static factory method and static
084: * instances. Or override it to implement resourceKey(Component)
085: *
086: * @param min
087: * Lower bound on valid decimal number
088: * @param max
089: * Upper bound on valid decimal number
090: */
091: protected IntegerValidator(final long min, final long max) {
092: this .min = min;
093: this .max = max;
094: }
095:
096: /**
097: * Gets the upper bound on valid length.
098: *
099: * @return the upper bound on valid length
100: */
101: public final long getMax() {
102: return max;
103: }
104:
105: /**
106: * Gets the lower bound on valid length.
107: *
108: * @return the lower bound on valid length
109: */
110: public final long getMin() {
111: return min;
112: }
113:
114: /**
115: * Validates the given form component. Ensures that the form component has a
116: * numeric value. If min and max arguments are given, this validator also
117: * ensures the value is in bounds.
118: * @see wicket.markup.html.form.validation.StringValidator#onValidate(wicket.markup.html.form.FormComponent, java.lang.String)
119: */
120: public final void onValidate(FormComponent formComponent,
121: String value) {
122: // If value is non-empty
123: if (!Strings.isEmpty(value)) {
124: try {
125: // Get long value
126: final long longValue = Long.parseLong(value);
127:
128: // Check range
129: if (longValue < min || longValue > max) {
130: error(formComponent);
131: }
132: } catch (NumberFormatException e) {
133: error(formComponent);
134: }
135: }
136: }
137:
138: /**
139: * @see wicket.markup.html.form.validation.AbstractValidator#messageModel(wicket.markup.html.form.FormComponent)
140: */
141: protected Map messageModel(FormComponent formComponent) {
142: final Map map = super .messageModel(formComponent);
143: map.put("min", new Long(min));
144: map.put("max", new Long(max));
145: return map;
146: }
147:
148: /**
149: * @see java.lang.Object#toString()
150: */
151: public String toString() {
152: return "[IntegerValidator min = " + min + ", max = " + max
153: + "]";
154: }
155: }
|