001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.validator.routines;
018:
019: import java.text.DecimalFormatSymbols;
020: import java.text.Format;
021: import java.text.NumberFormat;
022: import java.text.DecimalFormat;
023: import java.util.Locale;
024:
025: /**
026: * <p>Abstract class for Number Validation.</p>
027: *
028: * <p>This is a <i>base</i> class for building Number
029: * Validators using format parsing.</p>
030: *
031: * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
032: * @since Validator 1.3.0
033: */
034: public abstract class AbstractNumberValidator extends
035: AbstractFormatValidator {
036:
037: /** Standard <code>NumberFormat</code> type */
038: public static final int STANDARD_FORMAT = 0;
039:
040: /** Currency <code>NumberFormat</code> type */
041: public static final int CURRENCY_FORMAT = 1;
042:
043: /** Percent <code>NumberFormat</code> type */
044: public static final int PERCENT_FORMAT = 2;
045:
046: private boolean allowFractions;
047: private int formatType;
048:
049: /**
050: * Construct an instance with specified <i>strict</i>
051: * and <i>decimal</i> parameters.
052: *
053: * @param strict <code>true</code> if strict
054: * <code>Format</code> parsing should be used.
055: * @param formatType The <code>NumberFormat</code> type to
056: * create for validation, default is STANDARD_FORMAT.
057: * @param allowFractions <code>true</code> if fractions are
058: * allowed or <code>false</code> if integers only.
059: */
060: public AbstractNumberValidator(boolean strict, int formatType,
061: boolean allowFractions) {
062: super (strict);
063: this .allowFractions = allowFractions;
064: this .formatType = formatType;
065: }
066:
067: /**
068: * <p>Indicates whether the number being validated is
069: * a decimal or integer.</p>
070: *
071: * @return <code>true</code> if decimals are allowed
072: * or <code>false</code> if the number is an integer.
073: */
074: public boolean isAllowFractions() {
075: return allowFractions;
076: }
077:
078: /**
079: * <p>Indicates the type of <code>NumberFormat</code> created
080: * by this validator instance.</p>
081: *
082: * @return the format type created.
083: */
084: public int getFormatType() {
085: return formatType;
086: }
087:
088: /**
089: * <p>Validate using the specified <code>Locale</code>.</p>
090: *
091: * @param value The value validation is being performed on.
092: * @param pattern The pattern used to validate the value against, or the
093: * default for the <code>Locale</code> if <code>null</code>.
094: * @param locale The locale to use for the date format, system default if null.
095: * @return <code>true</code> if the value is valid.
096: */
097: public boolean isValid(String value, String pattern, Locale locale) {
098: Object parsedValue = parse(value, pattern, locale);
099: return (parsedValue == null ? false : true);
100: }
101:
102: /**
103: * Check if the value is within a specified range.
104: *
105: * @param value The value validation is being performed on.
106: * @param min The minimum value of the range.
107: * @param max The maximum value of the range.
108: * @return <code>true</code> if the value is within the
109: * specified range.
110: */
111: public boolean isInRange(Number value, Number min, Number max) {
112: return (minValue(value, min) && maxValue(value, max));
113: }
114:
115: /**
116: * Check if the value is greater than or equal to a minimum.
117: *
118: * @param value The value validation is being performed on.
119: * @param min The minimum value.
120: * @return <code>true</code> if the value is greater than
121: * or equal to the minimum.
122: */
123: public boolean minValue(Number value, Number min) {
124: if (isAllowFractions()) {
125: return (value.doubleValue() >= min.doubleValue());
126: } else {
127: return (value.longValue() >= min.longValue());
128: }
129: }
130:
131: /**
132: * Check if the value is less than or equal to a maximum.
133: *
134: * @param value The value validation is being performed on.
135: * @param max The maximum value.
136: * @return <code>true</code> if the value is less than
137: * or equal to the maximum.
138: */
139: public boolean maxValue(Number value, Number max) {
140: if (isAllowFractions()) {
141: return (value.doubleValue() <= max.doubleValue());
142: } else {
143: return (value.longValue() <= max.longValue());
144: }
145: }
146:
147: /**
148: * <p>Parse the value using the specified pattern.</p>
149: *
150: * @param value The value validation is being performed on.
151: * @param pattern The pattern used to validate the value against, or the
152: * default for the <code>Locale</code> if <code>null</code>.
153: * @param locale The locale to use for the date format, system default if null.
154: * @return The parsed value if valid or <code>null</code> if invalid.
155: */
156: protected Object parse(String value, String pattern, Locale locale) {
157:
158: value = (value == null ? null : value.trim());
159: if (value == null || value.length() == 0) {
160: return null;
161: }
162: Format formatter = getFormat(pattern, locale);
163: return parse(value, formatter);
164:
165: }
166:
167: /**
168: * <p>Process the parsed value, performing any further validation
169: * and type conversion required.</p>
170: *
171: * @param value The parsed object created.
172: * @param formatter The Format used to parse the value with.
173: * @return The parsed value converted to the appropriate type
174: * if valid or <code>null</code> if invalid.
175: */
176: protected abstract Object processParsedValue(Object value,
177: Format formatter);
178:
179: /**
180: * <p>Returns a <code>NumberFormat</code> for the specified <i>pattern</i>
181: * and/or <code>Locale</code>.</p>
182: *
183: * @param pattern The pattern used to validate the value against or
184: * <code>null</code> to use the default for the <code>Locale</code>.
185: * @param locale The locale to use for the currency format, system default if null.
186: * @return The <code>NumberFormat</code> to created.
187: */
188: protected Format getFormat(String pattern, Locale locale) {
189:
190: NumberFormat formatter = null;
191: boolean usePattern = (pattern != null && pattern.length() > 0);
192: if (!usePattern) {
193: formatter = (NumberFormat) getFormat(locale);
194: } else if (locale == null) {
195: formatter = new DecimalFormat(pattern);
196: } else {
197: DecimalFormatSymbols symbols = new DecimalFormatSymbols(
198: locale);
199: formatter = new DecimalFormat(pattern, symbols);
200: }
201:
202: if (determineScale(formatter) == 0) {
203: formatter.setParseIntegerOnly(true);
204: }
205: return formatter;
206: }
207:
208: /**
209: * <p>Returns the <i>multiplier</i> of the <code>NumberFormat</code>.</p>
210: *
211: * @param format The <code>NumberFormat</code> to determine the
212: * multiplier of.
213: * @return The multiplying factor for the format..
214: */
215: protected int determineScale(NumberFormat format) {
216: if (!isStrict()) {
217: return -1;
218: }
219: if (!isAllowFractions() || format.isParseIntegerOnly()) {
220: return 0;
221: }
222: int minimumFraction = format.getMinimumFractionDigits();
223: int maximumFraction = format.getMaximumFractionDigits();
224: if (minimumFraction != maximumFraction) {
225: return -1;
226: }
227: int scale = minimumFraction;
228: if (format instanceof DecimalFormat) {
229: int multiplier = ((DecimalFormat) format).getMultiplier();
230: if (multiplier == 100) {
231: scale += 2;
232: } else if (multiplier == 1000) {
233: scale += 3;
234: }
235: } else if (formatType == PERCENT_FORMAT) {
236: scale += 2;
237: }
238: return scale;
239: }
240:
241: /**
242: * <p>Returns a <code>NumberFormat</code> for the specified Locale.</p>
243: *
244: * @param locale The locale a <code>NumberFormat</code> is required for,
245: * system default if null.
246: * @return The <code>NumberFormat</code> to created.
247: */
248: protected Format getFormat(Locale locale) {
249: NumberFormat formatter = null;
250: switch (formatType) {
251: case CURRENCY_FORMAT:
252: if (locale == null) {
253: formatter = NumberFormat.getCurrencyInstance();
254: } else {
255: formatter = NumberFormat.getCurrencyInstance(locale);
256: }
257: break;
258: case PERCENT_FORMAT:
259: if (locale == null) {
260: formatter = NumberFormat.getPercentInstance();
261: } else {
262: formatter = NumberFormat.getPercentInstance(locale);
263: }
264: break;
265: default:
266: if (locale == null) {
267: formatter = NumberFormat.getInstance();
268: } else {
269: formatter = NumberFormat.getInstance(locale);
270: }
271: break;
272: }
273: return formatter;
274: }
275: }
|