polishing

This commit is contained in:
Juergen Hoeller
2009-12-09 17:54:02 +00:00
parent 8334a03e03
commit 2ce5090d00
5 changed files with 134 additions and 160 deletions

View File

@@ -31,21 +31,7 @@ public interface FormatterRegistry extends ConverterRegistry {
/**
* Adds a Formatter to format fields of a specific type.
* The Formatter will delegate to the specified <code>printer</code> for printing and the specified <code>parser</code> for parsing.
* <p>
* On print, if the Printer's type T is declared and <code>fieldType</code> is not assignable to T,
* a coersion to T will be attempted before delegating to <code>printer</code> to print a field value.
* On parse, if the object returned by the Parser is not assignable to the runtime field type,
* a coersion to the field type will be attempted before returning the parsed field value.
* @param fieldType the field type to format
* @param formatter the formatter to add
*/
void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser);
/**
* Adds a Formatter to format fields of a specific type.
* <p>
* On print, if the Formatter's type T is declared and <code>fieldType</code> is not assignable to T,
* <p>On print, if the Formatter's type T is declared and <code>fieldType</code> is not assignable to T,
* a coersion to T will be attempted before delegating to <code>formatter</code> to print a field value.
* On parse, if the parsed object returned by <code>formatter</code> is not assignable to the runtime field type,
* a coersion to the field type will be attempted before returning the parsed field value.
@@ -54,6 +40,20 @@ public interface FormatterRegistry extends ConverterRegistry {
*/
void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter);
/**
* Adds a Printer/Parser pair to format fields of a specific type.
* The formatter will delegate to the specified <code>printer</code> for printing
* and the specified <code>parser</code> for parsing.
* <p>On print, if the Printer's type T is declared and <code>fieldType</code> is not assignable to T,
* a coersion to T will be attempted before delegating to <code>printer</code> to print a field value.
* On parse, if the object returned by the Parser is not assignable to the runtime field type,
* a coersion to the field type will be attempted before returning the parsed field value.
* @param fieldType the field type to format
* @param printer the printing part of the formatter
* @param parser the parsing part of the formatter
*/
void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser);
/**
* Adds a Formatter to format fields annotated with a specific format annotation.
* @param annotationFormatterFactory the annotation formatter factory to add

View File

@@ -17,6 +17,7 @@
package org.springframework.format.support;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.Set;
import org.springframework.context.i18n.LocaleContextHolder;
@@ -41,19 +42,18 @@ import org.springframework.format.Printer;
* @author Juergen Hoeller
* @since 3.0
*/
public class FormattingConversionService extends GenericConversionService
implements FormatterRegistry {
public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) {
addGenericConverter(new PrinterConverter(fieldType, printer, this));
addGenericConverter(new ParserConverter(fieldType, parser, this));
}
public class FormattingConversionService extends GenericConversionService implements FormatterRegistry {
public void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter) {
addGenericConverter(new PrinterConverter(fieldType, formatter, this));
addGenericConverter(new ParserConverter(fieldType, formatter, this));
}
public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) {
addGenericConverter(new PrinterConverter(fieldType, printer, this));
addGenericConverter(new ParserConverter(fieldType, parser, this));
}
@SuppressWarnings("unchecked")
public void addFormatterForFieldAnnotation(final AnnotationFormatterFactory annotationFormatterFactory) {
final Class<? extends Annotation> annotationType = (Class<? extends Annotation>)
@@ -68,8 +68,8 @@ public class FormattingConversionService extends GenericConversionService
for (final Class<?> fieldType : fieldTypes) {
addGenericConverter(new ConditionalGenericConverter() {
public Class<?>[][] getConvertibleTypes() {
return new Class<?>[][] {{fieldType, String.class}};
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(fieldType, String.class));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return (sourceType.getAnnotation(annotationType) != null);
@@ -84,8 +84,8 @@ public class FormattingConversionService extends GenericConversionService
}
});
addGenericConverter(new ConditionalGenericConverter() {
public Class<?>[][] getConvertibleTypes() {
return new Class<?>[][] {{String.class, fieldType}};
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, fieldType));
}
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
return (targetType.getAnnotation(annotationType) != null);
@@ -121,8 +121,8 @@ public class FormattingConversionService extends GenericConversionService
this.conversionService = conversionService;
}
public Class<?>[][] getConvertibleTypes() {
return new Class<?>[][] { { this.fieldType, String.class } };
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(this.fieldType, String.class));
}
@SuppressWarnings("unchecked")
@@ -130,7 +130,7 @@ public class FormattingConversionService extends GenericConversionService
if (!sourceType.isAssignableTo(this.printerObjectType)) {
source = this.conversionService.convert(source, sourceType, this.printerObjectType);
}
return source != null ? this.printer.print(source, LocaleContextHolder.getLocale()) : "";
return (source != null ? this.printer.print(source, LocaleContextHolder.getLocale()) : "");
}
private Class<?> resolvePrinterObjectType(Printer<?> printer) {
@@ -157,8 +157,8 @@ public class FormattingConversionService extends GenericConversionService
this.conversionService = conversionService;
}
public Class<?>[][] getConvertibleTypes() {
return new Class<?>[][] { { String.class, this.fieldType } };
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, this.fieldType));
}
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {