public final class Enums
extends java.lang.Object
Enum instances.| Modifier and Type | Method and Description |
|---|---|
static java.lang.reflect.Field |
getField(java.lang.Enum<?> enumValue)
Returns the
Field in which enumValue is defined. |
static <T extends java.lang.Enum<T>> |
getIfPresent(java.lang.Class<T> enumClass,
java.lang.String value)
Returns an optional enum constant for the given type, using
Enum.valueOf(java.lang.Class<T>, java.lang.String). |
static <T extends java.lang.Enum<T>> |
stringConverter(java.lang.Class<T> enumClass)
Returns a converter that converts between strings and
enum values of type
enumClass using Enum.valueOf(Class, String) and Enum.name(). |
static <T extends java.lang.Enum<T>> |
valueOfFunction(java.lang.Class<T> enumClass)
Deprecated.
Use
stringConverter(java.lang.Class<T>) instead. Note that the string converter has
slightly different behavior: it throws IllegalArgumentException if the enum
constant does not exist rather than returning null. It also converts null
to null rather than throwing NullPointerException. This method is
scheduled for removal in Guava 18.0. |
public static java.lang.reflect.Field getField(java.lang.Enum<?> enumValue)
Field in which enumValue is defined. For example, to get the
Description annotation on the GOLF constant of enum Sport, use
Enums.getField(Sport.GOLF).getAnnotation(Description.class).@Deprecated public static <T extends java.lang.Enum<T>> Function<java.lang.String,T> valueOfFunction(java.lang.Class<T> enumClass)
stringConverter(java.lang.Class<T>) instead. Note that the string converter has
slightly different behavior: it throws IllegalArgumentException if the enum
constant does not exist rather than returning null. It also converts null
to null rather than throwing NullPointerException. This method is
scheduled for removal in Guava 18.0.Function that maps an Enum name to the associated Enum
constant. The Function will return null if the Enum constant
does not exist.enumClass - the Class of the Enum declaring the constant valuespublic static <T extends java.lang.Enum<T>> Optional<T> getIfPresent(java.lang.Class<T> enumClass, java.lang.String value)
Enum.valueOf(java.lang.Class<T>, java.lang.String). If the
constant does not exist, Optional.absent() is returned. A common use case is for parsing
user input or falling back to a default enum constant. For example,
Enums.getIfPresent(Country.class, countryInput).or(Country.DEFAULT);public static <T extends java.lang.Enum<T>> Converter<java.lang.String,T> stringConverter(java.lang.Class<T> enumClass)
enum values of type
enumClass using Enum.valueOf(Class, String) and Enum.name(). The
converter will throw an IllegalArgumentException if the argument is not the name of
any enum constant in the specified enum.