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     * <em>Consider this class private.</em>
023     * 
024     * <p>
025     * Helps convert English Strings to English Enum values.
026     * </p>
027     * <p>
028     * Enum name arguments are converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to avoid problems on the
029     * Turkish locale. Do not use with Turkish enum values.
030     * </p>
031     */
032    public final class EnglishEnums {
033    
034        private EnglishEnums() {
035        }
036    
037        /**
038         * Returns the Result for the given string.
039         * <p>
040         * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
041         * avoid problems on the Turkish locale. Do not use with Turkish enum values.
042         * </p>
043         *
044         * @param enumType The Class of the enum.
045         * @param name The enum name, case-insensitive. If null, returns {@code defaultValue}.
046         * @param <T> The type of the enum.
047         * @return an enum value or null if {@code name} is null.
048         */
049        public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name) {
050            return valueOf(enumType, name, null);
051        }
052    
053        /**
054         * Returns an enum value for the given string.
055         * <p>
056         * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
057         * avoid problems on the Turkish locale. Do not use with Turkish enum values.
058         * </p>
059         *
060         * @param name The enum name, case-insensitive. If null, returns {@code defaultValue}.
061         * @param enumType The Class of the enum.
062         * @param defaultValue the enum value to return if {@code name} is null.
063         * @param <T> The type of the enum.
064         * @return an enum value or {@code defaultValue} if {@code name} is null.
065         */
066        public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name, final T defaultValue) {
067            return name == null ? defaultValue : Enum.valueOf(enumType, name.toUpperCase(Locale.ENGLISH));
068        }
069    
070    }