Top Description Inners Fields Constructors Methods
com.google.common.base

public final Class Enums

extends Object
Class Inheritance
Annotations
@GwtIncompatible
@J2ktIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

Utility methods for working with Enum instances.
Author
Steve McKay
Since
9.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private static final Map<Class<? extends Enum<?>>, Map<String, WeakReference<? extends Enum<?>>>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>>
getEnumConstants(Class<T> enumClass)

public static Field
getField(Enum<?> enumValue)

Returns the Field in which enumValue is defined.

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.

private static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>>
populateCache(Class<T> enumClass)

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().

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

enumConstantCacheback to summary
private static final Map<Class<? extends Enum<?>>, Map<String, WeakReference<? extends Enum<?>>>> enumConstantCache
Annotations
@GwtIncompatible

Constructor Detail

Enumsback to summary
private Enums()

Method Detail

getEnumConstantsback to summary
pack-priv static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants(Class<T> enumClass)
Annotations
@GwtIncompatible
getFieldback to summary
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).

Annotations
@GwtIncompatible
Since
12.0
getIfPresentback to summary
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. 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
populateCacheback to summary
private static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> populateCache(Class<T> enumClass)
Annotations
@GwtIncompatible
stringConverterback to summary
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.

Annotations
@GwtIncompatible
Since
16.0
com.google.common.base back to summary

private final Class Enums.StringConverter<T extends Enum<T>>

extends Converter<String, T extends Enum<T>>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@GwtIncompatible

Field Summary

Modifier and TypeField and Description
private final Class<T>
private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
StringConverter(Class<T> enumClass)

Method Summary

Modifier and TypeMethod and Description
protected String
doBackward(T
the instance to convert; will never be null
enumValue
)

Implements abstract com.google.common.base.Converter.doBackward.

Returns a representation of b as an instance of type A.
protected T
doForward(String
the instance to convert; will never be null
value
)

Implements abstract com.google.common.base.Converter.doForward.

Returns a representation of a as an instance of type B.
public boolean
equals(Object
the reference object with which to compare.
object
)

Overrides com.google.common.base.Converter.equals.

Implements com.google.common.base.Function.equals.

Indicates whether another object is equal to this converter.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from com.google.common.base.Converter:
andThenapplyconvertconvertAllcorrectedDoBackwardcorrectedDoForwarddoAndThenfromidentityreverse

Field Detail

enumClassback to summary
private final Class<T> enumClass
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

StringConverterback to summary
pack-priv StringConverter(Class<T> enumClass)

Method Detail

doBackwardback to summary
protected String doBackward(T enumValue)

Implements abstract com.google.common.base.Converter.doBackward.

Doc from com.google.common.base.Converter.doBackward.

Returns a representation of b as an instance of type A. If b cannot be converted, an unchecked exception (such as IllegalArgumentException) should be thrown.

Parameters
enumValue:T

the instance to convert; will never be null

Returns:String

the converted instance; must not be null

Annotations
@Override
doForwardback to summary
protected T doForward(String value)

Implements abstract com.google.common.base.Converter.doForward.

Doc from com.google.common.base.Converter.doForward.

Returns a representation of a as an instance of type B. If a cannot be converted, an unchecked exception (such as IllegalArgumentException) should be thrown.

Parameters
value:String

the instance to convert; will never be null

Returns:T

the converted instance; must not be null

Annotations
@Override
equalsback to summary
public boolean equals(Object object)

Overrides com.google.common.base.Converter.equals.

Implements com.google.common.base.Function.equals.

Doc from com.google.common.base.Converter.equals.

Indicates whether another object is equal to this converter.

Most implementations will have no reason to override the behavior of Object#equals. However, an implementation may also choose to return true whenever object is a Converter that it considers interchangeable with this one. "Interchangeable" typically means that Objects.equal(this.convert(a), that.convert(a)) is true for all a of type A (and similarly for reverse). Note that a false result from this method does not imply that the converters are known not to be interchangeable.

Parameters
object:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override