@GwtCompatible(emulated=true) @Beta public final class Enums extends Object
Enum
instances.
Modifier and Type | Method and Description |
---|---|
static Field |
getField(Enum
Returns the
Field in which
enumValue is defined.
|
static <T extends Enum |
getIfPresent(Class
Returns an optional enum constant for the given type, using
Enum .
|
static <T extends Enum |
stringConverter(Class
Returns a converter that converts between strings and
enum values of type
enumClass using
Enum and
Enum .
|
@GwtIncompatible(value="reflection") public static FieldgetField(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)
.
public static <T extends Enum<T>> Optional <T> getIfPresent(Class <T> enumClass, 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 Enum<T>> Converter <String ,T> stringConverter(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.