do not register a default NumberFormatter (SPR-6490)

This commit is contained in:
Juergen Hoeller
2009-12-03 22:41:44 +00:00
parent ac3a1d9132
commit 73a75220a8
4 changed files with 54 additions and 18 deletions

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.format.number;
import java.math.BigDecimal;
@@ -30,6 +31,7 @@ import org.springframework.format.annotation.NumberFormat.Style;
/**
* Formats fields annotated with the {@link NumberFormat} annotation.
*
* @author Keith Donald
* @since 3.0
* @see NumberFormat
@@ -37,11 +39,13 @@ import org.springframework.format.annotation.NumberFormat.Style;
public final class NumberFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<NumberFormat> {
private final Set<Class<?>> fieldTypes;
public NumberFormatAnnotationFormatterFactory() {
this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
}
public Set<Class<?>> getFieldTypes() {
return this.fieldTypes;
}
@@ -54,6 +58,7 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF
return configureFormatterFrom(annotation, fieldType);
}
// internal helpers
private Set<Class<?>> createFieldTypes() {
@@ -71,16 +76,19 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF
private Formatter<Number> configureFormatterFrom(NumberFormat annotation, Class<?> fieldType) {
if (!annotation.pattern().isEmpty()) {
return new NumberFormatter(annotation.pattern());
} else {
}
else {
Style style = annotation.style();
if (style == Style.PERCENT) {
return new PercentFormatter();
} else if (style == Style.CURRENCY) {
}
else if (style == Style.CURRENCY) {
return new CurrencyFormatter();
} else {
}
else {
return new NumberFormatter();
}
}
}
}
}

View File

@@ -38,18 +38,26 @@ public final class NumberFormatter extends AbstractNumberFormatter {
private String pattern;
/**
* Create a new NumberFormatter without a pattern.
*/
public NumberFormatter() {
}
/**
* Create a new NumberFormatter with the specified pattern.
* @param pattern the format pattern
* @see #setPattern
*/
public NumberFormatter(String pattern) {
this.pattern = pattern;
}
/**
* Sets the pattern to use to format number values.
* If not specified, the default DecimalFormat pattern is used.
* @param pattern the format pattern
* @see DecimalFormat#applyPattern(String)
*/
public void setPattern(String pattern) {

View File

@@ -22,7 +22,6 @@ import org.springframework.core.convert.support.ConversionServiceFactory;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
import org.springframework.format.number.NumberFormatter;
import org.springframework.util.ClassUtils;
/**
@@ -50,9 +49,10 @@ public class FormattingConversionServiceFactoryBean
ConversionServiceFactory.addDefaultConverters(this.conversionService);
installFormatters(this.conversionService);
}
// implementing FactoryBean
public FormattingConversionService getObject() {
return this.conversionService;
}
@@ -67,14 +67,13 @@ public class FormattingConversionServiceFactoryBean
// subclassing hooks
/**
* Install Formatters and Converters into the new FormattingConversionService using the FormatterRegistry SPI.
* Subclasses may override to customize the set of formatters and/or converters that are installed.
*/
protected void installFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(Number.class, new NumberFormatter());
registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
if (jodaTimePresent) {
new JodaTimeFormattingConfigurer().installJodaTimeFormatting(registry);
}