You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2006/01/22 04:25:12 UTC
svn commit: r371174 [1/3] - in /jakarta/commons/proper/validator/trunk: ./
src/share/org/apache/commons/validator/routines/
src/test/org/apache/commons/validator/routines/ xdocs/
Author: niallp
Date: Sat Jan 21 19:24:40 2006
New Revision: 371174
URL: http://svn.apache.org/viewcvs?rev=371174&view=rev
Log:
Refactor "routines" package to provide localized pattern support, provide currency and percent support for all numeric types and improve JUnit test cases.
Modified:
jakarta/commons/proper/validator/trunk/build.xml
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractCalendarValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractFormatValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractNumberValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigDecimalValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigIntegerValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ByteValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CalendarValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CurrencyValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DateValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DoubleValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/FloatValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/IntegerValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/LongValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/PercentValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ShortValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/TimeValidator.java
jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/package.html
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/BaseCalendarValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/BaseNumberValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/BigDecimalValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/BigIntegerValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ByteValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/CalendarValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/CurrencyValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/DateValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/DoubleValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/FloatValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/IntegerValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/LongValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/PercentValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/RoutinesTestSuite.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/ShortValidatorTest.java
jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/TimeValidatorTest.java
jakarta/commons/proper/validator/trunk/xdocs/changes.xml
Modified: jakarta/commons/proper/validator/trunk/build.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/build.xml?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/build.xml (original)
+++ jakarta/commons/proper/validator/trunk/build.xml Sat Jan 21 19:24:40 2006
@@ -329,9 +329,13 @@
</target>
- <target name="test" depends="compile.tests"
+ <target name="test" depends="compile.tests,test.main,test.routines"
description="Run all unit test cases">
- <echo message="Running tests ..."/>
+ </target>
+
+ <target name="test.main" depends="compile.tests"
+ description="Run main unit test cases">
+ <echo message="Running main tests ..."/>
<java classname="${test.runner}" fork="yes"
failonerror="${test.failonerror}">
<arg value="org.apache.commons.validator.ValidatorTestSuite"/>
@@ -339,6 +343,15 @@
</java>
</target>
+ <target name="test.routines" depends="compile.tests"
+ description="Run routines unit test cases">
+ <echo message="Running routines tests ..."/>
+ <java classname="${test.runner}" fork="yes"
+ failonerror="${test.failonerror}">
+ <arg value="org.apache.commons.validator.routines.RoutinesTestSuite"/>
+ <classpath refid="test.classpath"/>
+ </java>
+ </target>
<!-- ========== Example Targets =========================================== -->
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractCalendarValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractCalendarValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractCalendarValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractCalendarValidator.java Sat Jan 21 19:24:40 2006
@@ -20,6 +20,7 @@
*/
package org.apache.commons.validator.routines;
+import java.text.DateFormatSymbols;
import java.text.Format;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -43,14 +44,6 @@
private int timeStyle = -1;
/**
- * Construct a <i>strict</i> instance with <i>short</i>
- * date style.
- */
- public AbstractCalendarValidator() {
- this(true, DateFormat.SHORT, -1);
- }
-
- /**
* Construct an instance with the specified <i>strict</i>,
* <i>time</i> and <i>date</i> style parameters.
*
@@ -66,113 +59,126 @@
}
/**
- * <p>Creates a <code>SimpleDateFormat</code> for the specified
- * pattern.</p>
+ * <p>Validate using the specified <code>Locale</code>.
*
- * <p>If no pattern is specified the default Locale is used
- * to determine the <code>DateFormat</code>.
- *
- * @param pattern The pattern of the required the <code>DateFormat</code>.
- * @return The <code>DateFormat</code> to created.
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to format the value.
+ * @param locale The locale to use for the Format, defaults to the default
+ * @return <code>true</code> if the value is valid.
*/
- protected Format getFormat(String pattern) {
-
- DateFormat formatter = null;
- if (pattern == null || pattern.length() == 0) {
- formatter = (DateFormat)getFormat(Locale.getDefault());
- } else {
- formatter = new SimpleDateFormat(pattern);
- }
- formatter.setLenient(false);
- return formatter;
-
+ public boolean isValid(String value, String pattern, Locale locale) {
+ Object parsedValue = parse(value, pattern, locale, (TimeZone)null);
+ return (parsedValue == null ? false : true);
}
/**
- * <p>Returns a <code>DateFormat</code> for the specified Locale.</p>
- *
- * @param locale The locale a <code>DateFormat</code> is required for,
- * system default if null.
- * @return The <code>DateFormat</code> to created.
+ * <p>Format an object into a <code>String</code> using
+ * the default Locale.</p>
+ *
+ * @param value The value validation is being performed on.
+ * @param timeZone The Time Zone used to format the date,
+ * system default if null (unless value is a <code>Calendar</code>.
+ * @return The value formatted as a <code>String</code>.
*/
- protected Format getFormat(Locale locale) {
-
- if (locale == null) {
- locale = Locale.getDefault();
- }
-
- DateFormat formatter = null;
- if (dateStyle >= 0 && timeStyle >= 0) {
- formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
- } else if (timeStyle >= 0) {
- formatter = DateFormat.getTimeInstance(timeStyle, locale);
- } else if (dateStyle >= 0) {
- formatter = DateFormat.getDateInstance(dateStyle, locale);
- } else {
- formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale);
- }
- formatter.setLenient(false);
- return formatter;
+ public String format(Object value, TimeZone timeZone) {
+ return format(value, (String)null, (Locale)null, timeZone);
+ }
+ /**
+ * <p>Format an object into a <code>String</code> using
+ * the specified pattern.</p>
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to format the value.
+ * @param timeZone The Time Zone used to format the date,
+ * system default if null (unless value is a <code>Calendar</code>.
+ * @return The value formatted as a <code>String</code>.
+ */
+ public String format(Object value, String pattern, TimeZone timeZone) {
+ return format(value, pattern, (Locale)null, timeZone);
}
/**
- * <p>Checks if the value is valid against a specified pattern.</p>
+ * <p>Format an object into a <code>String</code> using
+ * the specified Locale.</p>
*
* @param value The value validation is being performed on.
- * @param pattern The pattern used to validate the value against.
- * @return The parsed value if valid or <code>null</code> if invalid.
+ * @param locale The locale to use for the Format.
+ * @param timeZone The Time Zone used to format the date,
+ * system default if null (unless value is a <code>Calendar</code>.
+ * @return The value formatted as a <code>String</code>.
*/
- protected Object validateObj(String value, String pattern) {
- return validateObj(value, pattern, (TimeZone)null);
+ public String format(Object value, Locale locale, TimeZone timeZone) {
+ return format(value, (String)null, locale, timeZone);
}
/**
- * <p>Checks if the value is valid against a specified pattern.</p>
+ * <p>Format an object using the specified pattern and/or
+ * <code>Locale</code>.
*
* @param value The value validation is being performed on.
- * @param pattern The pattern used to validate the value against.
- * @param timeZone The Time Zone used to parse the date, system default if null.
- * @return The parsed value if valid or <code>null</code> if invalid.
+ * @param pattern The pattern used to format the value.
+ * @param locale The locale to use for the Format.
+ * @return The value formatted as a <code>String</code>.
*/
- protected Object validateObj(String value, String pattern, TimeZone timeZone) {
+ public String format(Object value, String pattern, Locale locale) {
+ return format(value, pattern, locale, (TimeZone)null);
+ }
- if (value == null || value.length() == 0) {
- return null;
- }
- DateFormat formatter = (DateFormat)getFormat(pattern);
+ /**
+ * <p>Format an object using the specified pattern and/or
+ * <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to format the value.
+ * @param locale The locale to use for the Format.
+ * @param timeZone The Time Zone used to format the date,
+ * system default if null (unless value is a <code>Calendar</code>.
+ * @return The value formatted as a <code>String</code>.
+ */
+ public String format(Object value, String pattern, Locale locale, TimeZone timeZone) {
+ DateFormat formatter = (DateFormat)getFormat(pattern, locale);
if (timeZone != null) {
formatter.setTimeZone(timeZone);
+ } else if (value instanceof Calendar) {
+ formatter.setTimeZone(((Calendar)value).getTimeZone());
}
- return parse(value, formatter);
-
+ return format(value, formatter);
}
/**
- * <p>Checks if the value is valid for a specified <code>Locale</code>.</p>
- *
- * @param value The value validation is being performed on.
- * @param locale The locale to use for the date format, system default if null.
- * @return The parsed value if valid or <code>null</code> if invalid.
+ * <p>Format a value with the specified <code>DateFormat</code>.</p>
+ *
+ * @param value The value to be formatted.
+ * @param formatter The Format to use.
+ * @return The formatted value.
*/
- protected Object validateObj(String value, Locale locale) {
- return validateObj(value, locale, (TimeZone)null);
+ protected String format(Object value, Format formatter) {
+ if (value == null) {
+ return null;
+ } else if (value instanceof Calendar) {
+ value = ((Calendar)value).getTime();
+ }
+ return formatter.format(value);
}
/**
- * <p>Checks if the value is valid for a specified <code>Locale</code>.</p>
+ * <p>Checks if the value is valid against a specified pattern.</p>
*
* @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
* @param locale The locale to use for the date format, system default if null.
* @param timeZone The Time Zone used to parse the date, system default if null.
* @return The parsed value if valid or <code>null</code> if invalid.
*/
- protected Object validateObj(String value, Locale locale, TimeZone timeZone) {
+ protected Object parse(String value, String pattern, Locale locale, TimeZone timeZone) {
+ value = (value == null ? null : value.trim());
if (value == null || value.length() == 0) {
return null;
}
- DateFormat formatter = (DateFormat)getFormat(locale);
+ DateFormat formatter = (DateFormat)getFormat(pattern, locale);
if (timeZone != null) {
formatter.setTimeZone(timeZone);
}
@@ -181,47 +187,73 @@
}
/**
- * <p>Parse the value with the specified <code>Format</code>.</p>
+ * <p>Process the parsed value, performing any further validation
+ * and type conversion required.</p>
*
- * @param value The value to be parsed.
- * @param formatter The Format to parse the value with.
- * @return The parsed value if valid or <code>null</code> if invalid.
+ * @param value The parsed object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed value converted to the appropriate type
+ * if valid or <code>null</code> if invalid.
*/
- protected Object parse(String value, Format formatter) {
-
- Object parsedValue = super.parse(value, formatter);
- if (parsedValue == null) {
- return null;
- }
-
- // Process the Calendar, not the parsed Date object
- Calendar calendar = ((DateFormat)formatter).getCalendar();
- return processCalendar(calendar);
-
- }
+ protected abstract Object processParsedValue(Object value, Format formatter);
/**
- * <p>Perform further validation and convert the <code>Calendar</code> to
- * the appropriate type.</p>
+ * <p>Returns a <code>DateFormat</code> for the specified <i>pattern</i>
+ * and/or <code>Locale</code>.</p>
*
- * @param calendar The calendar object create from the parsed value.
- * @return The validated/converted <code>Calendar</code> value if valid
- * or <code>null</code> if invalid.
+ * @param pattern The pattern used to validate the value against or
+ * <code>null</code> to use the default for the <code>Locale</code>.
+ * @param locale The locale to use for the currency format, system default if null.
+ * @return The <code>DateFormat</code> to created.
*/
- protected abstract Object processCalendar(Calendar calendar);
+ protected Format getFormat(String pattern, Locale locale) {
+ DateFormat formatter = null;
+ boolean usePattern = (pattern != null && pattern.length() > 0);
+ if (!usePattern) {
+ formatter = (DateFormat)getFormat(locale);
+ } else if (locale == null) {
+ formatter = new SimpleDateFormat(pattern);
+ } else {
+ DateFormatSymbols symbols = new DateFormatSymbols(locale);
+ formatter = new SimpleDateFormat(pattern, symbols);
+ }
+ formatter.setLenient(false);
+ return formatter;
+ }
/**
- * <p>Format a value with the specified <code>DateFormat</code>.</p>
+ * <p>Returns a <code>DateFormat</code> for the specified Locale.</p>
*
- * @param value The value to be formatted.
- * @param formatter The Format to use.
- * @return The formatted value.
+ * @param locale The locale a <code>DateFormat</code> is required for,
+ * system default if null.
+ * @return The <code>DateFormat</code> to created.
*/
- protected String format(Object value, Format formatter) {
- if (value instanceof Calendar) {
- value = ((Calendar)value).getTime();
+ protected Format getFormat(Locale locale) {
+
+ DateFormat formatter = null;
+ if (dateStyle >= 0 && timeStyle >= 0) {
+ if (locale == null) {
+ formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
+ } else {
+ formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
+ }
+ } else if (timeStyle >= 0) {
+ if (locale == null) {
+ formatter = DateFormat.getTimeInstance(timeStyle);
+ } else {
+ formatter = DateFormat.getTimeInstance(timeStyle, locale);
+ }
+ } else {
+ int useDateStyle = dateStyle >= 0 ? dateStyle : DateFormat.SHORT;
+ if (locale == null) {
+ formatter = DateFormat.getDateInstance(useDateStyle);
+ } else {
+ formatter = DateFormat.getDateInstance(useDateStyle, locale);
+ }
}
- return formatter.format(value);
+ formatter.setLenient(false);
+ return formatter;
+
}
/**
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractFormatValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractFormatValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractFormatValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractFormatValidator.java Sat Jan 21 19:24:40 2006
@@ -76,8 +76,7 @@
* @return <code>true</code> if the value is valid.
*/
public boolean isValid(String value) {
- Object parsedValue = validateObj(value);
- return (parsedValue == null ? false : true);
+ return isValid(value, (String)null, (Locale)null);
}
/**
@@ -88,8 +87,7 @@
* @return <code>true</code> if the value is valid.
*/
public boolean isValid(String value, String pattern) {
- Object parsedValue = validateObj(value, pattern);
- return (parsedValue == null ? false : true);
+ return isValid(value, pattern, (Locale)null);
}
/**
@@ -100,11 +98,20 @@
* @return <code>true</code> if the value is valid.
*/
public boolean isValid(String value, Locale locale) {
- Object parsedValue = validateObj(value, locale);
- return (parsedValue == null ? false : true);
+ return isValid(value, (String)null, locale);
}
/**
+ * <p>Validate using the specified pattern and/or <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to format the value.
+ * @param locale The locale to use for the Format, defaults to the default
+ * @return <code>true</code> if the value is valid.
+ */
+ public abstract boolean isValid(String value, String pattern, Locale locale);
+
+ /**
* <p>Format an object into a <code>String</code> using
* the default Locale.</p>
*
@@ -112,7 +119,7 @@
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value) {
- return format(value, Locale.getDefault());
+ return format(value, (String)null, (Locale)null);
}
/**
@@ -124,8 +131,7 @@
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, String pattern) {
- Format formatter = getFormat(pattern);
- return format(value, formatter);
+ return format(value, pattern, (Locale)null);
}
/**
@@ -137,7 +143,20 @@
* @return The value formatted as a <code>String</code>.
*/
public String format(Object value, Locale locale) {
- Format formatter = getFormat(locale);
+ return format(value, (String)null, locale);
+ }
+
+ /**
+ * <p>Format an object using the specified pattern and/or
+ * <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to format the value.
+ * @param locale The locale to use for the Format.
+ * @return The value formatted as a <code>String</code>.
+ */
+ public String format(Object value, String pattern, Locale locale) {
+ Format formatter = getFormat(pattern, locale);
return format(value, formatter);
}
@@ -153,51 +172,6 @@
}
/**
- * <p>Checks if the value is valid using the default Locale.</p>
- *
- * @param value The value validation is being performed on.
- * @return The processed value if valid or <code>null</code> if invalid.
- */
- protected Object validateObj(String value) {
- return validateObj(value, Locale.getDefault());
- }
-
- /**
- * <p>Checks if the value is valid against a specified pattern.</p>
- *
- * @param value The value validation is being performed on.
- * @param pattern The pattern used to validate the value against.
- * @return The parsed value if valid or <code>null</code> if invalid.
- */
- protected Object validateObj(String value, String pattern) {
-
- if (value == null || value.length() == 0) {
- return null;
- }
- Format formatter = getFormat(pattern);
- return parse(value, formatter);
-
- }
-
- /**
- * <p>Checks if the value is valid for a specified <code>Locale</code>.</p>
- *
- * @param value The value validation is being performed on.
- * @param locale The locale to use for the date format, defaults to the default
- * system default if null.
- * @return The parsed value if valid or <code>null</code> if invalid.
- */
- protected Object validateObj(String value, Locale locale) {
-
- if (value == null || value.length() == 0) {
- return null;
- }
- Format formatter = getFormat(locale);
- return parse(value, formatter);
-
- }
-
- /**
* <p>Parse the value with the specified <code>Format</code>.</p>
*
* @param value The value to be parsed.
@@ -206,41 +180,44 @@
*/
protected Object parse(String value, Format formatter) {
- String tempValue = (value == null ? null : value.trim());
- if (tempValue == null || tempValue.length() == 0) {
- return null;
- }
-
ParsePosition pos = new ParsePosition(0);
- Object parsedValue = formatter.parseObject(tempValue, pos);
-
+ Object parsedValue = formatter.parseObject(value, pos);
if (pos.getErrorIndex() > -1) {
return null;
}
- if (isStrict() && pos.getIndex() < tempValue.length()) {
+ if (isStrict() && pos.getIndex() < value.length()) {
return null;
}
+ if (parsedValue != null) {
+ parsedValue = processParsedValue(parsedValue, formatter);
+ }
+
return parsedValue;
}
/**
- * <p>Returns a <code>Format</code> for the specified pattern.</p>
+ * <p>Process the parsed value, performing any further validation
+ * and type conversion required.</p>
*
- * @param pattern The pattern of the required the <code>Format</code>.
- * @return The <code>Format</code> to created.
+ * @param value The parsed object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed value converted to the appropriate type
+ * if valid or <code>null</code> if invalid.
*/
- protected abstract Format getFormat(String pattern);
+ protected abstract Object processParsedValue(Object value, Format formatter);
/**
- * <p>Returns a <code>Format</code> for the specified Locale.</p>
+ * <p>Returns a <code>Format</code> for the specified <i>pattern</i>
+ * and/or <code>Locale</code>.</p>
*
- * @param locale The locale a <code>Format</code> is required for,
- * defaults to the default
- * @return The <code>Format</code> to created.
+ * @param pattern The pattern used to validate the value against or
+ * <code>null</code> to use the default for the <code>Locale</code>.
+ * @param locale The locale to use for the currency format, system default if null.
+ * @return The <code>NumberFormat</code> to created.
*/
- protected abstract Format getFormat(Locale locale);
+ protected abstract Format getFormat(String pattern, Locale locale);
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractNumberValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractNumberValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractNumberValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/AbstractNumberValidator.java Sat Jan 21 19:24:40 2006
@@ -20,6 +20,7 @@
*/
package org.apache.commons.validator.routines;
+import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.text.NumberFormat;
import java.text.DecimalFormat;
@@ -35,16 +36,18 @@
* @since Validator 1.2.1
*/
public abstract class AbstractNumberValidator extends AbstractFormatValidator {
+
+ /** Standard <code>NumberFormat</code> type */
+ public static final int STANDARD_FORMAT = 0;
- private boolean decimal;
+ /** Currency <code>NumberFormat</code> type */
+ public static final int CURRENCY_FORMAT = 1;
- /**
- * Construct a <i>strict</i> instance for <i>decimal</i>
- * formats.
- */
- public AbstractNumberValidator() {
- this(true, true);
- }
+ /** Percent <code>NumberFormat</code> type */
+ public static final int PERCENT_FORMAT = 2;
+
+ private boolean allowFractions;
+ private int formatType;
/**
* Construct an instance with specified <i>strict</i>
@@ -52,12 +55,50 @@
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
- * @param decimal Set whether this validator handlers decimal
- * values (or only integer numbers).
+ * @param formatType The <code>NumberFormat</code> type to
+ * create for validation, default is STANDARD_FORMAT.
+ * @param allowFractions <code>true</code> if fractions are
+ * allowed or <code>false</code> if integers only.
*/
- public AbstractNumberValidator(boolean strict, boolean decimal) {
+ public AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions) {
super(strict);
- this.decimal = decimal;
+ this.allowFractions = allowFractions;
+ this.formatType = formatType;
+ }
+
+ /**
+ * <p>Indicates whether the number being validated is
+ * a decimal or integer.</p>
+ *
+ * @return <code>true</code> if decimals are allowed
+ * or <code>false</code> if the number is an integer.
+ */
+ public boolean isAllowFractions() {
+ return allowFractions;
+ }
+
+ /**
+ * <p>Indicates the type of <code>NumberFormat</code> created
+ * by this validator instance.</p>
+ *
+ * @return the format type created.
+ */
+ public int getFormatType() {
+ return formatType;
+ }
+
+ /**
+ * <p>Validate using the specified <code>Locale</code>.</p>
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return <code>true</code> if the value is valid.
+ */
+ public boolean isValid(String value, String pattern, Locale locale) {
+ Object parsedValue = parse(value, pattern, locale);
+ return (parsedValue == null ? false : true);
}
/**
@@ -82,7 +123,7 @@
* or equal to the minimum.
*/
public boolean minValue(Number value, Number min) {
- if (decimal) {
+ if (isAllowFractions()) {
return (value.doubleValue() >= min.doubleValue());
} else {
return (value.longValue() >= min.longValue());
@@ -98,7 +139,7 @@
* or equal to the maximum.
*/
public boolean maxValue(Number value, Number max) {
- if (decimal) {
+ if (isAllowFractions()) {
return (value.doubleValue() <= max.doubleValue());
} else {
return (value.longValue() <= max.longValue());
@@ -106,79 +147,129 @@
}
/**
- * <p>Creates a <code>DecimalFormat</code> for the specified
- * pattern.</p>
- *
- * <p>If no pattern is specified the default Locale is used
- * to determine the <code>NumberFormat</code>.
- *
- * @param pattern The pattern of the required the <code>DecimalFormat</code>.
- * @return The <code>NumberFormat</code> to created.
+ * <p>Parse the value using the specified pattern.</p>
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed value if valid or <code>null</code> if invalid.
*/
- protected Format getFormat(String pattern) {
+ protected Object parse(String value, String pattern, Locale locale) {
- NumberFormat formatter = null;
- if (pattern == null || pattern.length() == 0) {
- formatter = (NumberFormat)getFormat(Locale.getDefault());
- } else {
- formatter = new DecimalFormat(pattern);
- }
- if (isStrict() && !decimal) {
- formatter.setParseIntegerOnly(true);
+ value = (value == null ? null : value.trim());
+ if (value == null || value.length() == 0) {
+ return null;
}
- return formatter;
+ Format formatter = getFormat(pattern, locale);
+ return parse(value, formatter);
}
/**
- * <p>Returns a <code>NumberFormat</code> for the specified Locale.</p>
+ * <p>Process the parsed value, performing any further validation
+ * and type conversion required.</p>
+ *
+ * @param value The parsed object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed value converted to the appropriate type
+ * if valid or <code>null</code> if invalid.
+ */
+ protected abstract Object processParsedValue(Object value, Format formatter);
+
+ /**
+ * <p>Returns a <code>NumberFormat</code> for the specified <i>pattern</i>
+ * and/or <code>Locale</code>.</p>
*
- * @param locale The locale a <code>NumberFormat</code> is required
- * for, defaults to the default.
+ * @param pattern The pattern used to validate the value against or
+ * <code>null</code> to use the default for the <code>Locale</code>.
+ * @param locale The locale to use for the currency format, system default if null.
* @return The <code>NumberFormat</code> to created.
*/
- protected Format getFormat(Locale locale) {
+ protected Format getFormat(String pattern, Locale locale) {
- if (locale == null) {
- locale = Locale.getDefault();
+ NumberFormat formatter = null;
+ boolean usePattern = (pattern != null && pattern.length() > 0);
+ if (!usePattern) {
+ formatter = (NumberFormat)getFormat(locale);
+ } else if (locale == null) {
+ formatter = new DecimalFormat(pattern);
+ } else {
+ DecimalFormatSymbols symbols = new DecimalFormatSymbols(locale);
+ formatter = new DecimalFormat(pattern, symbols);
}
- NumberFormat formatter = NumberFormat.getInstance(locale);
- if (isStrict() && !decimal) {
+ if (determineScale(formatter) == 0) {
formatter.setParseIntegerOnly(true);
}
-
return formatter;
-
}
/**
- * <p>Parse the value with the specified <code>NumberFormat</code>.</p>
+ * <p>Returns the <i>multiplier</i> of the <code>NumberFormat</code>.</p>
*
- * @param value The value to be parsed.
- * @param formatter The Format to parse the value with.
- * @return The parsed value if valid or <code>null</code> if invalid.
- */
- protected Object parse(String value, Format formatter) {
-
- Number number = (Number)super.parse(value, formatter);
- if (number == null) {
- return null;
+ * @param format The <code>NumberFormat</code> to determine the
+ * multiplier of.
+ * @return The multiplying factor for the format..
+ */
+ protected int determineScale(NumberFormat format) {
+ if (!isStrict()) {
+ return -1;
+ }
+ if (!isAllowFractions() || format.isParseIntegerOnly()) {
+ return 0;
+ }
+ int minimumFraction = format.getMinimumFractionDigits();
+ int maximumFraction = format.getMaximumFractionDigits();
+ if (minimumFraction != maximumFraction) {
+ return -1;
+ }
+ int scale = minimumFraction;
+ if (format instanceof DecimalFormat) {
+ int multiplier = ((DecimalFormat)format).getMultiplier();
+ if (multiplier == 100) {
+ scale += 2;
+ } else if (multiplier == 1000) {
+ scale += 3;
+ }
+ } else if (formatType == PERCENT_FORMAT) {
+ scale += 2;
}
-
- // Process the parsed Number
- return processNumber(number);
-
+ return scale;
}
/**
- * <p>Perform further validation and convert the <code>Number</code> to
- * the appropriate type.</p>
+ * <p>Returns a <code>NumberFormat</code> for the specified Locale.</p>
*
- * @param number The number object created from the parsed value.
- * @return The validated/converted <code>Number</code> value if valid
- * or <code>null</code> if invalid.
+ * @param locale The locale a <code>NumberFormat</code> is required for,
+ * system default if null.
+ * @return The <code>NumberFormat</code> to created.
*/
- protected abstract Object processNumber(Number number);
-
+ protected Format getFormat(Locale locale) {
+ NumberFormat formatter = null;
+ switch (formatType) {
+ case CURRENCY_FORMAT:
+ if (locale == null) {
+ formatter = NumberFormat.getCurrencyInstance();
+ } else {
+ formatter = NumberFormat.getCurrencyInstance(locale);
+ }
+ break;
+ case PERCENT_FORMAT:
+ if (locale == null) {
+ formatter = NumberFormat.getPercentInstance();
+ } else {
+ formatter = NumberFormat.getPercentInstance(locale);
+ }
+ break;
+ default:
+ if (locale == null) {
+ formatter = NumberFormat.getInstance();
+ } else {
+ formatter = NumberFormat.getInstance(locale);
+ }
+ break;
+ }
+ return formatter;
+ }
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigDecimalValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigDecimalValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigDecimalValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigDecimalValidator.java Sat Jan 21 19:24:40 2006
@@ -21,6 +21,8 @@
package org.apache.commons.validator.routines;
import java.math.BigDecimal;
+import java.text.Format;
+import java.text.NumberFormat;
import java.util.Locale;
/**
@@ -31,15 +33,18 @@
* a <code>BigDecimal</code> using <code>java.text.NumberFormat</code>
* to parse either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* <p>Use one of the <code>isValid()</code> methods to just validate or
* one of the <code>validate()</code> methods to validate and receive a
* <i>converted</i> <code>BigDecimal</code> value.</p>
*
+ * <p>Fraction/decimal values are automatically trimmed to the appropriate length.</p>
+ *
* <p>Once a value has been sucessfully converted the following
* methods can be used to perform minimum, maximum and range checks:</p>
* <ul>
@@ -56,9 +61,10 @@
* <code>format()</code> methods are also provided. That is you can
* format either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* @version $Revision$ $Date$
@@ -84,13 +90,41 @@
}
/**
- * Construct an instance with the specified strict setting.
+ * <p>Construct an instance with the specified strict setting.</p>
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
*/
public BigDecimalValidator(boolean strict) {
- super(strict, true);
+ this(strict, STANDARD_FORMAT, true);
+ }
+
+ /**
+ * <p>Construct an instance with the specified strict setting
+ * and format type.</p>
+ *
+ * <p>The <code>formatType</code> specified what type of
+ * <code>NumberFormat</code> is created - valid types
+ * are:</p>
+ * <ul>
+ * <li>AbstractNumberValidator.STANDARD_FORMAT -to create
+ * <i>standard</i> number formats (the default).</li>
+ * <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
+ * <i>currency</i> number formats.</li>
+ * <li>AbstractNumberValidator.PERCENT_FORMAT -to create
+ * <i>percent</i> number formats (the default).</li>
+ * </ul>
+ *
+ * @param strict <code>true</code> if strict
+ * <code>Format</code> parsing should be used.
+ * @param formatType The <code>NumberFormat</code> type to
+ * create for validation, default is STANDARD_FORMAT.
+ * @param allowFractions <code>true</code> if fractions are
+ * allowed or <code>false</code> if integers only.
+ */
+ protected BigDecimalValidator(boolean strict, int formatType,
+ boolean allowFractions) {
+ super(strict, formatType, allowFractions);
}
/**
@@ -102,7 +136,7 @@
* if invalid.
*/
public BigDecimal validate(String value) {
- return (BigDecimal)validateObj(value);
+ return (BigDecimal)parse(value, (String)null, (Locale)null);
}
/**
@@ -110,11 +144,12 @@
* specified <i>pattern</i>.
*
* @param value The value validation is being performed on.
- * @param pattern The pattern used to validate the value against.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
* @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
*/
public BigDecimal validate(String value, String pattern) {
- return (BigDecimal)validateObj(value, pattern);
+ return (BigDecimal)parse(value, pattern, (Locale)null);
}
/**
@@ -122,12 +157,25 @@
* specified <code>Locale</code>.
*
* @param value The value validation is being performed on.
- * @param locale The locale to use for the date format, defaults to the default
- * system default if null.
+ * @param locale The locale to use for the number format, system default if null.
* @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
*/
public BigDecimal validate(String value, Locale locale) {
- return (BigDecimal)validateObj(value, locale);
+ return (BigDecimal)parse(value, (String)null, locale);
+ }
+
+ /**
+ * <p>Validate/convert a <code>BigDecimal</code> using the
+ * specified pattern and/ or <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
+ */
+ public BigDecimal validate(String value, String pattern, Locale locale) {
+ return (BigDecimal)parse(value, pattern, locale);
}
/**
@@ -168,17 +216,26 @@
}
/**
- * Returns the parsed number un-changed.
+ * Convert the parsed value to a <code>BigDecimal</code>.
*
- * @param number The number validation is being performed on.
- * @return The validated/converted <code>BigDecimal</code> value if valid
- * or <code>null</code> if invalid.
- */
- protected Object processNumber(Number number) {
- if (number instanceof Long) {
- return BigDecimal.valueOf(number.longValue());
+ * @param value The parsed <code>Number</code> object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed <code>Number</code> converted to a
+ * <code>BigDecimal</code>.
+ */
+ protected Object processParsedValue(Object value, Format formatter) {
+ BigDecimal decimal = null;
+ if (value instanceof Long) {
+ decimal = BigDecimal.valueOf(((Long)value).longValue());
} else {
- return new BigDecimal(number.toString());
+ decimal = new BigDecimal(value.toString());
+ }
+
+ int scale = determineScale((NumberFormat)formatter);
+ if (scale >= 0) {
+ decimal = decimal.setScale(scale, BigDecimal.ROUND_DOWN);
}
+
+ return decimal;
}
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigIntegerValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigIntegerValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigIntegerValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/BigIntegerValidator.java Sat Jan 21 19:24:40 2006
@@ -21,6 +21,7 @@
package org.apache.commons.validator.routines;
import java.math.BigInteger;
+import java.text.Format;
import java.util.Locale;
/**
@@ -31,9 +32,10 @@
* a <code>BigInteger</code> using <code>java.text.NumberFormat</code>
* to parse either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* <p>Use one of the <code>isValid()</code> methods to just validate or
@@ -56,9 +58,10 @@
* <code>format()</code> methods are also provided. That is you can
* format either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* @version $Revision$ $Date$
@@ -80,17 +83,32 @@
* Construct a <i>strict</i> instance.
*/
public BigIntegerValidator() {
- this(true);
+ this(true, STANDARD_FORMAT);
}
/**
- * Construct an instance with the specified strict setting.
+ * <p>Construct an instance with the specified strict setting
+ * and format type.</p>
+ *
+ * <p>The <code>formatType</code> specified what type of
+ * <code>NumberFormat</code> is created - valid types
+ * are:</p>
+ * <ul>
+ * <li>AbstractNumberValidator.STANDARD_FORMAT -to create
+ * <i>standard</i> number formats (the default).</li>
+ * <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
+ * <i>currency</i> number formats.</li>
+ * <li>AbstractNumberValidator.PERCENT_FORMAT -to create
+ * <i>percent</i> number formats (the default).</li>
+ * </ul>
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
+ * @param formatType The <code>NumberFormat</code> type to
+ * create for validation, default is STANDARD_FORMAT.
*/
- public BigIntegerValidator(boolean strict) {
- super(strict, false);
+ public BigIntegerValidator(boolean strict, int formatType) {
+ super(strict, formatType, false);
}
/**
@@ -102,7 +120,7 @@
* if invalid.
*/
public BigInteger validate(String value) {
- return (BigInteger)validateObj(value);
+ return (BigInteger)parse(value, (String)null, (Locale)null);
}
/**
@@ -114,7 +132,7 @@
* @return The parsed <code>BigInteger</code> if valid or <code>null</code> if invalid.
*/
public BigInteger validate(String value, String pattern) {
- return (BigInteger)validateObj(value, pattern);
+ return (BigInteger)parse(value, pattern, (Locale)null);
}
/**
@@ -122,12 +140,25 @@
* specified <code>Locale</code>.
*
* @param value The value validation is being performed on.
- * @param locale The locale to use for the date format, defaults to the default
- * system default if null.
+ * @param locale The locale to use for the number format, system default if null.
* @return The parsed <code>BigInteger</code> if valid or <code>null</code> if invalid.
*/
public BigInteger validate(String value, Locale locale) {
- return (BigInteger)validateObj(value, locale);
+ return (BigInteger)parse(value, (String)null, locale);
+ }
+
+ /**
+ * <p>Validate/convert a <code>BigInteger</code> using the
+ * specified pattern and/ or <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed <code>BigInteger</code> if valid or <code>null</code> if invalid.
+ */
+ public BigInteger validate(String value, String pattern, Locale locale) {
+ return (BigInteger)parse(value, pattern, locale);
}
/**
@@ -168,16 +199,14 @@
}
/**
- * <p>Perform further validation and convert the <code>Number</code> to
- * a <code>BigInteger</code>.</p>
+ * Convert the parsed value to a <code>BigInteger</code>.
*
- * @param number The number validation is being performed on.
- * @return The validated/converted <code>BigInteger</code> value if valid
- * or <code>null</code> if invalid.
+ * @param value The parsed <code>Number</code> object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed <code>Number</code> converted to a
+ * <code>BigInteger</code>.
*/
- protected Object processNumber(Number number) {
-
- return BigInteger.valueOf(number.longValue());
-
+ protected Object processParsedValue(Object value, Format formatter) {
+ return BigInteger.valueOf(((Number)value).longValue());
}
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ByteValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ByteValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ByteValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ByteValidator.java Sat Jan 21 19:24:40 2006
@@ -20,6 +20,7 @@
*/
package org.apache.commons.validator.routines;
+import java.text.Format;
import java.util.Locale;
/**
@@ -30,9 +31,10 @@
* a <code>Byte</code> using <code>java.text.NumberFormat</code>
* to parse either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* <p>Use one of the <code>isValid()</code> methods to just validate or
@@ -55,9 +57,10 @@
* <code>format()</code> methods are also provided. That is you can
* format either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* @version $Revision$ $Date$
@@ -79,17 +82,32 @@
* Construct a <i>strict</i> instance.
*/
public ByteValidator() {
- this(true);
+ this(true, STANDARD_FORMAT);
}
/**
- * Construct an instance with the specified strict setting.
+ * <p>Construct an instance with the specified strict setting
+ * and format type.</p>
+ *
+ * <p>The <code>formatType</code> specified what type of
+ * <code>NumberFormat</code> is created - valid types
+ * are:</p>
+ * <ul>
+ * <li>AbstractNumberValidator.STANDARD_FORMAT -to create
+ * <i>standard</i> number formats (the default).</li>
+ * <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
+ * <i>currency</i> number formats.</li>
+ * <li>AbstractNumberValidator.PERCENT_FORMAT -to create
+ * <i>percent</i> number formats (the default).</li>
+ * </ul>
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
+ * @param formatType The <code>NumberFormat</code> type to
+ * create for validation, default is STANDARD_FORMAT.
*/
- public ByteValidator(boolean strict) {
- super(strict, false);
+ public ByteValidator(boolean strict, int formatType) {
+ super(strict, formatType, false);
}
/**
@@ -101,7 +119,7 @@
* if invalid.
*/
public Byte validate(String value) {
- return (Byte)validateObj(value);
+ return (Byte)parse(value, (String)null, (Locale)null);
}
/**
@@ -113,7 +131,7 @@
* @return The parsed <code>Byte</code> if valid or <code>null</code> if invalid.
*/
public Byte validate(String value, String pattern) {
- return (Byte)validateObj(value, pattern);
+ return (Byte)parse(value, pattern, (Locale)null);
}
/**
@@ -121,12 +139,25 @@
* specified <code>Locale</code>.
*
* @param value The value validation is being performed on.
- * @param locale The locale to use for the date format, defaults to the default
- * system default if null.
+ * @param locale The locale to use for the number format, system default if null.
* @return The parsed <code>Byte</code> if valid or <code>null</code> if invalid.
*/
public Byte validate(String value, Locale locale) {
- return (Byte)validateObj(value, locale);
+ return (Byte)parse(value, (String)null, locale);
+ }
+
+ /**
+ * <p>Validate/convert a <code>Byte</code> using the
+ * specified pattern and/ or <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed <code>Byte</code> if valid or <code>null</code> if invalid.
+ */
+ public Byte validate(String value, String pattern, Locale locale) {
+ return (Byte)parse(value, pattern, locale);
}
/**
@@ -207,24 +238,21 @@
* <p>Perform further validation and convert the <code>Number</code> to
* a <code>Byte</code>.</p>
*
- * @param number The number validation is being performed on.
- * @return The validated/converted <code>Byte</code> value if valid
- * or <code>null</code> if invalid.
+ * @param value The parsed <code>Number</code> object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed <code>Number</code> converted to a
+ * <code>Byte</code> if valid or <code>null</code> if invalid.
*/
- protected Object processNumber(Number number) {
-
- long longValue = number.longValue();
+ protected Object processParsedValue(Object value, Format formatter) {
- if (longValue < Byte.MIN_VALUE) {
- return null;
- }
+ long longValue = ((Number)value).longValue();
- if (longValue > Byte.MAX_VALUE) {
+ if (longValue < Byte.MIN_VALUE ||
+ longValue > Byte.MAX_VALUE) {
return null;
+ } else {
+ return new Byte((byte)longValue);
}
-
- return new Byte(number.byteValue());
-
}
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CalendarValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CalendarValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CalendarValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CalendarValidator.java Sat Jan 21 19:24:40 2006
@@ -21,6 +21,7 @@
package org.apache.commons.validator.routines;
import java.text.DateFormat;
+import java.text.Format;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
@@ -28,15 +29,20 @@
/**
* <p><b>Calendar Validation</b> and Conversion routines (<code>java.util.Calendar</code>).</p>
*
- * <p>This validator provides a number of methods for
- * validating/converting a <code>String</code> value to
- * a <code>Calendar</code> using <code>java.text.DateFormat</code>
- * to parse either:</p>
+ * <p>This validator provides a number of methods for validating/converting
+ * a <code>String</code> date value to a <code>java.util.Calendar</code> using
+ * <code>java.text.DateFormat</code> to parse either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
+ *
+ * <p>For each of the above mechanisms, conversion method (i.e the
+ * <code>validate</code> methods) implementations are provided which
+ * either use the default <code>TimeZone</code> or allow the
+ * <code>TimeZone</code> to be specified.</p>
*
* <p>Use one of the <code>isValid()</code> methods to just validate or
* one of the <code>validate()</code> methods to validate and receive a
@@ -124,12 +130,12 @@
* if invalid.
*/
public Calendar validate(String value) {
- return (Calendar)validateObj(value);
+ return (Calendar)parse(value, (String)null, (Locale)null, (TimeZone)null);
}
/**
* <p>Validate/convert a <code>Calendar</code> using the specified
- * <code>TimeZone</code> and default <code>Locale</code>.
+ * <code>TimeZone</code> and default <code>Locale</code>.
*
* @param value The value validation is being performed on.
* @param timeZone The Time Zone used to parse the date, system default if null.
@@ -137,7 +143,7 @@
* if invalid.
*/
public Calendar validate(String value, TimeZone timeZone) {
- return (Calendar)validateObj(value, Locale.getDefault(), timeZone);
+ return (Calendar)parse(value, (String)null, (Locale)null, timeZone);
}
/**
@@ -149,7 +155,7 @@
* @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
*/
public Calendar validate(String value, String pattern) {
- return (Calendar)validateObj(value, pattern);
+ return (Calendar)parse(value, pattern, (Locale)null, (TimeZone)null);
}
/**
@@ -162,7 +168,7 @@
* @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
*/
public Calendar validate(String value, String pattern, TimeZone timeZone) {
- return (Calendar)validateObj(value, pattern, timeZone);
+ return (Calendar)parse(value, pattern, (Locale)null, timeZone);
}
/**
@@ -174,7 +180,7 @@
* @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
*/
public Calendar validate(String value, Locale locale) {
- return (Calendar)validateObj(value, locale);
+ return (Calendar)parse(value, (String)null, locale, (TimeZone)null);
}
/**
@@ -187,7 +193,36 @@
* @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
*/
public Calendar validate(String value, Locale locale, TimeZone timeZone) {
- return (Calendar)validateObj(value, locale, timeZone);
+ return (Calendar)parse(value, (String)null, locale, timeZone);
+ }
+
+ /**
+ * <p>Validate/convert a <code>Calendar</code> using the specified pattern
+ * and <code>Locale</code> and the default <code>TimeZone</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
+ */
+ public Calendar validate(String value, String pattern, Locale locale) {
+ return (Calendar)parse(value, pattern, locale, (TimeZone)null);
+ }
+
+ /**
+ * <p>Validate/convert a <code>Calendar</code> using the specified
+ * pattern, and <code>Locale</code> and <code>TimeZone</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @param timeZone The Time Zone used to parse the date, system default if null.
+ * @return The parsed <code>Calendar</code> if valid or <code>null</code> if invalid.
+ */
+ public Calendar validate(String value, String pattern, Locale locale, TimeZone timeZone) {
+ return (Calendar)parse(value, pattern, locale, timeZone);
}
/**
@@ -290,18 +325,14 @@
}
/**
- * <p>Perform further validation and convert the <code>Calendar</code> to
- * the appropriate type.</p>
- *
- * <p>This implementation returns the <code>Calendar</code> object
- * unchanged</p>
+ * <p>Convert the parsed <code>Date</code> to a <code>Calendar</code>.</p>
*
- * @param calendar The calendar object create from the parsed value.
- * @return The validated/converted <code>Calendar</code> value if valid
- * or <code>null</code> if invalid.
+ * @param value The parsed <code>Date</code> object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed value converted to a <code>Calendar</code>.
*/
- protected Object processCalendar(Calendar calendar) {
- return calendar;
+ protected Object processParsedValue(Object value, Format formatter) {
+ return ((DateFormat)formatter).getCalendar();
}
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CurrencyValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CurrencyValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CurrencyValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/CurrencyValidator.java Sat Jan 21 19:24:40 2006
@@ -20,20 +20,29 @@
*/
package org.apache.commons.validator.routines;
+import java.text.DecimalFormat;
import java.text.Format;
-import java.text.NumberFormat;
-import java.util.Locale;
/**
* <p><b>Currency Validation</b> and Conversion routines (<code>java.math.BigDecimal</code>).</p>
*
- * <p>This validator is a variation of the <code>BigDecimal</code> Validator
- * that uses the <i>currency</i> format when validating using
- * a <code>Locale</code>.</p>
+ * <p>This is one implementation of a currency validator that has the following features:</p>
+ * <ul>
+ * <li>It is <i>lenient</i> about the the presence of the <i>currency symbol</i></li>
+ * <li>It converts the currency to a <code>java.math.BigDecimal</code></li>
+ * </ul>
*
- * <p>Otherwise it provides the same functionality as the <code>BigDecimal</code>
- * with converted values being returned as a <code>BigDecimal</code>.</p>
+ * <p>However any of the <i>number</i> validators can be used for <i>currency</i> validation.
+ * For example, if you wanted a <i>currency</i> validator that converts to a
+ * <code>java.lang.Integer</code> then you can simply instantiate an
+ * <code>IntegerValidator</code> with the appropriate <i>format type</i>:</p>
+ *
+ * <p><code>... = new IntegerValidator(false, IntegerValidator.CURRENCY_FORMAT);</code></p>
*
+ * <p>Pick the appropriate validator, depending on the type (e.g Float, Double, Integer, Long etc)
+ * you want the currency converted to. One thing to note - only the CurrencyValidator
+ * implements <i>lenient</i> behaviour regarding the currency symbol.</p>
+ *
* @version $Revision$ $Date$
* @since Validator 1.2.1
*/
@@ -41,6 +50,9 @@
private static final CurrencyValidator VALIDATOR = new CurrencyValidator();
+ /** DecimalFormat's currency symbol */
+ private static final char CURRENCY_SYMBOL = '\u00A4';
+
/**
* Return a singleton instance of this validator.
* @return A singleton instance of the CurrencyValidator.
@@ -53,7 +65,7 @@
* Construct a <i>strict</i> instance.
*/
public CurrencyValidator() {
- this(true);
+ this(true, true);
}
/**
@@ -61,26 +73,47 @@
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
+ * @param allowFractions <code>true</code> if fractions are
+ * allowed or <code>false</code> if integers only.
*/
- public CurrencyValidator(boolean strict) {
- super(strict);
+ public CurrencyValidator(boolean strict, boolean allowFractions) {
+ super(strict, CURRENCY_FORMAT, allowFractions);
}
/**
- * <p>Returns a currency <code>NumberFormat</code> for the specified Locale.</p>
+ * <p>Parse the value with the specified <code>Format</code>.</p>
+ *
+ * <p>This implementation is lenient whether the currency symbol
+ * is present or not. The default <code>NumberFormat</code>
+ * behaviour is for the parsing to "fail" if the currency
+ * symbol is missing. This method re-parses with a format
+ * without the currency symbol if it fails initially.</p>
*
- * @param locale The locale a currency <code>NumberFormat</code> is required
- * for, defaults to the default.
- * @return The currency <code>NumberFormat</code> to created.
+ * @param value The value to be parsed.
+ * @param formatter The Format to parse the value with.
+ * @return The parsed value if valid or <code>null</code> if invalid.
*/
- public Format getFormat(Locale locale) {
+ protected Object parse(String value, Format formatter) {
- if (locale == null) {
- locale = Locale.getDefault();
+ // Initial parse of the value
+ Object parsedValue = super.parse(value, formatter);
+ if (parsedValue != null || !(formatter instanceof DecimalFormat)) {
+ return parsedValue;
}
- return NumberFormat.getCurrencyInstance(locale);
-
+ // Re-parse using a pattern without the currency symbol
+ DecimalFormat decimalFormat = (DecimalFormat)formatter;
+ String pattern = decimalFormat.toPattern();
+ if (pattern.indexOf(CURRENCY_SYMBOL) >= 0) {
+ StringBuffer buffer = new StringBuffer(pattern.length());
+ for (int i = 0; i < pattern.length(); i++) {
+ if (pattern.charAt(i) != CURRENCY_SYMBOL) {
+ buffer.append(pattern.charAt(i));
+ }
+ }
+ decimalFormat.applyPattern(buffer.toString());
+ parsedValue = super.parse(value, decimalFormat);
+ }
+ return parsedValue;
}
-
}
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DateValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DateValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DateValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DateValidator.java Sat Jan 21 19:24:40 2006
@@ -21,6 +21,7 @@
package org.apache.commons.validator.routines;
import java.text.DateFormat;
+import java.text.Format;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
@@ -29,16 +30,21 @@
/**
* <p><b>Date Validation</b> and Conversion routines (<code>java.util.Date</code>).</p>
*
- * <p>This validator provides a number of methods for
- * validating/converting a <code>String</code> value to
- * a <code>Date</code> using <code>java.text.DateFormat</code>
- * to parse either:</p>
+ * <p>This validator provides a number of methods for validating/converting
+ * a <code>String</code> date value to a <code>java.util.Date</code> using
+ * <code>java.text.DateFormat</code> to parse either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
+ * <p>For each of the above mechanisms, conversion method (i.e the
+ * <code>validate</code> methods) implementations are provided which
+ * either use the default <code>TimeZone</code> or allow the
+ * <code>TimeZone</code> to be specified.</p>
+ *
* <p>Use one of the <code>isValid()</code> methods to just validate or
* one of the <code>validate()</code> methods to validate and receive a
* <i>converted</i> <code>Date</code> value.</p>
@@ -121,7 +127,7 @@
* if invalid.
*/
public Date validate(String value) {
- return (Date)validateObj(value);
+ return (Date)parse(value, (String)null, (Locale)null, (TimeZone)null);
}
/**
@@ -133,7 +139,7 @@
* @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
*/
public Date validate(String value, TimeZone timeZone) {
- return (Date)validateObj(value, Locale.getDefault(), timeZone);
+ return (Date)parse(value, (String)null, (Locale)null, timeZone);
}
/**
@@ -141,11 +147,12 @@
* <i>pattern</i> and default <code>TimeZone</code>.
*
* @param value The value validation is being performed on.
- * @param pattern The pattern used to validate the value against.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
* @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
*/
public Date validate(String value, String pattern) {
- return (Date)validateObj(value, pattern);
+ return (Date)parse(value, pattern, (Locale)null, (TimeZone)null);
}
/**
@@ -153,12 +160,13 @@
* <i>pattern</i> and <code>TimeZone</code>.
*
* @param value The value validation is being performed on.
- * @param pattern The pattern used to validate the value against.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
* @param timeZone The Time Zone used to parse the date, system default if null.
* @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
*/
public Date validate(String value, String pattern, TimeZone timeZone) {
- return (Date)validateObj(value, pattern, timeZone);
+ return (Date)parse(value, pattern, (Locale)null, timeZone);
}
/**
@@ -170,7 +178,7 @@
* @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
*/
public Date validate(String value, Locale locale) {
- return (Date)validateObj(value, locale);
+ return (Date)parse(value, (String)null, locale, (TimeZone)null);
}
/**
@@ -183,7 +191,36 @@
* @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
*/
public Date validate(String value, Locale locale, TimeZone timeZone) {
- return (Date)validateObj(value, locale, timeZone);
+ return (Date)parse(value, (String)null, locale, timeZone);
+ }
+
+ /**
+ * <p>Validate/convert a <code>Date</code> using the specified pattern
+ * and <code>Locale</code> and the default <code>TimeZone</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
+ */
+ public Date validate(String value, String pattern, Locale locale) {
+ return (Date)parse(value, pattern, locale, (TimeZone)null);
+ }
+
+ /**
+ * <p>Validate/convert a <code>Date</code> using the specified
+ * pattern, and <code>Locale</code> and <code>TimeZone</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @param timeZone The Time Zone used to parse the date, system default if null.
+ * @return The parsed <code>Date</code> if valid or <code>null</code> if invalid.
+ */
+ public Date validate(String value, String pattern, Locale locale, TimeZone timeZone) {
+ return (Date)parse(value, pattern, locale, timeZone);
}
/**
@@ -282,14 +319,14 @@
}
/**
- * <p>Convert the <code>Calendar</code> to a <code>Date</code></p>
+ * <p>Returns the parsed <code>Date</code> unchanged.</p>
*
- * @param calendar The calendar object create from the parsed value.
- * @return The validated/converted <code>Calendar</code> value if valid
- * or <code>null</code> if invalid.
+ * @param value The parsed <code>Date</code> object created.
+ * @param formatter The Format used to parse the value with.
+ * @return The parsed value converted to a <code>Calendar</code>.
*/
- protected Object processCalendar(Calendar calendar) {
- return calendar.getTime();
+ protected Object processParsedValue(Object value, Format formatter) {
+ return value;
}
/**
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DoubleValidator.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DoubleValidator.java?rev=371174&r1=371173&r2=371174&view=diff
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DoubleValidator.java (original)
+++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/DoubleValidator.java Sat Jan 21 19:24:40 2006
@@ -20,6 +20,7 @@
*/
package org.apache.commons.validator.routines;
+import java.text.Format;
import java.util.Locale;
/**
@@ -30,9 +31,10 @@
* a <code>Double</code> using <code>java.text.NumberFormat</code>
* to parse either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* <p>Use one of the <code>isValid()</code> methods to just validate or
@@ -55,9 +57,10 @@
* <code>format()</code> methods are also provided. That is you can
* format either:</p>
* <ul>
- * <li>using a specified pattern</li>
- * <li>using the format for a specified <code>Locale</code></li>
- * <li>using the format for the <i>default</i> <code>Locale</code></li>
+ * <li>using the default format for the default <code>Locale</code></li>
+ * <li>using a specified pattern with the default <code>Locale</code></li>
+ * <li>using the default format for a specified <code>Locale</code></li>
+ * <li>using a specified pattern with a specified <code>Locale</code></li>
* </ul>
*
* @version $Revision$ $Date$
@@ -79,17 +82,32 @@
* Construct a <i>strict</i> instance.
*/
public DoubleValidator() {
- this(true);
+ this(true, STANDARD_FORMAT);
}
/**
- * Construct an instance with the specified strict setting.
+ * <p>Construct an instance with the specified strict setting
+ * and format type.</p>
+ *
+ * <p>The <code>formatType</code> specified what type of
+ * <code>NumberFormat</code> is created - valid types
+ * are:</p>
+ * <ul>
+ * <li>AbstractNumberValidator.STANDARD_FORMAT -to create
+ * <i>standard</i> number formats (the default).</li>
+ * <li>AbstractNumberValidator.CURRENCY_FORMAT -to create
+ * <i>currency</i> number formats.</li>
+ * <li>AbstractNumberValidator.PERCENT_FORMAT -to create
+ * <i>percent</i> number formats (the default).</li>
+ * </ul>
*
* @param strict <code>true</code> if strict
* <code>Format</code> parsing should be used.
+ * @param formatType The <code>NumberFormat</code> type to
+ * create for validation, default is STANDARD_FORMAT.
*/
- public DoubleValidator(boolean strict) {
- super(strict, true);
+ public DoubleValidator(boolean strict, int formatType) {
+ super(strict, formatType, true);
}
/**
@@ -101,7 +119,7 @@
* if invalid.
*/
public Double validate(String value) {
- return (Double)validateObj(value);
+ return (Double)parse(value, (String)null, (Locale)null);
}
/**
@@ -113,7 +131,7 @@
* @return The parsed <code>BigDecimal</code> if valid or <code>null</code> if invalid.
*/
public Double validate(String value, String pattern) {
- return (Double)validateObj(value, pattern);
+ return (Double)parse(value, pattern, (Locale)null);
}
/**
@@ -121,12 +139,25 @@
* specified <code>Locale</code>.
*
* @param value The value validation is being performed on.
- * @param locale The locale to use for the date format, defaults to the default
- * system default if null.
+ * @param locale The locale to use for the number format, system default if null.
* @return The parsed <code>Double</code> if valid or <code>null</code> if invalid.
*/
public Double validate(String value, Locale locale) {
- return (Double)validateObj(value, locale);
+ return (Double)parse(value, (String)null, locale);
+ }
+
+ /**
+ * <p>Validate/convert a <code>Double</code> using the
+ * specified pattern and/ or <code>Locale</code>.
+ *
+ * @param value The value validation is being performed on.
+ * @param pattern The pattern used to validate the value against, or the
+ * default for the <code>Locale</code> if <code>null</code>.
+ * @param locale The locale to use for the date format, system default if null.
+ * @return The parsed <code>Double</code> if valid or <code>null</code> if invalid.
+ */
+ public Double validate(String value, String pattern, Locale locale) {
+ return (Double)parse(value, pattern, locale);
}
/**
@@ -204,19 +235,19 @@
}
/**
- * <p>Perform further validation and convert the <code>Number</code> to
- * a <code>Double</code>.</p>
+ * Convert the parsed value to a <code>Double</code>.
*
- * @param number The number validation is being performed on.
+ * @param value The parsed <code>Number</code> object created.
+ * @param formatter The Format used to parse the value with.
* @return The validated/converted <code>Double</code> value if valid
* or <code>null</code> if invalid.
*/
- protected Object processNumber(Number number) {
+ protected Object processParsedValue(Object value, Format formatter) {
- if (number instanceof Double) {
- return number;
+ if (value instanceof Double) {
+ return value;
} else {
- return new Double(number.doubleValue());
+ return new Double(((Number)value).doubleValue());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org