View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.expression;
18  
19  import org.springframework.core.convert.TypeDescriptor;
20  
21  /**
22   * A type converter can convert values between different types encountered during
23   * expression evaluation. This is an SPI for the expression parser; see
24   * {@link org.springframework.core.convert.ConversionService} for the primary
25   * user API to Spring's conversion facilities.
26   *
27   * @author Andy Clement
28   * @author Juergen Hoeller
29   * @since 3.0
30   */
31  public interface TypeConverter {
32  
33  	/**
34  	 * Return {@code true} if the type converter can convert the specified type
35  	 * to the desired target type.
36  	 * @param sourceType a type descriptor that describes the source type
37  	 * @param targetType a type descriptor that describes the requested result type
38  	 * @return {@code true} if that conversion can be performed
39  	 */
40  	boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType);
41  
42  	/**
43  	 * Convert (or coerce) a value from one type to another, for example from a
44  	 * {@code boolean} to a {@code String}.
45  	 * <p>The {@link TypeDescriptor} parameters enable support for typed collections:
46  	 * A caller may prefer a {@code List&lt;Integer&gt;}, for example, rather than
47  	 * simply any {@code List}.
48  	 * @param value the value to be converted
49  	 * @param sourceType a type descriptor that supplies extra information about the
50  	 * source object
51  	 * @param targetType a type descriptor that supplies extra information about the
52  	 * requested result type
53  	 * @return the converted value
54  	 * @throws EvaluationException if conversion failed or is not possible to begin with
55  	 */
56  	Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType);
57  
58  }