Package com.google.common.base
Class Enums
- java.lang.Object
-
- com.google.common.base.Enums
-
@GwtCompatible(emulated=true) @Beta public final class Enums extends Object
Utility methods for working withEnuminstances.- Since:
- 9.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FieldgetField(Enum<?> enumValue)Returns theFieldin whichenumValueis defined.static <T extends Enum<T>>
Optional<T>getIfPresent(Class<T> enumClass, String value)Returns an optional enum constant for the given type, usingEnum.valueOf(java.lang.Class<T>, java.lang.String).static <T extends Enum<T>>
Function<String,T>valueOfFunction(Class<T> enumClass)
-
-
-
Method Detail
-
getField
@GwtIncompatible("reflection") public static Field getField(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
-
valueOfFunction
public static <T extends Enum<T>> Function<String,T> valueOfFunction(Class<T> enumClass)
Returns aFunctionthat maps anEnumname to the associatedEnumconstant. TheFunctionwill returnnullif theEnumconstant does not exist.- Parameters:
enumClass- theClassof theEnumdeclaring the constant values.
-
getIfPresent
public static <T extends Enum<T>> Optional<T> getIfPresent(Class<T> enumClass, 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
-
-