001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017     package org.apache.logging.log4j.util;
018    
019    import java.util.Locale;
020    
021    /**
022     * Helps convert English Strings to English Enum values.
023     * <p>
024     * Enum name arguments are converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
025     * avoid problems on the Turkish locale. Do not use with Turkish enum values.
026     * </p>
027     */
028    public class EnglishEnums {
029    
030        /**
031         * Returns the Result for the given string.
032         * <p>
033         * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
034         * avoid problems on the Turkish locale. Do not use with Turkish enum values.
035         * </p>
036         * 
037         * @param name
038         *            The enum name, case-insensitive. If null, returns {@code defaultValue}
039         * @return an enum value or null if {@code name} is null
040         */
041        public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name) {
042            return valueOf(enumType, name, null);
043        }
044    
045        /**
046         * Returns an enum value for the given string.
047         * <p>
048         * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
049         * avoid problems on the Turkish locale. Do not use with Turkish enum values.
050         * </p>
051         * 
052         * @param name
053         *            The enum name, case-insensitive. If null, returns {@code defaultValue}
054         * @param defaultValue
055         *            the enum value to return if {@code name} is null
056         * @return an enum value or {@code defaultValue} if {@code name} is null
057         */
058        public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name, T defaultValue) {
059            return name == null ? defaultValue : Enum.valueOf(enumType, name.toUpperCase(Locale.ENGLISH));
060        }
061    
062    }