Class EnumUtil
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuildEnumMap
(Class<E> enumClass) Decodes an integer bitmask into an EnumSet of the specified enum type.static <E extends Enum<E>>
intEncodes an EnumSet into an integer bitmask.static <E extends Enum<E>>
intencodeWithReflection
(EnumSet<E> set) Encodes an EnumSet into an integer bitmask using reflection.static <E extends Enum<E>>
EGiven an enum type from the generated code and an integer value returns the corresponding enum value for the integer.static <E extends Enum<E>>
void
-
Constructor Details
-
EnumUtil
public EnumUtil()
-
-
Method Details
-
register
-
buildEnumMap
-
of
Given an enum type from the generated code and an integer value returns the corresponding enum value for the integer.- Type Parameters:
E
-- Parameters:
type
- the enum type from the wayland-scanner generated codei
- the integer value- Returns:
- the enum value
- Throws:
NullPointerException
- if enumClass is null or is not from the wayland-scanner generated code
-
decode
Decodes an integer bitmask into an EnumSet of the specified enum type.The function assumes that the enum constants are properly defined in the natural order of their bit positions. It iterates over the bits of the mask and adds the corresponding enum values to the resulting EnumSet.
- Type Parameters:
E
- the enum type- Parameters:
enumClass
- the Class object representing the enum typemask
- the integer bitmask to decode- Returns:
- an EnumSet containing the enum values corresponding to the set bits in the mask
- Throws:
NullPointerException
- if enumClass is nullIllegalArgumentException
- if mask is negative or if the ordinal value of any enum constant exceeds the number of bits in an int
-
encode
Encodes an EnumSet into an integer bitmask.The function assumes that the enum constants are defined in the order of powers of 2. It uses the ordinal values of the enum constants to set the appropriate bits in the bitmask.
- Type Parameters:
E
- the enum type- Parameters:
set
- the EnumSet to be encoded- Returns:
- an integer bitmask representing the encoded EnumSet
-
encodeWithReflection
Encodes an EnumSet into an integer bitmask using reflection.Unlike
encode(EnumSet)
, this method does not rely on the ordinal values of the enum constants. Instead, it uses reflection to invoke agetValue()
method on each enum constant to obtain the corresponding bit value. The resulting bitmask is constructed by performing a bitwise OR operation on the obtained bit values.The enum type must have a
getValue()
method that returns the desired bit value for each constant.- Type Parameters:
E
- the enum type- Parameters:
set
- the EnumSet to be encoded- Returns:
- an integer bitmask representing the encoded EnumSet
- Throws:
IllegalArgumentException
- if the enum type does not have agetValue()
method or if there is an exception during reflection
-