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 * <em>Consider this class private.</em>
23 *
24 * <p>
25 * Helps convert English Strings to English Enum values.
26 * </p>
27 * <p>
28 * Enum name arguments are converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to avoid problems on the
29 * Turkish locale. Do not use with Turkish enum values.
30 * </p>
31 */
32 public final class EnglishEnums {
33
34 private EnglishEnums() {
35 }
36
37 /**
38 * Returns the Result for the given string.
39 * <p>
40 * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
41 * avoid problems on the Turkish locale. Do not use with Turkish enum values.
42 * </p>
43 *
44 * @param enumType The Class of the enum.
45 * @param name The enum name, case-insensitive. If null, returns {@code defaultValue}.
46 * @param <T> The type of the enum.
47 * @return an enum value or null if {@code name} is null.
48 */
49 public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name) {
50 return valueOf(enumType, name, null);
51 }
52
53 /**
54 * Returns an enum value for the given string.
55 * <p>
56 * The {@code name} is converted internally to upper case with the {@linkplain Locale#ENGLISH ENGLISH} locale to
57 * avoid problems on the Turkish locale. Do not use with Turkish enum values.
58 * </p>
59 *
60 * @param name The enum name, case-insensitive. If null, returns {@code defaultValue}.
61 * @param enumType The Class of the enum.
62 * @param defaultValue the enum value to return if {@code name} is null.
63 * @param <T> The type of the enum.
64 * @return an enum value or {@code defaultValue} if {@code name} is null.
65 */
66 public static <T extends Enum<T>> T valueOf(final Class<T> enumType, final String name, final T defaultValue) {
67 return name == null ? defaultValue : Enum.valueOf(enumType, name.toUpperCase(Locale.ENGLISH));
68 }
69
70 }