提交 d9f8ee88 编写于 作者: J Juergen Hoeller

Introduced EmbeddedValueResolutionSupport base class for AnnotationFormatterFactory implementations

上级 8f2ed66b
/*
* 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);
}
}
/*
* 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<DateTimeFormat>, EmbeddedValueResolverAware {
public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
implements AnnotationFormatterFactory<DateTimeFormat> {
private static final Set<Class<?>> FIELD_TYPES;
static {
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(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<Class<?>> 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);
}
}
/*
* 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<DateTimeFormat>, EmbeddedValueResolverAware {
public class JodaDateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
implements AnnotationFormatterFactory<DateTimeFormat> {
private static final Set<Class<?>> 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<Class<?>> getFieldTypes() {
return FIELD_TYPES;
......
/*
* 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
* <code>java.time</code> package in JDK 8.
* Formats fields annotated with the {@link DateTimeFormat} annotation using the
* JSR-310 <code>java.time</code> package in JDK 8.
*
* @author Juergen Hoeller
* @since 4.0
* @see org.springframework.format.annotation.DateTimeFormat
*/
public class Jsr310DateTimeFormatAnnotationFormatterFactory
implements AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
implements AnnotationFormatterFactory<DateTimeFormat> {
private static final Set<Class<?>> FIELD_TYPES;
static {
// Create the set of field types that may be annotated with @DateTimeFormat.
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(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<Class<?>> getFieldTypes() {
return FIELD_TYPES;
......
/*
* 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<NumberFormat>, EmbeddedValueResolverAware {
private final Set<Class<?>> fieldTypes;
private StringValueResolver embeddedValueResolver;
public NumberFormatAnnotationFormatterFactory() {
Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(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);
}
@Override
public final Set<Class<?>> getFieldTypes() {
return this.fieldTypes;
}
public class NumberFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
implements AnnotationFormatterFactory<NumberFormat> {
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.embeddedValueResolver = resolver;
public Set<Class<?>> getFieldTypes() {
return NumberUtils.STANDARD_NUMBER_TYPES;
}
protected String resolveEmbeddedValue(String value) {
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
}
@Override
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
return configureFormatterFrom(annotation);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册