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       * Obtain an enum value by looking up the name in a given property, defaulting if necessary.
11       *
12       * @param propertyName the name of the property containing the stringified enum value.
13       * @param values       the value of the enumeration, usually {@code E.values()}.
14       * @param defaultValue the value to default to if the property is unset or does not match {@code E.name()} for some element
15       *                     of {@code values}.
16       * @return the corresponding enum value if the property is set and recognized, or {@code defaultValue} otherwise.
17       * @since v3.2.20
18       */
19      public static <E extends Enum<E>> E enumValueFromProperty(final String propertyName, final E[] values, final E defaultValue) {
20          final String propertyValue = System.getProperty(propertyName);
21          if (null != propertyValue) {
22              for (final E value : values) {
23                  if (value.name().equalsIgnoreCase(propertyValue)) {
24                      return value;
25                  }
26              }
27          }
28          return defaultValue;
29      }
30  }