Package com.globalmentor.java
Class Enums
- java.lang.Object
-
- com.globalmentor.java.Enums
-
public class Enums extends java.lang.Object
Utilities for working with enums.- Author:
- Garret Wilson
-
-
Constructor Summary
Constructors Constructor Description Enums()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E extends java.lang.Enum<E>>
java.util.Optional<E>asEnum(java.lang.Class<E> enumClass, java.lang.String name)
Returns the enum constant specified by the given name for the indicated enum type, if the name is in fact of the indicated enum type.static <E extends java.lang.Enum<E>>
java.util.EnumSet<E>createEnumSet(java.lang.Class<E> enumClass, E... enumElements)
Creates a set of enums using varargs.static <E extends java.lang.Enum<E>>
java.lang.StringgetPropertyName(E e)
Returns an identifying string for the enum that includes the enum class and the enum name.static <E extends java.lang.Enum<E>>
java.lang.StringgetPropertyName(E e, java.lang.String property)
Returns an identifying string for the enum that includes the enum class, the enum name, and an optional property or aspect (such as "label" or "glyph").static java.lang.String
getSerializationName(java.lang.Enum<?> e)
Returns a form of the enum name appropriate for serialization.static <E extends java.lang.Enum<E>>
EgetSerializedEnum(java.lang.Class<E> enumType, java.lang.String serializationName)
Returns the appropriate enum that has been serialized.
-
-
-
Method Detail
-
asEnum
public static <E extends java.lang.Enum<E>> java.util.Optional<E> asEnum(java.lang.Class<E> enumClass, java.lang.String name)
Returns the enum constant specified by the given name for the indicated enum type, if the name is in fact of the indicated enum type.- API Note:
- This method is equivalent to calling
Enum.valueOf(Class, String)
except that it returnsOptional.empty()
instead of throwing an exception if the name is unrecognized for the indicated enum type. - Type Parameters:
E
- The enum type whose constant is to be returned.- Parameters:
enumClass
- The class identifying the type of enum.name
- The name of the enum constant to return.- Returns:
- The enum constant of the specified enum type with the specified name, which will be empty if the specified enum type has no constant with the specified name.
- Throws:
java.lang.NullPointerException
- if the enum class and/or name isnull
.
-
createEnumSet
@SafeVarargs public static <E extends java.lang.Enum<E>> java.util.EnumSet<E> createEnumSet(java.lang.Class<E> enumClass, E... enumElements)
Creates a set of enums using varargs.- API Note:
- This method exists because the existing method
EnumSet.of(Enum, Enum...)
requires knowledge ahead of time of whether there is at least one enum element to be added to the set. - Type Parameters:
E
- The type of enum to be stored in the set.- Parameters:
enumClass
- The enum class.enumElements
- The elements to be contained in the set.- Returns:
- A set of enums containing the given enum values.
- Throws:
java.lang.NullPointerException
- if the given enum class and/or enum elements isnull
.- See Also:
EnumSet.of(Enum, Enum...)
-
getSerializationName
public static java.lang.String getSerializationName(java.lang.Enum<?> e)
Returns a form of the enum name appropriate for serialization.If the enum is a lexical
Identifier
, the name is converted to lowercase and all underscore characters ('_') are replaced by hyphens ('-'). For example,FILE_NOT_FOUND
would producefile-not-found
.- Implementation Note:
- JDK 6/7 did not work with some enum generics if
<E extends Enum<E>>
was used in the signature, but in Eclipse 4.2.1 it worked fine. Nevertheless usingEnum<?>
seems more flexible in general as a parameter. - Parameters:
e
- The enum instance to convert to a serialization form.- Returns:
- A string representing the enum instance in a style appropriate for use in serialization.
- See Also:
Enum.name()
,Identifier
-
getSerializedEnum
public static <E extends java.lang.Enum<E>> E getSerializedEnum(java.lang.Class<E> enumType, java.lang.String serializationName)
Returns the appropriate enum that has been serialized.If the enum is a lexical
Identifier
, the name is converted to uppercase and all hyphen characters ('-') are replaced by underscores ('_') in order to determine the original enum name. For example,file-not-found
would produceFILE_NOT_FOUND
. This method assumes that the original enum name does not contain lowercase letters.- Type Parameters:
E
- The type of the enum.- Parameters:
enumType
- The class object of the enum type from which to return an enum.serializationName
- The serialization form of the name of the enum to return.- Returns:
- The enum constant of the specified enum type with the specified serialization name.
- Throws:
java.lang.NullPointerException
- if the enum type and/or the serialization name isnull
.java.lang.IllegalArgumentException
- if the specified enum type has no constant with the specified serialization name, or the specified class object does not represent an enum type.- See Also:
Enum.valueOf(Class, String)
,Identifier
-
getPropertyName
public static <E extends java.lang.Enum<E>> java.lang.String getPropertyName(E e)
Returns an identifying string for the enum that includes the enum class and the enum name. The ID will be in the formcom.example.EnumClass.NAME
.- API Note:
- This ID is useful for resource keys, for example.
- Type Parameters:
E
- The type of the enum.- Parameters:
e
- The enum instance for which to return an ID.- Returns:
- The identifying string for the given enum.
- Throws:
java.lang.NullPointerException
- if the given enum isnull
.- See Also:
Classes.getPropertyName(Class, String)
,Object.getClass()
,Enum.name()
-
getPropertyName
public static <E extends java.lang.Enum<E>> java.lang.String getPropertyName(E e, java.lang.String property)
Returns an identifying string for the enum that includes the enum class, the enum name, and an optional property or aspect (such as "label" or "glyph"). The ID will be in the formcom.example.EnumClass.NAME.property
.- API Note:
- This ID is useful for resource keys, for example.
- Type Parameters:
E
- The type of the enum.- Parameters:
e
- The enum instance for which to return an ID.property
- The name of the enum property, ornull
if no property is desired.- Returns:
- A string identification of the enum.
- Throws:
java.lang.NullPointerException
- if the given enum isnull
.- See Also:
Classes.getPropertyName(Class, String)
,Object.getClass()
,Enum.name()
-
-