Skip to content

Commit d9f8ee8

Browse files
committed
Introduced EmbeddedValueResolutionSupport base class for AnnotationFormatterFactory implementations
1 parent 8f2ed66 commit d9f8ee8

File tree

5 files changed

+75
-94
lines changed

5 files changed

+75
-94
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.support;
18+
19+
import org.springframework.context.EmbeddedValueResolverAware;
20+
import org.springframework.util.StringValueResolver;
21+
22+
/**
23+
* Convenient base class for components with a need for embedded value resolution
24+
* (i.e. {@link org.springframework.context.EmbeddedValueResolverAware} consumers).
25+
*
26+
* @author Juergen Hoeller
27+
* @since 4.1
28+
*/
29+
public class EmbeddedValueResolutionSupport implements EmbeddedValueResolverAware {
30+
31+
private StringValueResolver embeddedValueResolver;
32+
33+
34+
@Override
35+
public void setEmbeddedValueResolver(StringValueResolver resolver) {
36+
this.embeddedValueResolver = resolver;
37+
}
38+
39+
/**
40+
* Resolve the given embedded value through this instance's {@link StringValueResolver}.
41+
* @param value the value to resolve
42+
* @return the resolved value, or always the original value if no resolver is available
43+
* @see #setEmbeddedValueResolver
44+
*/
45+
protected String resolveEmbeddedValue(String value) {
46+
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
47+
}
48+
49+
50+
}

spring-context/src/main/java/org/springframework/format/datetime/DateTimeFormatAnnotationFormatterFactory.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,13 +22,12 @@
2222
import java.util.HashSet;
2323
import java.util.Set;
2424

25-
import org.springframework.context.EmbeddedValueResolverAware;
25+
import org.springframework.context.support.EmbeddedValueResolutionSupport;
2626
import org.springframework.format.AnnotationFormatterFactory;
2727
import org.springframework.format.Formatter;
2828
import org.springframework.format.Parser;
2929
import org.springframework.format.Printer;
3030
import org.springframework.format.annotation.DateTimeFormat;
31-
import org.springframework.util.StringValueResolver;
3231

3332
/**
3433
* Formats fields annotated with the {@link DateTimeFormat} annotation using
@@ -38,11 +37,12 @@
3837
* @since 3.2
3938
* @see org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory
4039
*/
41-
public class DateTimeFormatAnnotationFormatterFactory implements
42-
AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
40+
public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
41+
implements AnnotationFormatterFactory<DateTimeFormat> {
4342

4443

4544
private static final Set<Class<?>> FIELD_TYPES;
45+
4646
static {
4747
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
4848
fieldTypes.add(Date.class);
@@ -52,14 +52,6 @@ public class DateTimeFormatAnnotationFormatterFactory implements
5252
}
5353

5454

55-
private StringValueResolver embeddedValueResolver;
56-
57-
58-
@Override
59-
public void setEmbeddedValueResolver(StringValueResolver resolver) {
60-
this.embeddedValueResolver = resolver;
61-
}
62-
6355
@Override
6456
public Set<Class<?>> getFieldTypes() {
6557
return FIELD_TYPES;
@@ -83,8 +75,4 @@ protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> field
8375
return formatter;
8476
}
8577

86-
protected String resolveEmbeddedValue(String value) {
87-
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
88-
}
89-
9078
}

spring-context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,12 +29,11 @@
2929
import org.joda.time.ReadablePartial;
3030
import org.joda.time.format.DateTimeFormatter;
3131

32-
import org.springframework.context.EmbeddedValueResolverAware;
32+
import org.springframework.context.support.EmbeddedValueResolutionSupport;
3333
import org.springframework.format.AnnotationFormatterFactory;
3434
import org.springframework.format.Parser;
3535
import org.springframework.format.Printer;
3636
import org.springframework.format.annotation.DateTimeFormat;
37-
import org.springframework.util.StringValueResolver;
3837

