001 // Copyright 2006, 2007, 2008 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.ioc.services;
016
017 import org.apache.tapestry5.ioc.annotations.UsesConfiguration;
018
019 /**
020 * Makes use of {@link org.apache.tapestry5.ioc.services.Coercion}s to convert between an input value (of some specific
021 * type) and a desired output type. Smart about coercing, even if it requires multiple coercion steps (i.e., via an
022 * intermediate type, such as String).
023 */
024 @UsesConfiguration(CoercionTuple.class)
025 public interface TypeCoercer
026 {
027 /**
028 * Performs a coercion from an input type to a desired output type. When the target type is a primitive, the actual
029 * conversion will be to the equivalent wrapper type. In some cases, the TypeCoercer will need to search for an
030 * appropriate coercion, and may even combine existing coercions to form new ones; in those cases, the results of
031 * the search are cached.
032 * <p/>
033 * <p/>
034 * The TypeCoercer also caches the results of a coercion search.
035 *
036 * @param <S> source type (input)
037 * @param <T> target type (output)
038 * @param input
039 * @param targetType defines the target type
040 * @return the coerced value
041 */
042 <S, T> T coerce(S input, Class<T> targetType);
043
044 /**
045 * Used primarily inside test suites, this method performs the same steps as {@link #coerce(Object, Class)}, but
046 * returns a string describing the series of coercision, such as "Object --> String --> Long --> Integer".
047 *
048 * @param <S> source type (input)
049 * @param <T> target type (output)
050 * @param inputType the source coercion type (use void.class for coercions from null)
051 * @param targetType defines the target type
052 * @return a string identifying the series of coercions, or the empty string if no coercion is necessary
053 */
054 <S, T> String explain(Class<S> inputType, Class<T> targetType);
055
056 /**
057 * Clears cached information stored by the TypeCoercer.
058 */
059 void clearCache();
060 }