001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.services;
014
015import org.apache.tapestry5.Translator;
016import org.apache.tapestry5.commons.util.StrategyRegistry;
017import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
018
019/**
020 * A source for {@link org.apache.tapestry5.Translator}s, either by name or by property type. The source knows
021 * about two sets of translators: the <em>standard</em> translators contributed directly to the service
022 * and the <em>alternate</em> translators, contributed to the {@link TranslatorAlternatesSource} service.
023 *
024 * Each contributed translator must have a unique {@linkplain org.apache.tapestry5.Translator#getName() name}.
025 *
026 * Generally, Translators are matched by type (i.e., the type matching a particular property that will be read or
027 * updated). Contributions to this service use a {@link StrategyRegistry} to match by type. Translators can also be
028 * selected by name. The {@link TranslatorAlternatesSource} service configuration is often used for this purpose.
029 *
030 * The contribution key must match the {@linkplain Translator#getType() translator type}.
031 */
032@UsesMappedConfiguration(key=Class.class, value=Translator.class)
033@SuppressWarnings("unchecked")
034public interface TranslatorSource
035{
036    /**
037     * Returns the translator with the given name (either a standard translator, or an alternate).
038     * 
039     * @param name
040     *            name of translator (as configured, but case is ignored)
041     * @return the shared translator instance
042     * @throws RuntimeException
043     *             if no translator is configured for the provided name
044     */
045    Translator get(String name);
046
047    /**
048     * Finds a {@link Translator} that is appropriate to the given type, which is usually obtained via
049     * {@link org.apache.tapestry5.Binding#getBindingType()}. Performs an inheritance-based search for the best match,
050     * among the <em>standard</em> translator (not alternates).
051     * 
052     * @param valueType
053     *            the type of value for which a default translator is needed
054     * @return the matching translator, or null if no match can be found
055     */
056    Translator findByType(Class valueType);
057
058    /**
059     * Finds a {@link Translator} that is appropriate to the given type, which is usually obtained via
060     * {@link org.apache.tapestry5.Binding#getBindingType()}. Performs an inheritance-based search for the best match,
061     * among the <em>standard</em> translators (not alternates).
062     * 
063     * @param valueType
064     *            the type of value for which a default translator is needed
065     * @return the matching translator
066     * @throws IllegalArgumentException
067     *             if no standard validator matches the provided type
068     */
069    Translator getByType(Class valueType);
070}