Package com.google.common.base
Class Enums
- java.lang.Object
-
- com.google.common.base.Enums
-
@GwtIncompatible public final class Enums extends java.lang.Object
Utility methods for working withEnuminstances.- Since:
- 9.0
- Author:
- Steve McKay
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.reflect.FieldgetField(java.lang.Enum<?> enumValue)Returns theFieldin whichenumValueis defined.static <T extends java.lang.Enum<T>>
Optional<T>getIfPresent(java.lang.Class<T> enumClass, java.lang.String value)Returns an optional enum constant for the given type, usingEnum.valueOf(java.lang.Class<T>, java.lang.String).static <T extends java.lang.Enum<T>>
Converter<java.lang.String,T>stringConverter(java.lang.Class<T> enumClass)Returns a serializable converter that converts between strings andenumvalues of typeenumClassusingEnum.valueOf(Class, String)andEnum.name().
-
-
-
Method Detail
-
getField
@GwtIncompatible public static java.lang.reflect.Field getField(java.lang.Enum<?> enumValue)
Returns theFieldin whichenumValueis defined. For example, to get theDescriptionannotation on theGOLFconstant of enumSport, useEnums.getField(Sport.GOLF).getAnnotation(Description.class).- Since:
- 12.0
-
getIfPresent
public static <T extends java.lang.Enum<T>> Optional<T> getIfPresent(java.lang.Class<T> enumClass, java.lang.String value)
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 java.lang.Enum<T>> Converter<java.lang.String,T> stringConverter(java.lang.Class<T> enumClass)
Returns a serializable converter that converts between strings andenumvalues of typeenumClassusingEnum.valueOf(Class, String)andEnum.name(). The converter will throw anIllegalArgumentExceptionif the argument is not the name of any enum constant in the specified enum.- Since:
- 16.0
-
-