View Javadoc

1   package com.atlassian.plugin.util;
2   
3   /**
4    * Utilities for Java Enum classes.
5    *
6    * @since v3.2.20
7    */
8   public class EnumUtils
9   {
10      /**
11       * Obtain an enum value by looking up the name in a given property, defaulting if necessary.
12       *
13       * @param propertyName the name of the property containing the stringified enum value.
14       * @param values the value of the enumeration, usually {@code E.values()}.
15       * @param defaultValue the value to default to if the property is unset or does not match {@code E.name()} for some element
16       * of {@code values}.
17       * @return the corresponding enum value if the property is set and recognized, or {@code defaultValue} otherwise.
18       * @since v3.2.20
19       */
20      public static <E extends Enum<E>> E enumValueFromProperty(final String propertyName, final E[] values, final E defaultValue)
21      {
22          final String propertyValue = System.getProperty(propertyName);
23          if (null != propertyValue)
24          {
25              for (final E value : values)
26              {
27                  if (value.name().equalsIgnoreCase(propertyValue))
28                  {
29                      return value;
30                  }
31              }
32          }
33          return defaultValue;
34      }
35  }