001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.format.datetime;
018
019import java.util.Calendar;
020import java.util.Collections;
021import java.util.Date;
022import java.util.HashSet;
023import java.util.Set;
024
025import org.springframework.context.support.EmbeddedValueResolutionSupport;
026import org.springframework.format.AnnotationFormatterFactory;
027import org.springframework.format.Formatter;
028import org.springframework.format.Parser;
029import org.springframework.format.Printer;
030import org.springframework.format.annotation.DateTimeFormat;
031
032/**
033 * Formats fields annotated with the {@link DateTimeFormat} annotation using a {@link DateFormatter}.
034 *
035 * @author Phillip Webb
036 * @since 3.2
037 * @see org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory
038 */
039public class DateTimeFormatAnnotationFormatterFactory  extends EmbeddedValueResolutionSupport
040                implements AnnotationFormatterFactory<DateTimeFormat> {
041
042        private static final Set<Class<?>> FIELD_TYPES;
043
044        static {
045                Set<Class<?>> fieldTypes = new HashSet<Class<?>>(4);
046                fieldTypes.add(Date.class);
047                fieldTypes.add(Calendar.class);
048                fieldTypes.add(Long.class);
049                FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
050        }
051
052
053        @Override
054        public Set<Class<?>> getFieldTypes() {
055                return FIELD_TYPES;
056        }
057
058        @Override
059        public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
060                return getFormatter(annotation, fieldType);
061        }
062
063        @Override
064        public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
065                return getFormatter(annotation, fieldType);
066        }
067
068        protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
069                DateFormatter formatter = new DateFormatter();
070                formatter.setStylePattern(resolveEmbeddedValue(annotation.style()));
071                formatter.setIso(annotation.iso());
072                formatter.setPattern(resolveEmbeddedValue(annotation.pattern()));
073                return formatter;
074        }
075
076}