diff --git a/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java index cb321dbe62..c5baf995b1 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java +++ b/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java @@ -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 { private final Set> fieldTypes; - + + public NumberFormatAnnotationFormatterFactory() { this.fieldTypes = Collections.unmodifiableSet(createFieldTypes()); } + public Set> getFieldTypes() { return this.fieldTypes; } @@ -54,6 +58,7 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF return configureFormatterFrom(annotation, fieldType); } + // internal helpers private Set> createFieldTypes() { @@ -71,16 +76,19 @@ public final class NumberFormatAnnotationFormatterFactory implements AnnotationF private Formatter 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(); } } } -} \ No newline at end of file +} diff --git a/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatter.java b/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatter.java index d373afc848..91e4af0e88 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatter.java +++ b/org.springframework.context/src/main/java/org/springframework/format/number/NumberFormatter.java @@ -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) { diff --git a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java index 994194292e..80bb0455c8 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java +++ b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java @@ -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); } diff --git a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java index 364272e8ae..7afa524310 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceFactoryBeanTests.java @@ -1,17 +1,37 @@ -package org.springframework.format.support; +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static org.junit.Assert.assertEquals; +package org.springframework.format.support; import java.math.BigDecimal; import java.util.Date; import java.util.Locale; import org.junit.After; +import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; + import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.convert.ConversionService; +/** + * @author Keith Donald + * @author Juergen Hoeller + */ public class FormattingConversionServiceFactoryBeanTests { private ConversionService conversionService; @@ -31,13 +51,14 @@ public class FormattingConversionServiceFactoryBeanTests { @Test public void testFormatNumber() { - BigDecimal value = conversionService.convert("3,000.25", BigDecimal.class); - assertEquals("3,000.25", conversionService.convert(value, String.class)); + BigDecimal value = conversionService.convert("3000.25", BigDecimal.class); + assertEquals("3000.25", conversionService.convert(value, String.class)); } @Test public void testFormatDate() { Date value = conversionService.convert("10/29/09 12:00 PM", Date.class); - assertEquals("10/29/09 12:00 PM", conversionService.convert(value, String.class)); + assertEquals("10/29/09 12:00 PM", conversionService.convert(value, String.class)); } + }