001: /*
002: * Copyright 2007 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.validators;
017:
018: import org.iscreen.BaseValidator;
019: import org.iscreen.ConfigurationException;
020: import org.iscreen.FailureMessage;
021: import org.iscreen.SimpleBean;
022: import org.iscreen.ValidatorContext;
023:
024: /**
025: * This validator verifies that a passed in java.lang.Number is within
026: * a certain range. <br />
027: * <br />
028: * This validator takes two constraints:<br />
029: * minimumValue - The smallest number the value can be. <br />
030: * maximumValue - The largest number the value can be (-1 means infinite). <br />
031: * <br />
032: * This validator takes two failure messages:<br />
033: * rangeFailure - Failure when the value it outside the range. <br />
034: * invalidNumberFailure - Failure when the value is not a java.lang.Number. <br />
035: *
036: * @author Shellman, Dan
037: */
038: public class NumberRangeValidator extends BaseValidator {
039: private FailureMessage rangeFailure;
040: private FailureMessage invalidNumberFailure;
041: private int max = Integer.MAX_VALUE;
042: private int min;
043:
044: /**
045: * Default constructor.
046: */
047: public NumberRangeValidator() {
048: } //end NumberRangeValidator()
049:
050: public void validate(ValidatorContext context,
051: Object objectToValidate) {
052: int numberToValidate;
053:
054: try {
055: numberToValidate = getNumber(objectToValidate);
056: } catch (ConfigurationException e) {
057: context.reportFailure(invalidNumberFailure);
058: return;
059: }
060:
061: if ((max != -1 && numberToValidate > max)
062: || numberToValidate < min) {
063: context.reportFailure(rangeFailure, new Integer(
064: numberToValidate));
065: }
066: } //end validate()
067:
068: public void setMinimumValue(Integer minValue) {
069: min = minValue.intValue();
070: } //end setMinimumValue()
071:
072: public Integer getMinimumValue() {
073: return new Integer(min);
074: } //end getMinimumValue()
075:
076: public void setMaximumValue(Integer maxValue) {
077: max = maxValue.intValue();
078: } //end setMaximumValue()
079:
080: public Integer getMaximumValue() {
081: return new Integer(max);
082: } //end getMaximumValue()
083:
084: public void setRangeFailure(FailureMessage message) {
085: rangeFailure = message;
086: setDefaultFailure(message);
087: } //end setRangeFailure()
088:
089: public void setInvalidNumberFailure(FailureMessage message) {
090: invalidNumberFailure = message;
091: } //end setInvalidNumberFailure()
092:
093: // *****
094: // Protected methods
095: // *****
096:
097: protected int getNumber(Object obj) {
098: Object value = ((SimpleBean) obj).getValue();
099:
100: if (value instanceof Number) {
101: return ((Number) value).intValue();
102: } else if (value instanceof String) {
103: try {
104: return Integer.parseInt((String) value);
105: } catch (NumberFormatException e) {
106: }
107: }
108:
109: throw new ConfigurationException(
110: "NumberRangeValidator: Object is not a number.");
111: } //end getNumber()
112: } //end NumberRangeValidator
|