Class Enums
java.lang.Object
com.google.common.base.Enums
-
Method Summary
Modifier and TypeMethodDescriptionstatic Field
Returns theField
in whichenumValue
is defined.getIfPresent
(Class<T> enumClass, String value) Returns an optional enum constant for the given type, usingEnum.valueOf(java.lang.Class<T>, java.lang.String)
.stringConverter
(Class<T> enumClass) Returns a serializable converter that converts between strings andenum
values of typeenumClass
usingEnum.valueOf(Class, String)
andEnum.name()
.
-
Method Details
-
getField
Returns theField
in whichenumValue
is defined. For example, to get theDescription
annotation on theGOLF
constant of enumSport
, useEnums.getField(Sport.GOLF).getAnnotation(Description.class)
.- Since:
- 12.0
-
getIfPresent
Returns an optional enum constant for the given type, usingEnum.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);
- Since:
- 12.0
-
stringConverter
@GwtIncompatible public static <T extends Enum<T>> Converter<String,T> stringConverter(Class<T> enumClass) Returns a serializable converter that converts between strings andenum
values of typeenumClass
usingEnum.valueOf(Class, String)
andEnum.name()
. The converter will throw anIllegalArgumentException
if the argument is not the name of any enum constant in the specified enum.- Since:
- 16.0
-