001// Copyright 2007, 2008, 2010, 2011 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.util;
016
017import java.util.Map;
018
019import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020import org.apache.tapestry5.ioc.internal.util.InternalCommonsUtils;
021import org.apache.tapestry5.ioc.services.Coercion;
022import org.apache.tapestry5.ioc.util.AvailableValues;
023import org.apache.tapestry5.ioc.util.UnknownValueException;
024
025/**
026 * A {@link org.apache.tapestry5.ioc.services.Coercion} for converting strings into an instance of a particular
027 * enumerated type. The {@link Enum#name() name} is used as the key to identify the enum instance, in a case-insensitive
028 * fashion.
029 * <p>
030 * Moved from tapestry-core to tapestry-ioc in release 5.3, but kept in same package for compatibility.
031 * Moved tapestry-ioc to commons in release 5.4, but kept in same package for compatibility.
032 * 
033 * @param <T>
034 *            the type of enumeration
035 */
036public final class StringToEnumCoercion<T extends Enum> implements Coercion<String, T>
037{
038    private final Class<T> enumClass;
039
040    private final Map<String, T> stringToEnum = CollectionFactory.newCaseInsensitiveMap();
041
042    public StringToEnumCoercion(Class<T> enumClass)
043    {
044        this(enumClass, enumClass.getEnumConstants());
045    }
046
047    public StringToEnumCoercion(Class<T> enumClass, T... values)
048    {
049        this.enumClass = enumClass;
050
051        for (T value : values)
052            stringToEnum.put(value.name(), value);
053    }
054
055    @Override
056    public T coerce(String input)
057    {
058        if (InternalCommonsUtils.isBlank(input))
059            return null;
060
061        T result = stringToEnum.get(input);
062
063        if (result == null)
064        {
065            String message = String.format("Input '%s' does not identify a value from enumerated type %s.", input,
066                    enumClass.getName());
067
068            throw new UnknownValueException(message, new AvailableValues(enumClass.getName() + " enum constants",
069                    stringToEnum));
070        }
071
072        return result;
073    }
074
075    /**
076     * Allows an alias value (alternate) string to reference a value.
077     * 
078     * @since 5.2.2
079     */
080    public StringToEnumCoercion<T> addAlias(String alias, T value)
081    {
082        stringToEnum.put(alias, value);
083
084        return this;
085    }
086
087    public static <T extends Enum> StringToEnumCoercion<T> create(Class<T> enumClass)
088    {
089        return new StringToEnumCoercion<T>(enumClass);
090    }
091
092}