001/*
002 * Copyright 2002-2013 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.expression;
018
019import org.springframework.core.convert.TypeDescriptor;
020
021/**
022 * A type converter can convert values between different types encountered during
023 * expression evaluation. This is an SPI for the expression parser; see
024 * {@link org.springframework.core.convert.ConversionService} for the primary
025 * user API to Spring's conversion facilities.
026 *
027 * @author Andy Clement
028 * @author Juergen Hoeller
029 * @since 3.0
030 */
031public interface TypeConverter {
032
033        /**
034         * Return {@code true} if the type converter can convert the specified type
035         * to the desired target type.
036         * @param sourceType a type descriptor that describes the source type
037         * @param targetType a type descriptor that describes the requested result type
038         * @return {@code true} if that conversion can be performed
039         */
040        boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
041
042        /**
043         * Convert (or coerce) a value from one type to another, for example from a
044         * {@code boolean} to a {@code String}.
045         * <p>The {@link TypeDescriptor} parameters enable support for typed collections:
046         * A caller may prefer a {@code List&lt;Integer&gt;}, for example, rather than
047         * simply any {@code List}.
048         * @param value the value to be converted
049         * @param sourceType a type descriptor that supplies extra information about the
050         * source object
051         * @param targetType a type descriptor that supplies extra information about the
052         * requested result type
053         * @return the converted value
054         * @throws EvaluationException if conversion failed or is not possible to begin with
055         */
056        Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
057
058}