3938
/**
4039
* Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time.
@@ -46,10 +45,11 @@
4645
* @since 3.0
4746
* @see DateTimeFormat
4847
*/
49-
public class JodaDateTimeFormatAnnotationFormatterFactory
50-
implements AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
48+
public class JodaDateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
49+
implements AnnotationFormatterFactory<DateTimeFormat> {
5150

5251
private static final Set<Class<?>> FIELD_TYPES;
52+
5353
static {
5454
// Create the set of field types that may be annotated with @DateTimeFormat.
5555
// Note: the 3 ReadablePartial concrete types are registered explicitly since
@@ -69,19 +69,6 @@ public class JodaDateTimeFormatAnnotationFormatterFactory
6969
}
7070

7171

72-
private StringValueResolver embeddedValueResolver;
73-
74-
75-
@Override
76-
public void setEmbeddedValueResolver(StringValueResolver resolver) {
77-
this.embeddedValueResolver = resolver;
78-
}
79-
80-
protected String resolveEmbeddedValue(String value) {
81-
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
82-
}
83-
84-
8572
@Override
8673
public final Set<Class<?>> getFieldTypes() {
8774
return FIELD_TYPES;

spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,25 +28,25 @@
2828
import java.util.HashSet;
2929
import java.util.Set;
3030

31-
import org.springframework.context.EmbeddedValueResolverAware;
31+
import org.springframework.context.support.EmbeddedValueResolutionSupport;
3232
import org.springframework.format.AnnotationFormatterFactory;
3333
import org.springframework.format.Parser;
3434
import org.springframework.format.Printer;
3535
import org.springframework.format.annotation.DateTimeFormat;
36-
import org.springframework.util.StringValueResolver;
3736

3837
/**
39-
* Formats fields annotated with the {@link DateTimeFormat} annotation using the JSR-310
40-
* <code>java.time</code> package in JDK 8.
38+
* Formats fields annotated with the {@link DateTimeFormat} annotation using the
39+
* JSR-310 <code>java.time</code> package in JDK 8.
4140
*
4241
* @author Juergen Hoeller
4342
* @since 4.0
4443
* @see org.springframework.format.annotation.DateTimeFormat
4544
*/
46-
public class Jsr310DateTimeFormatAnnotationFormatterFactory
47-
implements AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
45+
public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
46+
implements AnnotationFormatterFactory<DateTimeFormat> {
4847

4948
private static final Set<Class<?>> FIELD_TYPES;
49+
5050
static {
5151
// Create the set of field types that may be annotated with @DateTimeFormat.
5252
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(8);
@@ -60,19 +60,6 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory
6060
}
6161

6262

63-
private StringValueResolver embeddedValueResolver;
64-
65-
66-
@Override
67-
public void setEmbeddedValueResolver(StringValueResolver resolver) {
68-
this.embeddedValueResolver = resolver;
69-
}
70-
71-
protected String resolveEmbeddedValue(String value) {
72-
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
73-
}
74-
75-
7663
@Override
7764
public final Set<Class<?>> getFieldTypes() {
7865
return FIELD_TYPES;

spring-context/src/main/java/org/springframework/format/number/NumberFormatAnnotationFormatterFactory.java

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,65 +16,34 @@
1616

1717
package org.springframework.format.number;
1818

19-
import java.math.BigDecimal;
20-
import java.math.BigInteger;
21-
import java.util.Collections;
22-
import java.util.HashSet;
2319
import java.util.Set;
2420

25-
import org.springframework.context.EmbeddedValueResolverAware;
21+
import org.springframework.context.support.EmbeddedValueResolutionSupport;
2622
import org.springframework.format.AnnotationFormatterFactory;
2723
import org.springframework.format.Formatter;
2824
import org.springframework.format.Parser;
2925
import org.springframework.format.Printer;
3026
import org.springframework.format.annotation.NumberFormat;
3127
import org.springframework.format.annotation.NumberFormat.Style;
28+
import org.springframework.util.NumberUtils;
3229
import org.springframework.util.StringUtils;
33-
import org.springframework.util.StringValueResolver;
3430

3531
/**
3632
* Formats fields annotated with the {@link NumberFormat} annotation.
3733
*
3834
* @author Keith Donald
35+
* @author Juergen Hoeller
3936
* @since 3.0
4037
* @see NumberFormat
4138
*/
42-
public class NumberFormatAnnotationFormatterFactory
43-
implements AnnotationFormatterFactory<NumberFormat>, EmbeddedValueResolverAware {
44-
45-
private final Set<Class<?>> fieldTypes;
46-
47-
private StringValueResolver embeddedValueResolver;
48-
49-
50-
public NumberFormatAnnotationFormatterFactory() {
51-
Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(7);
52-
rawFieldTypes.add(Short.class);
53-
rawFieldTypes.add(Integer.class);
54-
rawFieldTypes.add(Long.class);
55-
rawFieldTypes.add(Float.class);
56-
rawFieldTypes.add(Double.class);
57-
rawFieldTypes.add(BigDecimal.class);
58-
rawFieldTypes.add(BigInteger.class);
59-
this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes);
60-
}
61-
62-
@Override
63-
public final Set<Class<?>> getFieldTypes() {
64-
return this.fieldTypes;
65-
}
66-
39+
public class NumberFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
40+
implements AnnotationFormatterFactory<NumberFormat> {
6741

6842
@Override
69-
public void setEmbeddedValueResolver(StringValueResolver resolver) {
70-
this.embeddedValueResolver = resolver;
43+
public Set<Class<?>> getFieldTypes() {
44+
return NumberUtils.STANDARD_NUMBER_TYPES;
7145
}
7246

73-
protected String resolveEmbeddedValue(String value) {
74-
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
75-
}
76-
77-
7847
@Override
7948
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
8049
return configureFormatterFrom(annotation);

0 commit comments

Comments
 (0)