Class 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.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.String
      getPropertyName​(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.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").
      static <E extends java.lang.Enum<?>>
      java.lang.String
      getSerializationName​(E e)
      Returns a form of the enum name appropriate for serialization.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Enums

        public Enums()
    • Method Detail

      • createEnumSet

        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. 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 is null.
      • getSerializationName

        public static <E extends java.lang.Enum<?>> java.lang.String getSerializationName​(E 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 produce file-not-found.

        Type Parameters:
        E - The type of the enum.
        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 produce FILE_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 is null.
        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. This ID is useful for resource keys, for example. The ID will be in the form com.example.EnumClass.NAME.
        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 is null.
        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"). This ID is useful for resource keys, for example. The ID will be in the form com.example.EnumClass.NAME.property.
        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, or null if no property is desired.
        Returns:
        A string identification of the enum.
        Throws:
        java.lang.NullPointerException - if the given enum is null.
        See Also:
        Classes.getPropertyName(Class, String), Object.getClass(), Enum.name()