View Javadoc

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