Introduced EmbeddedValueResolutionSupport base class for AnnotationFormatterFactory implementations
This commit is contained in:
parent
8f2ed66b4a
commit
d9f8ee886e
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.context.EmbeddedValueResolverAware;
|
import org.springframework.context.support.EmbeddedValueResolutionSupport;
|
||||||
import org.springframework.format.AnnotationFormatterFactory;
|
import org.springframework.format.AnnotationFormatterFactory;
|
||||||
import org.springframework.format.Formatter;
|
import org.springframework.format.Formatter;
|
||||||
import org.springframework.format.Parser;
|
import org.springframework.format.Parser;
|
||||||
import org.springframework.format.Printer;
|
import org.springframework.format.Printer;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.util.StringValueResolver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats fields annotated with the {@link DateTimeFormat} annotation using
|
* Formats fields annotated with the {@link DateTimeFormat} annotation using
|
||||||
|
@ -38,11 +37,12 @@ import org.springframework.util.StringValueResolver;
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
* @see org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory
|
* @see org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory
|
||||||
*/
|
*/
|
||||||
public class DateTimeFormatAnnotationFormatterFactory implements
|
public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
|
||||||
AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
|
implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||||
|
|
||||||
|
|
||||||
private static final Set<Class<?>> FIELD_TYPES;
|
private static final Set<Class<?>> FIELD_TYPES;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
|
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
|
||||||
fieldTypes.add(Date.class);
|
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
|
@Override
|
||||||
public Set<Class<?>> getFieldTypes() {
|
public Set<Class<?>> getFieldTypes() {
|
||||||
return FIELD_TYPES;
|
return FIELD_TYPES;
|
||||||
|
@ -83,8 +75,4 @@ public class DateTimeFormatAnnotationFormatterFactory implements
|
||||||
return formatter;
|
return formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String resolveEmbeddedValue(String value) {
|
|
||||||
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.ReadablePartial;
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
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.AnnotationFormatterFactory;
|
||||||
import org.springframework.format.Parser;
|
import org.springframework.format.Parser;
|
||||||
import org.springframework.format.Printer;
|
import org.springframework.format.Printer;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.util.StringValueResolver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time.
|
* Formats fields annotated with the {@link DateTimeFormat} annotation using Joda-Time.
|
||||||
|
@ -46,10 +45,11 @@ import org.springframework.util.StringValueResolver;
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see DateTimeFormat
|
* @see DateTimeFormat
|
||||||
*/
|
*/
|
||||||
public class JodaDateTimeFormatAnnotationFormatterFactory
|
public class JodaDateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
|
||||||
implements AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
|
implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||||
|
|
||||||
private static final Set<Class<?>> FIELD_TYPES;
|
private static final Set<Class<?>> FIELD_TYPES;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Create the set of field types that may be annotated with @DateTimeFormat.
|
// Create the set of field types that may be annotated with @DateTimeFormat.
|
||||||
// Note: the 3 ReadablePartial concrete types are registered explicitly since
|
// 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
|
@Override
|
||||||
public final Set<Class<?>> getFieldTypes() {
|
public final Set<Class<?>> getFieldTypes() {
|
||||||
return FIELD_TYPES;
|
return FIELD_TYPES;
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.context.EmbeddedValueResolverAware;
|
import org.springframework.context.support.EmbeddedValueResolutionSupport;
|
||||||
import org.springframework.format.AnnotationFormatterFactory;
|
import org.springframework.format.AnnotationFormatterFactory;
|
||||||
import org.springframework.format.Parser;
|
import org.springframework.format.Parser;
|
||||||
import org.springframework.format.Printer;
|
import org.springframework.format.Printer;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import org.springframework.util.StringValueResolver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats fields annotated with the {@link DateTimeFormat} annotation using the JSR-310
|
* Formats fields annotated with the {@link DateTimeFormat} annotation using the
|
||||||
* <code>java.time</code> package in JDK 8.
|
* JSR-310 <code>java.time</code> package in JDK 8.
|
||||||
*
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @since 4.0
|
* @since 4.0
|
||||||
* @see org.springframework.format.annotation.DateTimeFormat
|
* @see org.springframework.format.annotation.DateTimeFormat
|
||||||
*/
|
*/
|
||||||
public class Jsr310DateTimeFormatAnnotationFormatterFactory
|
public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
|
||||||
implements AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware {
|
implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||||
|
|
||||||
private static final Set<Class<?>> FIELD_TYPES;
|
private static final Set<Class<?>> FIELD_TYPES;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Create the set of field types that may be annotated with @DateTimeFormat.
|
// Create the set of field types that may be annotated with @DateTimeFormat.
|
||||||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(8);
|
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
|
@Override
|
||||||
public final Set<Class<?>> getFieldTypes() {
|
public final Set<Class<?>> getFieldTypes() {
|
||||||
return FIELD_TYPES;
|
return FIELD_TYPES;
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,65 +16,34 @@
|
||||||
|
|
||||||
package org.springframework.format.number;
|
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 java.util.Set;
|
||||||
|
|
||||||
import org.springframework.context.EmbeddedValueResolverAware;
|
import org.springframework.context.support.EmbeddedValueResolutionSupport;
|
||||||
import org.springframework.format.AnnotationFormatterFactory;
|
import org.springframework.format.AnnotationFormatterFactory;
|
||||||
import org.springframework.format.Formatter;
|
import org.springframework.format.Formatter;
|
||||||
import org.springframework.format.Parser;
|
import org.springframework.format.Parser;
|
||||||
import org.springframework.format.Printer;
|
import org.springframework.format.Printer;
|
||||||
import org.springframework.format.annotation.NumberFormat;
|
import org.springframework.format.annotation.NumberFormat;
|
||||||
import org.springframework.format.annotation.NumberFormat.Style;
|
import org.springframework.format.annotation.NumberFormat.Style;
|
||||||
|
import org.springframework.util.NumberUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.util.StringValueResolver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats fields annotated with the {@link NumberFormat} annotation.
|
* Formats fields annotated with the {@link NumberFormat} annotation.
|
||||||
*
|
*
|
||||||
* @author Keith Donald
|
* @author Keith Donald
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
* @see NumberFormat
|
* @see NumberFormat
|
||||||
*/
|
*/
|
||||||
public class NumberFormatAnnotationFormatterFactory
|
public class NumberFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
|
||||||
implements AnnotationFormatterFactory<NumberFormat>, EmbeddedValueResolverAware {
|
implements AnnotationFormatterFactory<NumberFormat> {
|
||||||
|
|
||||||
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
|
@Override
|
||||||
public final Set<Class<?>> getFieldTypes() {
|
public Set<Class<?>> getFieldTypes() {
|
||||||
return this.fieldTypes;
|
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
|
@Override
|
||||||
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
|
public Printer<Number> getPrinter(NumberFormat annotation, Class<?> fieldType) {
|
||||||
return configureFormatterFrom(annotation);
|
return configureFormatterFrom(annotation);
|
||||||
|
|
Loading…
Reference in New Issue