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.InternalUtils;
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 is release 5.3, but kept in same package for compatibility.
031 * 
032 * @param <T>
033 *            the type of enumeration
034 */
035public final class StringToEnumCoercion<T extends Enum> implements Coercion<String, T>
036{
037    private final Class<T> enumClass;
038
039    private final Map<String, T> stringToEnum = CollectionFactory.newCaseInsensitiveMap();
040
041    public StringToEnumCoercion(Class<T> enumClass)
042    {
043        this(enumClass, enumClass.getEnumConstants());
044    }
045
046    public StringToEnumCoercion(Class<T> enumClass, T... values)
047    {
048        this.enumClass = enumClass;
049
050        for (T value : values)
051            stringToEnum.put(value.name(), value);
052    }
053
054    @Override
055    public T coerce(String input)
056    {
057        if (InternalUtils.isBlank(input))
058            return null;
059
060        T result = stringToEnum.get(input);
061
062        if (result == null)
063        {
064            String message = String.format("Input '%s' does not identify a value from enumerated type %s.", input,
065                    enumClass.getName());
066
067            throw new UnknownValueException(message, new AvailableValues(enumClass.getName() + " enum constants",
068                    stringToEnum));
069        }
070
071        return result;
072    }
073
074    /**
075     * Allows an alias value (alternate) string to reference a value.
076     * 
077     * @since 5.2.2
078     */
079    public StringToEnumCoercion<T> addAlias(String alias, T value)
080    {
081        stringToEnum.put(alias, value);
082
083        return this;
084    }
085
086    public static <T extends Enum> StringToEnumCoercion<T> create(Class<T> enumClass)
087    {
088        return new StringToEnumCoercion<T>(enumClass);
089    }
090
091}