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; 018 019import java.lang.annotation.Annotation; 020import java.util.Set; 021 022/** 023 * A factory that creates formatters to format values of fields annotated with a particular 024 * {@link Annotation}. 025 * 026 * <p>For example, a {@code DateTimeFormatAnnotationFormatterFactory} might create a formatter 027 * that formats {@code Date} values set on fields annotated with {@code @DateTimeFormat}. 028 * 029 * @author Keith Donald 030 * @since 3.0 031 * @param <A> the annotation type that should trigger formatting 032 */ 033public interface AnnotationFormatterFactory<A extends Annotation> { 034 035 /** 036 * The types of fields that may be annotated with the <A> annotation. 037 */ 038 Set<Class<?>> getFieldTypes(); 039 040 /** 041 * Get the Printer to print the value of a field of {@code fieldType} annotated with 042 * {@code annotation}. 043 * <p>If the type T the printer accepts is not assignable to {@code fieldType}, a 044 * coercion from {@code fieldType} to T will be attempted before the Printer is invoked. 045 * @param annotation the annotation instance 046 * @param fieldType the type of field that was annotated 047 * @return the printer 048 */ 049 Printer<?> getPrinter(A annotation, Class<?> fieldType); 050 051 /** 052 * Get the Parser to parse a submitted value for a field of {@code fieldType} 053 * annotated with {@code annotation}. 054 * <p>If the object the parser returns is not assignable to {@code fieldType}, 055 * a coercion to {@code fieldType} will be attempted before the field is set. 056 * @param annotation the annotation instance 057 * @param fieldType the type of field that was annotated 058 * @return the parser 059 */ 060 Parser<?> getParser(A annotation, Class<?> fieldType); 061 062}