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