1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache license, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the license for the specific language governing permissions and 15 * limitations under the license. 16 */ 17 package org.apache.logging.log4j.util; 18 19 import java.util.Locale; 20 21 /** 22 * Helps convert English Strings to English Enum values. 23 * <p> 24 * Enum name arguments are converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to 25 * avoid problems on the Turkish locale. Do not use with Turkish enum values. 26 * </p> 27 */ 28 public final class EnglishEnums { 29 30 private EnglishEnums() { 31 } 32 33 /** 34 * Returns the Result for the given string. 35 * <p> 36 * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to 37 * avoid problems on the Turkish locale. Do not use with Turkish enum values. 38 * </p> 39 * 40 * @param enumType The Class of the enum. 41 * @param name The enum name, case-insensitive. If null, returns {@code defaultValue}. 42 * @param <T> The type of the enum. 43 * @return an enum value or null if {@code name} is null. 44 */ 45 public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name) { 46 return valueOf(enumType, name, null); 47 } 48 49 /** 50 * Returns an enum value for the given string. 51 * <p> 52 * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to 53 * avoid problems on the Turkish locale. Do not use with Turkish enum values. 54 * </p> 55 * 56 * @param name The enum name, case-insensitive. If null, returns {@code defaultValue}. 57 * @param enumType The Class of the enum. 58 * @param defaultValue the enum value to return if {@code name} is null. 59 * @param <T> The type of the enum. 60 * @return an enum value or {@code defaultValue} if {@code name} is null. 61 */ 62 public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name, final T defaultValue) { 63 return name == null ? defaultValue : Enum.valueOf(enumType, name.toUpperCase(Locale.ENGLISH)); 64 } 65 66 }