Package com.google.common.base
Class Enums
- java.lang.Object
-
- com.google.common.base.Enums
-
@GwtCompatible(emulated=true) @Beta public final class Enums extends java.lang.Object
Utility methods for working withEnum
instances.- Since:
- 9.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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 converter that converts between strings andenum
values of typeenumClass
usingEnum.valueOf(Class, String)
andEnum.name()
.static <T extends java.lang.Enum<T>>
Function<java.lang.String,T>valueOfFunction(java.lang.Class<T> enumClass)
Deprecated.UsestringConverter(java.lang.Class<T>)
instead.
-
-
-
Method Detail
-
valueOfFunction
@Deprecated public static <T extends java.lang.Enum<T>> Function<java.lang.String,T> valueOfFunction(java.lang.Class<T> enumClass)
Deprecated.UsestringConverter(java.lang.Class<T>)
instead. Note that the string converter has slightly different behavior: it throwsIllegalArgumentException
if the enum constant does not exist rather than returningnull
. It also convertsnull
tonull
rather than throwingNullPointerException
. This method is scheduled for removal in Guava 18.0.Returns aFunction
that maps anEnum
name to the associatedEnum
constant. TheFunction
will returnnull
if theEnum
constant does not exist.- Parameters:
enumClass
- theClass
of theEnum
declaring the constant values
-
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
public static <T extends java.lang.Enum<T>> Converter<java.lang.String,T> stringConverter(java.lang.Class<T> enumClass)
Returns a 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
-
-