Refactor DateTimeFormatterFactory
Refactor DateTimeFormatterFactory into two distinct classes; a general purpose factory and a specialized FactoryBean. These changes are modeled after the existing VelocityEngineFactory and VelocityEngineFactoryBean classes. Issue: SPR-9959
This commit is contained in:
@@ -22,23 +22,25 @@ import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} that creates a Joda {@link DateTimeFormatter}. Formatters will be
|
||||
* Factory that creates a Joda {@link DateTimeFormatter}. Formatters will be
|
||||
* created using the defined {@link #setPattern(String) pattern}, {@link #setIso(ISO) ISO},
|
||||
* or {@link #setStyle(String) style} (considered in that order).
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @see #getDateTimeFormatter()
|
||||
* @see #getDateTimeFormatter(DateTimeFormatter)
|
||||
* @see #createDateTimeFormatter()
|
||||
* @see #createDateTimeFormatter(DateTimeFormatter)
|
||||
* @see #setPattern(String)
|
||||
* @see #setIso(org.springframework.format.annotation.DateTimeFormat.ISO)
|
||||
* @see #setStyle(String)
|
||||
* @see DateTimeFormatterFactoryBean
|
||||
* @since 3.2
|
||||
*/
|
||||
public class DateTimeFormatterFactory implements FactoryBean<DateTimeFormatter> {
|
||||
public class DateTimeFormatterFactory {
|
||||
|
||||
private ISO iso;
|
||||
|
||||
@@ -64,33 +66,21 @@ public class DateTimeFormatterFactory implements FactoryBean<DateTimeFormatter>
|
||||
}
|
||||
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
return DateTimeFormatter.class;
|
||||
}
|
||||
|
||||
public DateTimeFormatter getObject() throws Exception {
|
||||
return getDateTimeFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new {@code DateTimeFormatter} using this factory. If no specific
|
||||
* Create a new {@code DateTimeFormatter} using this factory. If no specific
|
||||
* {@link #setStyle(String) style}, {@link #setIso(ISO) ISO}, or
|
||||
* {@link #setPattern(String) pattern} have been defined the
|
||||
* {@link DateTimeFormat#mediumDateTime() medium date time format} will be used.
|
||||
* @return a new date time formatter
|
||||
* @see #getObject()
|
||||
* @see #getDateTimeFormatter(DateTimeFormatter)
|
||||
* @see #createDateTimeFormatter(DateTimeFormatter)
|
||||
*/
|
||||
public DateTimeFormatter getDateTimeFormatter() {
|
||||
return getDateTimeFormatter(DateTimeFormat.mediumDateTime());
|
||||
public DateTimeFormatter createDateTimeFormatter() {
|
||||
return createDateTimeFormatter(DateTimeFormat.mediumDateTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a new {@code DateTimeFormatter} using this factory. If no specific
|
||||
* Create a new {@code DateTimeFormatter} using this factory. If no specific
|
||||
* {@link #setStyle(String) style}, {@link #setIso(ISO) ISO}, or
|
||||
* {@link #setPattern(String) pattern} have been defined the supplied
|
||||
* {@code fallbackFormatter} will be used.
|
||||
@@ -98,34 +88,32 @@ public class DateTimeFormatterFactory implements FactoryBean<DateTimeFormatter>
|
||||
* properties have been set (can be {@code null}).
|
||||
* @return a new date time formatter
|
||||
*/
|
||||
public DateTimeFormatter getDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
|
||||
DateTimeFormatter dateTimeFormatter = createDateTimeFormatter();
|
||||
if(dateTimeFormatter != null && this.timeZone != null) {
|
||||
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
|
||||
DateTimeFormatter dateTimeFormatter = null;
|
||||
if (StringUtils.hasLength(pattern)) {
|
||||
dateTimeFormatter = DateTimeFormat.forPattern(pattern);
|
||||
}
|
||||
else if (iso != null && iso != ISO.NONE) {
|
||||
if (iso == ISO.DATE) {
|
||||
dateTimeFormatter = ISODateTimeFormat.date();
|
||||
}
|
||||
else if (iso == ISO.TIME) {
|
||||
dateTimeFormatter = ISODateTimeFormat.time();
|
||||
}
|
||||
else {
|
||||
dateTimeFormatter = ISODateTimeFormat.dateTime();
|
||||
}
|
||||
}
|
||||
else if (StringUtils.hasLength(style)) {
|
||||
dateTimeFormatter = DateTimeFormat.forStyle(style);
|
||||
}
|
||||
|
||||
if (dateTimeFormatter != null && this.timeZone != null) {
|
||||
dateTimeFormatter = dateTimeFormatter.withZone(DateTimeZone.forTimeZone(this.timeZone));
|
||||
}
|
||||
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
|
||||
}
|
||||
|
||||
private DateTimeFormatter createDateTimeFormatter() {
|
||||
if (StringUtils.hasLength(pattern)) {
|
||||
return DateTimeFormat.forPattern(pattern);
|
||||
}
|
||||
if (iso != null && iso != ISO.NONE) {
|
||||
if (iso == ISO.DATE) {
|
||||
return ISODateTimeFormat.date();
|
||||
}
|
||||
if (iso == ISO.TIME) {
|
||||
return ISODateTimeFormat.time();
|
||||
}
|
||||
return ISODateTimeFormat.dateTime();
|
||||
}
|
||||
if (StringUtils.hasLength(style)) {
|
||||
return DateTimeFormat.forStyle(style);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the {@code TimeZone} to normalize the date values into, if any.
|
||||
* @param timeZone the time zone
|
||||
@@ -166,5 +154,4 @@ public class DateTimeFormatterFactory implements FactoryBean<DateTimeFormatter>
|
||||
public void setPattern(String pattern) {
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.format.datetime.joda;
|
||||
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
/**
|
||||
* {@link FactoryBean} that creates a Joda {@link DateTimeFormatter}. See the base class
|
||||
* {@linkplain DateTimeFormatterFactory} for configuration details.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Sam Brannen
|
||||
* @see #setPattern(String)
|
||||
* @see #setIso(org.springframework.format.annotation.DateTimeFormat.ISO)
|
||||
* @see #setStyle(String)
|
||||
* @see DateTimeFormatterFactory
|
||||
* @since 3.2
|
||||
*/
|
||||
public class DateTimeFormatterFactoryBean extends DateTimeFormatterFactory implements
|
||||
FactoryBean<DateTimeFormatter>, InitializingBean {
|
||||
|
||||
private DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.dateTimeFormatter = createDateTimeFormatter();
|
||||
}
|
||||
|
||||
public DateTimeFormatter getObject() throws Exception {
|
||||
return this.dateTimeFormatter;
|
||||
}
|
||||
|
||||
public Class<?> getObjectType() {
|
||||
return DateTimeFormatter.class;
|
||||
}
|
||||
|
||||
public boolean isSingleton() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -119,6 +119,6 @@ public class JodaDateTimeFormatAnnotationFormatterFactory
|
||||
factory.setStyle(resolveEmbeddedValue(annotation.style()));
|
||||
factory.setIso(annotation.iso());
|
||||
factory.setPattern(resolveEmbeddedValue(annotation.pattern()));
|
||||
return factory.getDateTimeFormatter();
|
||||
return factory.createDateTimeFormatter();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
* @see #setUseIsoFormat
|
||||
* @see FormatterRegistrar#registerFormatters
|
||||
* @see org.springframework.format.datetime.DateFormatterRegistrar
|
||||
* @see DateTimeFormatterFactoryBean
|
||||
*/
|
||||
public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
|
||||
|
||||
@@ -185,7 +186,7 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
|
||||
return formatter;
|
||||
}
|
||||
DateTimeFormatter fallbackFormatter = getFallbackFormatter(type);
|
||||
return factories.get(type).getDateTimeFormatter(fallbackFormatter );
|
||||
return factories.get(type).createDateTimeFormatter(fallbackFormatter );
|
||||
}
|
||||
|
||||
private DateTimeFormatter getFallbackFormatter(Type type) {
|
||||
|
||||
Reference in New Issue
Block a user