Package com.globalmentor.java
Class Enums
java.lang.Object
com.globalmentor.java.Enums
Utilities for working with enums.
- Author:
- Garret Wilson
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the enum constant specified by the given name for the indicated enum type, if the name is in fact of the indicated enum type.createEnumSet
(Class<E> enumClass, E... enumElements) Creates a set of enums using varargs.fromSerializionOf
(Class<E> enumType) Creates a function for mapping from a serialized form of some enum type to an instance of that enum type.getPropertyName
(E e) Returns an identifying string for the enum that includes the enum class and the enum name.getPropertyName
(E e, 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 String
getSerializationName
(Enum<?> e) Returns a form of the enum name appropriate for serialization.static <E extends Enum<E>>
EgetSerializedEnum
(Class<E> enumType, String serializationName) Returns the appropriate enum that has been serialized.
-
Constructor Details
-
Enums
public Enums()
-
-
Method Details
-
asEnum
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:
NullPointerException
- if the enum class and/or name isnull
.
-
createEnumSet
@SafeVarargs public static <E extends Enum<E>> EnumSet<E> createEnumSet(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:
NullPointerException
- if the given enum class and/or enum elements isnull
.- See Also:
-
getSerializationName
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:
-
getSerializedEnum
public static <E extends Enum<E>> E getSerializedEnum(@Nonnull Class<E> enumType, @Nonnull 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:
NullPointerException
- if the enum type and/or the serialization name isnull
.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:
-
fromSerializionOf
Creates a function for mapping from a serialized form of some enum type to an instance of that enum type.- Implementation Specification:
- Deserialization is performed using
getSerializedEnum(Class, String)
. - Type Parameters:
E
- The type of the enum.- Parameters:
enumType
- The class object of the enum type from which to return an enum.- Returns:
- A function for mapping to an instance of the specified enum type from a serialization name.
- Throws:
NullPointerException
- if the enum type isnull
.- See Also:
-
getPropertyName
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:
NullPointerException
- if the given enum isnull
.- See Also:
-
getPropertyName
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:
NullPointerException
- if the given enum isnull
.- See Also:
-