001// Copyright 2007, 2008, 2010 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
015package org.apache.tapestry5.services;
016
017import org.apache.tapestry5.Translator;
018import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
019import org.apache.tapestry5.ioc.util.StrategyRegistry;
020
021/**
022 * A source for {@link org.apache.tapestry5.Translator}s, either by name or by property type. The source knows
023 * about two sets of translators: the <em>standard</em> translators contributed directly to the service
024 * and the <em>alternate</em> translators, contributed to the {@link TranslatorAlternatesSource} service.
025 * <p/>
026 * Each contributed translator must have a unique {@linkplain org.apache.tapestry5.Translator#getName() name}.
027 * <p>
028 * Generally, Translators are matched by type (i.e., the type matching a particular property that will be read or
029 * updated). Contributions to this service use a {@link StrategyRegistry} to match by type. Translators can also be
030 * selected by name. The {@link TranslatorAlternatesSource} service configuration is often used for this purpose.
031 * <p>
032 * The contribution key must match the {@linkplain Translator#getType() translator type}.
033 */
034@UsesMappedConfiguration(key=Class.class, value=Translator.class)
035@SuppressWarnings("unchecked")
036public interface TranslatorSource
037{
038    /**
039     * Returns the translator with the given name (either a standard translator, or an alternate).
040     * 
041     * @param name
042     *            name of translator (as configured, but case is ignored)
043     * @return the shared translator instance
044     * @throws RuntimeException
045     *             if no translator is configured for the provided name
046     */
047    Translator get(String name);
048
049    /**
050     * Finds a {@link Translator} that is appropriate to the given type, which is usually obtained via
051     * {@link org.apache.tapestry5.Binding#getBindingType()}. Performs an inheritance-based search for the best match,
052     * among the <em>standard</em> translator (not alternates).
053     * 
054     * @param valueType
055     *            the type of value for which a default translator is needed
056     * @return the matching translator, or null if no match can be found
057     */
058    Translator findByType(Class valueType);
059
060    /**
061     * Finds a {@link Translator} that is appropriate to the given type, which is usually obtained via
062     * {@link org.apache.tapestry5.Binding#getBindingType()}. Performs an inheritance-based search for the best match,
063     * among the <em>standard</em> translators (not alternates).
064     * 
065     * @param valueType
066     *            the type of value for which a default translator is needed
067     * @return the matching translator
068     * @throws IllegalArgumentException
069     *             if no standard validator matches the provided type
070     */
071    Translator getByType(Class valueType);
072}