javadoc polishing--named Parser String arg name to 'text'

This commit is contained in:
Keith Donald 2009-11-28 21:16:01 +00:00
parent fbfa67e8a6
commit 0be6473316
7 changed files with 31 additions and 39 deletions

View File

@ -20,22 +20,22 @@ import java.text.ParseException;
import java.util.Locale; import java.util.Locale;
/** /**
* Parses objects of type T from their printed representations. * Parses text strings to produce instances of T.
* *
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @param <T> the type of object this Parser parses * @param <T> the type of object this Parser produces
*/ */
public interface Parser<T> { public interface Parser<T> {
/** /**
* Parse an object from its printed representation. * Parse a text String to produce a T.
* @param printed a printed representation * @param text the text string
* @param locale the current user locale * @param locale the current user locale
* @return the parsed object * @return an instance of T
* @throws ParseException when a parse exception occurs in a java.text parsing library * @throws ParseException when a parse exception occurs in a java.text parsing library
* @throws RuntimeException when a parse exception occurs * @throws IllegalArgumentException when a parse exception occurs
*/ */
T parse(String printed, Locale locale) throws ParseException; T parse(String text, Locale locale) throws ParseException;
} }

View File

@ -29,9 +29,9 @@ public interface Printer<T> {
/** /**
* Print the object of type T for display. * Print the object of type T for display.
* @param object the object to print * @param object the instance to print
* @param locale the current user locale * @param locale the current user locale
* @return the printed string * @return the printed text string
*/ */
String print(T object, Locale locale); String print(T object, Locale locale);

View File

@ -101,8 +101,8 @@ public class DateFormatter implements Formatter<Date> {
return getDateFormat(locale).format(date); return getDateFormat(locale).format(date);
} }
public Date parse(String formatted, Locale locale) throws ParseException { public Date parse(String text, Locale locale) throws ParseException {
return getDateFormat(locale).parse(formatted); return getDateFormat(locale).parse(text);
} }

View File

@ -39,7 +39,7 @@ public final class DateTimeParser implements Parser<DateTime> {
this.formatter = formatter; this.formatter = formatter;
} }
public DateTime parse(String printed, Locale locale) throws ParseException { public DateTime parse(String text, Locale locale) throws ParseException {
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(printed); return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text);
} }
} }

View File

@ -44,21 +44,21 @@ public abstract class AbstractNumberFormatter implements Formatter<Number> {
this.lenient = lenient; this.lenient = lenient;
} }
public String print(Number integer, Locale locale) { public String print(Number number, Locale locale) {
return getNumberFormat(locale).format(integer); return getNumberFormat(locale).format(number);
} }
public Number parse(String formatted, Locale locale) throws ParseException { public Number parse(String text, Locale locale) throws ParseException {
NumberFormat format = getNumberFormat(locale); NumberFormat format = getNumberFormat(locale);
ParsePosition position = new ParsePosition(0); ParsePosition position = new ParsePosition(0);
Number number = format.parse(formatted, position); Number number = format.parse(text, position);
if (position.getErrorIndex() != -1) { if (position.getErrorIndex() != -1) {
throw new ParseException(formatted, position.getIndex()); throw new ParseException(text, position.getIndex());
} }
if (!this.lenient) { if (!this.lenient) {
if (formatted.length() != position.getIndex()) { if (text.length() != position.getIndex()) {
// indicates a part of the string that was not parsed // indicates a part of the string that was not parsed
throw new ParseException(formatted, position.getIndex()); throw new ParseException(text, position.getIndex());
} }
} }
return number; return number;

View File

@ -75,8 +75,8 @@ public final class CurrencyFormatter extends AbstractNumberFormatter {
} }
public BigDecimal parse(String formatted, Locale locale) throws ParseException { public BigDecimal parse(String text, Locale locale) throws ParseException {
BigDecimal decimal = (BigDecimal) super.parse(formatted, locale); BigDecimal decimal = (BigDecimal) super.parse(text, locale);
if (decimal != null) { if (decimal != null) {
if (this.roundingMode != null) { if (this.roundingMode != null) {
decimal = decimal.setScale(this.fractionDigits, this.roundingMode); decimal = decimal.setScale(this.fractionDigits, this.roundingMode);

View File

@ -17,12 +17,10 @@
package org.springframework.format.support; package org.springframework.format.support;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.text.ParseException;
import java.util.Set; import java.util.Set;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.GenericTypeResolver; import org.springframework.core.GenericTypeResolver;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.ConversionFailedException; import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.TypeDescriptor;
@ -164,27 +162,21 @@ public class FormattingConversionService extends GenericConversionService
} }
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
String submittedValue = (String) source; String text = (String) source;
if (submittedValue == null || submittedValue.length() == 0) { if (text == null || text.length() == 0) {
return null; return null;
} }
Object parsedValue;
try { try {
parsedValue = this.parser.parse(submittedValue, LocaleContextHolder.getLocale()); Object result = this.parser.parse(text, LocaleContextHolder.getLocale());
TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass());
if (!resultType.isAssignableTo(targetType)) {
result = this.conversionService.convert(result, resultType, targetType);
}
return result;
} }
catch (ParseException ex) { catch (Exception ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex); throw new ConversionFailedException(sourceType, targetType, source, ex);
} }
TypeDescriptor parsedObjectType = TypeDescriptor.valueOf(parsedValue.getClass());
if (!parsedObjectType.isAssignableTo(targetType)) {
try {
parsedValue = this.conversionService.convert(parsedValue, parsedObjectType, targetType);
}
catch (ConversionException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
}
return parsedValue;
} }
public String toString() { public String toString() {