javadoc polishing--named Parser String arg name to 'text'
This commit is contained in:
parent
fbfa67e8a6
commit
0be6473316
|
|
@ -20,22 +20,22 @@ import java.text.ParseException;
|
|||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Parses objects of type T from their printed representations.
|
||||
* Parses text strings to produce instances of T.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @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> {
|
||||
|
||||
/**
|
||||
* Parse an object from its printed representation.
|
||||
* @param printed a printed representation
|
||||
* Parse a text String to produce a T.
|
||||
* @param text the text string
|
||||
* @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 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;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,9 @@ public interface Printer<T> {
|
|||
|
||||
/**
|
||||
* 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
|
||||
* @return the printed string
|
||||
* @return the printed text string
|
||||
*/
|
||||
String print(T object, Locale locale);
|
||||
|
||||
|
|
|
|||
|
|
@ -101,8 +101,8 @@ public class DateFormatter implements Formatter<Date> {
|
|||
return getDateFormat(locale).format(date);
|
||||
}
|
||||
|
||||
public Date parse(String formatted, Locale locale) throws ParseException {
|
||||
return getDateFormat(locale).parse(formatted);
|
||||
public Date parse(String text, Locale locale) throws ParseException {
|
||||
return getDateFormat(locale).parse(text);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ public final class DateTimeParser implements Parser<DateTime> {
|
|||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
public DateTime parse(String printed, Locale locale) throws ParseException {
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(printed);
|
||||
public DateTime parse(String text, Locale locale) throws ParseException {
|
||||
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,21 +44,21 @@ public abstract class AbstractNumberFormatter implements Formatter<Number> {
|
|||
this.lenient = lenient;
|
||||
}
|
||||
|
||||
public String print(Number integer, Locale locale) {
|
||||
return getNumberFormat(locale).format(integer);
|
||||
public String print(Number number, Locale locale) {
|
||||
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);
|
||||
ParsePosition position = new ParsePosition(0);
|
||||
Number number = format.parse(formatted, position);
|
||||
Number number = format.parse(text, position);
|
||||
if (position.getErrorIndex() != -1) {
|
||||
throw new ParseException(formatted, position.getIndex());
|
||||
throw new ParseException(text, position.getIndex());
|
||||
}
|
||||
if (!this.lenient) {
|
||||
if (formatted.length() != position.getIndex()) {
|
||||
if (text.length() != position.getIndex()) {
|
||||
// indicates a part of the string that was not parsed
|
||||
throw new ParseException(formatted, position.getIndex());
|
||||
throw new ParseException(text, position.getIndex());
|
||||
}
|
||||
}
|
||||
return number;
|
||||
|
|
|
|||
|
|
@ -75,8 +75,8 @@ public final class CurrencyFormatter extends AbstractNumberFormatter {
|
|||
}
|
||||
|
||||
|
||||
public BigDecimal parse(String formatted, Locale locale) throws ParseException {
|
||||
BigDecimal decimal = (BigDecimal) super.parse(formatted, locale);
|
||||
public BigDecimal parse(String text, Locale locale) throws ParseException {
|
||||
BigDecimal decimal = (BigDecimal) super.parse(text, locale);
|
||||
if (decimal != null) {
|
||||
if (this.roundingMode != null) {
|
||||
decimal = decimal.setScale(this.fractionDigits, this.roundingMode);
|
||||
|
|
|
|||
|
|
@ -17,12 +17,10 @@
|
|||
package org.springframework.format.support;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.text.ParseException;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.convert.ConversionException;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
|
|
@ -164,27 +162,21 @@ public class FormattingConversionService extends GenericConversionService
|
|||
}
|
||||
|
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
|
||||
String submittedValue = (String) source;
|
||||
if (submittedValue == null || submittedValue.length() == 0) {
|
||||
String text = (String) source;
|
||||
if (text == null || text.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
Object parsedValue;
|
||||
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);
|
||||
}
|
||||
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() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue