diff --git a/spring-context/src/main/java/org/springframework/context/support/EmbeddedValueResolutionSupport.java b/spring-context/src/main/java/org/springframework/context/support/EmbeddedValueResolutionSupport.java new file mode 100644 index 0000000000..c81319f44f --- /dev/null +++ b/spring-context/src/main/java/org/springframework/context/support/EmbeddedValueResolutionSupport.java @@ -0,0 +1,50 @@ +/* + * Copyright 2002-2014 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. + */ + +package org.springframework.context.support; + +import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.util.StringValueResolver; + +/** + * Convenient base class for components with a need for embedded value resolution + * (i.e. {@link org.springframework.context.EmbeddedValueResolverAware} consumers). + * + * @author Juergen Hoeller + * @since 4.1 + */ +public class EmbeddedValueResolutionSupport implements EmbeddedValueResolverAware { + + private StringValueResolver embeddedValueResolver; + + + @Override + public void setEmbeddedValueResolver(StringValueResolver resolver) { + this.embeddedValueResolver = resolver; + } + + /** + * Resolve the given embedded value through this instance's {@link StringValueResolver}. + * @param value the value to resolve + * @return the resolved value, or always the original value if no resolver is available + * @see #setEmbeddedValueResolver + */ + protected String resolveEmbeddedValue(String value) { + return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); + } + + +} diff --git a/spring-context/src/main/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java index eecad277ad..5c749b4af5 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -22,13 +22,12 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; -import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.context.support.EmbeddedValueResolutionSupport; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.Formatter; import org.springframework.format.Parser; import org.springframework.format.Printer; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.util.StringValueResolver; /** * Formats fields annotated with the {@link DateTimeFormat} annotation using @@ -38,11 +37,12 @@ import org.springframework.util.StringValueResolver; * @since 3.2 * @see org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory */ -public class DateTimeFormatAnnotationFormatterFactory implements - AnnotationFormatterFactory, EmbeddedValueResolverAware { +public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport + implements AnnotationFormatterFactory { private static final Set> FIELD_TYPES; + static { Set> fieldTypes = new HashSet>(4); fieldTypes.add(Date.class); @@ -52,14 +52,6 @@ public class DateTimeFormatAnnotationFormatterFactory implements } - private StringValueResolver embeddedValueResolver; - - - @Override - public void setEmbeddedValueResolver(StringValueResolver resolver) { - this.embeddedValueResolver = resolver; - } - @Override public Set> getFieldTypes() { return FIELD_TYPES; @@ -83,8 +75,4 @@ public class DateTimeFormatAnnotationFormatterFactory implements return formatter; } - protected String resolveEmbeddedValue(String value) { - return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); - } - } diff --git a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java index de14a7fb2b..0e1a057d0a 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -29,12 +29,11 @@ import org.joda.time.ReadableInstant; import org.joda.time.ReadablePartial; import org.joda.time.format.DateTimeFormatter; -import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.context.support.EmbeddedValueResolutionSupport; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.Parser; import org.springframework.format.Printer; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.util.StringValueResolver; /** * Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time. @@ -46,10 +45,11 @@ import org.springframework.util.StringValueResolver; * @since 3.0 * @see DateTimeFormat */ -public class JodaDateTimeFormatAnnotationFormatterFactory - implements AnnotationFormatterFactory, EmbeddedValueResolverAware { +public class JodaDateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport + implements AnnotationFormatterFactory { private static final Set> FIELD_TYPES; + static { // Create the set of field types that may be annotated with @DateTimeFormat. // Note: the 3 ReadablePartial concrete types are registered explicitly since @@ -69,19 +69,6 @@ public class JodaDateTimeFormatAnnotationFormatterFactory } - private StringValueResolver embeddedValueResolver; - - - @Override - public void setEmbeddedValueResolver(StringValueResolver resolver) { - this.embeddedValueResolver = resolver; - } - - protected String resolveEmbeddedValue(String value) { - return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); - } - - @Override public final Set> getFieldTypes() { return FIELD_TYPES; diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java index de02cb390d..62729c4a45 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -28,25 +28,25 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; -import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.context.support.EmbeddedValueResolutionSupport; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.Parser; import org.springframework.format.Printer; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.util.StringValueResolver; /** - * Formats fields annotated with the {@link DateTimeFormat} annotation using the JSR-310 - * java.time package in JDK 8. + * Formats fields annotated with the {@link DateTimeFormat} annotation using the + * JSR-310 java.time package in JDK 8. * * @author Juergen Hoeller * @since 4.0 * @see org.springframework.format.annotation.DateTimeFormat */ -public class Jsr310DateTimeFormatAnnotationFormatterFactory - implements AnnotationFormatterFactory, EmbeddedValueResolverAware { +public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport + implements AnnotationFormatterFactory { private static final Set> FIELD_TYPES; + static { // Create the set of field types that may be annotated with @DateTimeFormat. Set> fieldTypes = new HashSet>(8); @@ -60,19 +60,6 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory } - private StringValueResolver embeddedValueResolver; - - - @Override - public void setEmbeddedValueResolver(StringValueResolver resolver) { - this.embeddedValueResolver = resolver; - } - - protected String resolveEmbeddedValue(String value) { - return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); - } - - @Override public final Set> getFieldTypes() { return FIELD_TYPES; diff --git a/spring-context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java b/spring-context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java index a20cf4120a..33e40386b7 100644 --- a/spring-context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java +++ b/spring-context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -16,65 +16,34 @@ package org.springframework.format.number; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Collections; -import java.util.HashSet; import java.util.Set; -import org.springframework.context.EmbeddedValueResolverAware; +import org.springframework.context.support.EmbeddedValueResolutionSupport; import org.springframework.format.AnnotationFormatterFactory; import org.springframework.format.Formatter; import org.springframework.format.Parser; import org.springframework.format.Printer; import org.springframework.format.annotation.NumberFormat; import org.springframework.format.annotation.NumberFormat.Style; +import org.springframework.util.NumberUtils; import org.springframework.util.StringUtils; -import org.springframework.util.StringValueResolver; /** * Formats fields annotated with the {@link NumberFormat} annotation. * * @author Keith Donald + * @author Juergen Hoeller * @since 3.0 * @see NumberFormat */ -public class NumberFormatAnnotationFormatterFactory - implements AnnotationFormatterFactory, EmbeddedValueResolverAware { - - private final Set> fieldTypes; - - private StringValueResolver embeddedValueResolver; - - - public NumberFormatAnnotationFormatterFactory() { - Set> rawFieldTypes = new HashSet>(7); - rawFieldTypes.add(Short.class); - rawFieldTypes.add(Integer.class); - rawFieldTypes.add(Long.class); - rawFieldTypes.add(Float.class); - rawFieldTypes.add(Double.class); - rawFieldTypes.add(BigDecimal.class); - rawFieldTypes.add(BigInteger.class); - this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes); - } +public class NumberFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport + implements AnnotationFormatterFactory { @Override - public final Set> getFieldTypes() { - return this.fieldTypes; + public Set> getFieldTypes() { + return NumberUtils.STANDARD_NUMBER_TYPES; } - - @Override - public void setEmbeddedValueResolver(StringValueResolver resolver) { - this.embeddedValueResolver = resolver; - } - - protected String resolveEmbeddedValue(String value) { - return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); - } - - @Override public Printer getPrinter(NumberFormat annotation, Class fieldType) { return configureFormatterFrom(annotation);