Class Enums

java.lang.Object
com.google.common.base.Enums

@GwtIncompatible public final class Enums extends Object
Utility methods for working with Enum instances.
Since:
9.0
Author:
Steve McKay
  • Method Details

    • getField

      @GwtIncompatible public static Field getField(Enum<?> enumValue)
      Returns the 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).
      Since:
      12.0
    • getIfPresent

      public static <T extends Enum<T>> Optional<T> getIfPresent(Class<T> enumClass, String value)
      Returns an optional enum constant for the given type, using 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);
      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 and 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.
      Since:
      16.0