001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
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 de.odysseus.calyxo.forms.check;
017:
018: import de.odysseus.calyxo.base.util.NumberUtils;
019:
020: /**
021: * Check a number to be in some interval.
022: * The interval borders may be open depending on the
023: * <code>allowMin</code> and <code>allowMax</code> properties.
024: * Checking <code>null</code> succeeds.
025: *
026: * @author Christoph Beck
027: */
028: public class RangeChecker extends SimpleChecker {
029: private Double min;
030: private Double max;
031: private boolean allowMin;
032: private boolean allowMax;
033:
034: /**
035: * Constructor
036: */
037: public RangeChecker() {
038: this (Double.NEGATIVE_INFINITY, false, Double.POSITIVE_INFINITY,
039: false);
040: }
041:
042: /**
043: * Constructor
044: */
045: public RangeChecker(double min, boolean allowMin, double max,
046: boolean allowMax) {
047: super ();
048:
049: setMin(min);
050: setAllowMin(allowMin);
051: setMax(max);
052: setAllowMax(allowMax);
053: }
054:
055: /**
056: * Check object to be in receiver's interval.
057: */
058: public boolean check(Object value) {
059: if (value != null) {
060: Number number = (Number) value;
061: if (NumberUtils.isNaN(number))
062: return false;
063: int compare = NumberUtils.compare(min, number);
064: if (compare > 0 || !allowMin && compare == 0)
065: return false;
066: compare = NumberUtils.compare(number, max);
067: if (compare > 0 || !allowMax && compare == 0)
068: return false;
069: }
070: return true;
071: }
072:
073: /**
074: * Get max property
075: */
076: public double getMax() {
077: return max.doubleValue();
078: }
079:
080: /**
081: * Get min property
082: */
083: public double getMin() {
084: return min.doubleValue();
085: }
086:
087: /**
088: * Get allowMax property
089: */
090: public boolean isAllowMax() {
091: return allowMax;
092: }
093:
094: /**
095: * Get allowMin property
096: */
097: public boolean isAllowMin() {
098: return allowMin;
099: }
100:
101: /**
102: * Set allowMax property
103: */
104: public void setAllowMax(boolean newAllowMax) {
105: allowMax = newAllowMax;
106: }
107:
108: /**
109: * Set allowMin property
110: */
111: public void setAllowMin(boolean newAllowMin) {
112: allowMin = newAllowMin;
113: }
114:
115: /**
116: * Set max property
117: */
118: public void setMax(double newMax) {
119: max = new Double(newMax);
120: }
121:
122: /**
123: * Set min property
124: */
125: public void setMin(double newMin) {
126: min = new Double(newMin);
127: }
128:
129: protected String paramsToString() {
130: return ", " + min + (allowMin ? "<=" : "<") + " value "
131: + (allowMax ? "<=" : "<") + max;
132: }
133:
134: /**
135: * This checker is thread-save, so answer <code>true</code>
136: */
137: public boolean isSharable() {
138: return true;
139: }
140: }
|