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.core.convert.converter; 018 019import org.springframework.core.convert.TypeDescriptor; 020 021/** 022 * Allows a {@link Converter}, {@link GenericConverter} or {@link ConverterFactory} to 023 * conditionally execute based on attributes of the {@code source} and {@code target} 024 * {@link TypeDescriptor}. 025 * 026 * <p>Often used to selectively match custom conversion logic based on the presence of a 027 * field or class-level characteristic, such as an annotation or method. For example, when 028 * converting from a String field to a Date field, an implementation might return 029 * {@code true} if the target field has also been annotated with {@code @DateTimeFormat}. 030 * 031 * <p>As another example, when converting from a String field to an {@code Account} field, 032 * an implementation might return {@code true} if the target Account class defines a 033 * {@code public static findAccount(String)} method. 034 * 035 * @author Phillip Webb 036 * @author Keith Donald 037 * @since 3.2 038 * @see Converter 039 * @see GenericConverter 040 * @see ConverterFactory 041 * @see ConditionalGenericConverter 042 */ 043public interface ConditionalConverter { 044 045 /** 046 * Should the conversion from {@code sourceType} to {@code targetType} currently under 047 * consideration be selected? 048 * @param sourceType the type descriptor of the field we are converting from 049 * @param targetType the type descriptor of the field we are converting to 050 * @return true if conversion should be performed, false otherwise 051 */ 052 boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType); 053 054}