Class EnumUtils


  • public final class EnumUtils
    extends java.lang.Object
    Utils Class to handle enumeration values ​​to int-based "bit flags".
    Author:
    Nicolas Dos Santos
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean isFlagSet​(int value, java.lang.Enum<?> e)
      Checks whether the int-based "bit flags" representation of the given enumeration value is present in the given value.
      static int toAllFlagValue​(java.lang.Class<? extends java.lang.Enum<?>> enumClass)
      Returns int-based "bit flags" representing the values of the given Enum class.
      If the given Enum class is null, return 0.
      static <E extends java.lang.Enum<E>>
      E
      toEnum​(java.lang.Class<E> enumClass, java.lang.String enumName)
      Gets the enum for the class, returning null if not found.
      static int toFlagValue​(java.lang.Enum<?> e)
      Returns int-based "bit flags" representing the value of the given enumeration.
      If the given enumeration is null, return 0.
      static int toFlagValue​(java.lang.Enum<?>... enumArray)
      Returns int-based "bit flags" representing the values of the given enumerations.
      If the given enumerations is null, return 0.
      static int toFlagValue​(java.util.Collection<? extends java.lang.Enum<?>> enumList)
      Returns int-based "bit flags" representing the values of the given list of enumerations.
      If the given list of enumerations is null, return 0.
      static <T extends java.lang.Enum<T>>
      java.lang.String[]
      toStringArray​(java.lang.Class<T> enumClass)
      Converts an Enum to a String array.
      • Methods inherited from class java.lang.Object

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

      • toFlagValue

        public static int toFlagValue​(java.lang.Enum<?> e)
        Returns int-based "bit flags" representing the value of the given enumeration.
        If the given enumeration is null, return 0.
        Parameters:
        e - Enumeration value to handle
        Returns:
        int-based "bit flags", or 0 if e is null
      • toFlagValue

        public static int toFlagValue​(java.lang.Enum<?>... enumArray)
        Returns int-based "bit flags" representing the values of the given enumerations.
        If the given enumerations is null, return 0.
        Parameters:
        enumArray - Enumeration values to handle
        Returns:
        int-based "bit flags", or 0 if enumArray is null
      • toFlagValue

        public static int toFlagValue​(java.util.Collection<? extends java.lang.Enum<?>> enumList)
        Returns int-based "bit flags" representing the values of the given list of enumerations.
        If the given list of enumerations is null, return 0.
        Parameters:
        enumList - Enumeration values to handle
        Returns:
        int-based "bit flags", or 0 if enumList is null
      • toAllFlagValue

        public static int toAllFlagValue​(java.lang.Class<? extends java.lang.Enum<?>> enumClass)
        Returns int-based "bit flags" representing the values of the given Enum class.
        If the given Enum class is null, return 0.
        Parameters:
        enumClass - Enum class to handle
        Returns:
        int-based "bit flags", or 0 if enumClass is null
      • isFlagSet

        public static boolean isFlagSet​(int value,
                                        java.lang.Enum<?> e)
        Checks whether the int-based "bit flags" representation of the given enumeration value is present in the given value.
        Parameters:
        value - value to check
        e - Enumeration value
        Returns:
        true the Enumeration value is flagged
      • toStringArray

        public static <T extends java.lang.Enum<T>> java.lang.String[] toStringArray​(java.lang.Class<T> enumClass)
        Converts an Enum to a String array.
        Parameters:
        enumClass - Class representing the Enum
        Returns:
        String[]
      • toEnum

        public static <E extends java.lang.Enum<E>> E toEnum​(java.lang.Class<E> enumClass,
                                                             java.lang.String enumName)

        Gets the enum for the class, returning null if not found.

        This method differs from Enum.valueOf(java.lang.Class<T>, java.lang.String) in that it does not throw an exception for an invalid enum name.

        Type Parameters:
        E - the type of the enumeration
        Parameters:
        enumClass - the class of the enum to query, not null
        enumName - the enum name, null returns null
        Returns:
        the enum, null if not found