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