ehsbe CommonsTM
Version 1.1.0rc3

com.ehsbe.commons.util
Class EnumUtils

java.lang.Object
  extended by com.ehsbe.commons.util.EnumUtils

public class EnumUtils
extends Object

Utily collection for Enums. If not explict mentioned, the method works on regular enums (max. 64 values), only.

Version:
$Revision: 432 $
Author:
Jens Elkner

Constructor Summary
EnumUtils()
           
 
Method Summary
static
<E extends Enum<E>>
EnumSet<E>
convert(Class<E> targetType, EnumSet<? extends Enum<?>> src, boolean ignoreUnknown)
          Convert the given enum set to a enum set of the given targetType.
static
<E extends Enum<E>>
EnumSet<E>
difference(Class<E> enumType, long mask1, long mask2)
          Get the difference between two enum sets represented by the given masks.
static
<E extends Enum<E>>
Enum<?>[]
getArray(Class<E> elementType, byte mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
Enum<?>[]
getArray(Class<E> elementType, int mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
Enum<?>[]
getArray(Class<E> elementType, long mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
Enum<?>[]
getArray(Class<E> elementType, short mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
byte
getByteMask(EnumSet<E> eset)
          Get the mask, which corresponds to the given enum set.
static
<E extends Enum<E>>
E
getEnum(Class<E> elementType, int ordinal)
          Get an enum by its ordinal value.
static
<E extends Enum<E>>
int
getIntMask(EnumSet<E> eset)
          Get the mask, which corresponds to the given enum set.
static
<E extends Enum<E>>
long
getMask(EnumSet<E> eset)
          Get the mask, which corresponds to the given enum set.
static
<E extends Enum<E>>
EnumSet<E>
getSet(Class<E> elementType, byte mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
EnumSet<E>
getSet(Class<E> elementType, int mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
EnumSet<E>
getSet(Class<E> elementType, long mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
EnumSet<E>
getSet(Class<E> elementType, short mask)
          Get the enum set, which corresponds to the given mask and types.
static
<E extends Enum<E>>
short
getShortMask(EnumSet<E> eset)
          Get the mask, which corresponds to the given enum set.
static
<E extends Enum<E>>
boolean
isDeprecated(E element)
          Check, whether the given enum element is deprecated.
static
<E extends Enum<E>>
void
removeDeprecated(EnumSet<E> set)
          Remove all values from the given set, which have a Deprecated annotation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EnumUtils

public EnumUtils()
Method Detail

getMask

public static <E extends Enum<E>> long getMask(EnumSet<E> eset)
Get the mask, which corresponds to the given enum set.

Type Parameters:
E - the enumeration type of the set
Parameters:
eset - the set, for whome a mask should be created.
Returns:
0 if the given set is empty or null, the mask otherwise.

getIntMask

public static <E extends Enum<E>> int getIntMask(EnumSet<E> eset)
Get the mask, which corresponds to the given enum set.

Type Parameters:
E - the enumeration type of the set
Parameters:
eset - the set, for whome a mask should be created.
Returns:
0 if the given set is empty or null, the mask otherwise.

getShortMask

public static <E extends Enum<E>> short getShortMask(EnumSet<E> eset)
Get the mask, which corresponds to the given enum set.

Type Parameters:
E - the enumeration type of the set
Parameters:
eset - the set, for whome a mask should be created.
Returns:
0 if the given set is empty or null, the mask otherwise.

getByteMask

public static <E extends Enum<E>> byte getByteMask(EnumSet<E> eset)
Get the mask, which corresponds to the given enum set.

Type Parameters:
E - the enumeration type of the set
Parameters:
eset - the set, for whome a mask should be created.
Returns:
0 if the given set is empty or null, the mask otherwise.

getSet

public static <E extends Enum<E>> EnumSet<E> getSet(Class<E> elementType,
                                                    long mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getEnum

public static <E extends Enum<E>> E getEnum(Class<E> elementType,
                                            int ordinal)
Get an enum by its ordinal value. Works for JumboEnums as well.

Type Parameters:
E - enum type
Parameters:
elementType - enum class to lookup
ordinal - ordinal value of the enum to be searched
Returns:
null if the ordinal value is out of range, the appropriate enum otherwise.

getSet

public static <E extends Enum<E>> EnumSet<E> getSet(Class<E> elementType,
                                                    byte mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getSet

public static <E extends Enum<E>> EnumSet<E> getSet(Class<E> elementType,
                                                    short mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getSet

public static <E extends Enum<E>> EnumSet<E> getSet(Class<E> elementType,
                                                    int mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getArray

public static <E extends Enum<E>> Enum<?>[] getArray(Class<E> elementType,
                                                     long mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getArray

public static <E extends Enum<E>> Enum<?>[] getArray(Class<E> elementType,
                                                     byte mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getArray

public static <E extends Enum<E>> Enum<?>[] getArray(Class<E> elementType,
                                                     short mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

getArray

public static <E extends Enum<E>> Enum<?>[] getArray(Class<E> elementType,
                                                     int mask)
Get the enum set, which corresponds to the given mask and types.

Type Parameters:
E - the enumeration type of the element
Parameters:
elementType - the enumeration class, which corresponds to the mask
mask - the mask to use to create the enum set.
Returns:
a possible empty set.
Throws:
IllegalArgumentException - if the given elementType is null.

convert

public static <E extends Enum<E>> EnumSet<E> convert(Class<E> targetType,
                                                     EnumSet<? extends Enum<?>> src,
                                                     boolean ignoreUnknown)
Convert the given enum set to a enum set of the given targetType. The conversion is based on the name of the enum values of the source and target type. If the source set contains enums, which have no corresponding value with the same name within the targetType, it is ignored, if ignoreUnknown is true. Otherwise an IllegalArgumentException will be thrown.

Type Parameters:
E - the type of the target enum.
Parameters:
targetType - the element class of the returned enum set
src - value to be converted
ignoreUnknown - if true, ignore values, which are not convertable, i.e. target has no value with the same name.
Returns:
a possible empty set
Throws:
IllegalArgumentException - if the targetType is null OR ignoreUnknwon is set and an unconvertable value is encountered.

isDeprecated

public static <E extends Enum<E>> boolean isDeprecated(E element)
Check, whether the given enum element is deprecated.

Type Parameters:
E - type of the enum
Parameters:
element - element of an enum
Returns:
true, if the element is deprecated.

removeDeprecated

public static <E extends Enum<E>> void removeDeprecated(EnumSet<E> set)
Remove all values from the given set, which have a Deprecated annotation.

Type Parameters:
E - the type of the elements in the set
Parameters:
set - set to check.

difference

public static <E extends Enum<E>> EnumSet<E> difference(Class<E> enumType,
                                                        long mask1,
                                                        long mask2)
Get the difference between two enum sets represented by the given masks.

Type Parameters:
E - type of the elements in the set
Parameters:
enumType - class of the elements in the set
mask1 - mask of the first set
mask2 - mask to substract
Returns:
a possible empty set

ehsbe CommonsTM
Version 1.1.0rc3

Copyright ©2005-2009 EHSBE, Walther-Rathenau-Str. 58, 39104 Magdeburg, Sachsen-Anhalt, Germany. All Rights Reserved.