public class EnumHelper extends Object
Constructor and Description |
---|
EnumHelper() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
fromName(String name,
T[] enumValues)
This method takes an arbitrary
String name and returns the appropriate enum . |
static <T> T |
fromNameIgnoreCase(String name,
T[] enumValues)
This method takes an arbitrary
String name and returns the appropriate enum . |
static <T> T |
fromOrdinal(int ordinalValue,
T[] enumValues)
This method takes an arbitrary
int ordinalValue and returns the appropriate enum . |
public static <T> T fromOrdinal(int ordinalValue, T[] enumValues)
int
ordinalValue
and returns the appropriate enum
. In case
the int
ordinalValue
is not a valid value of the enum
, an InvalidEnumValueException
will be thrown.T
- the enum
to be worked withordinalValue
- The int
value to convert into the enum
(also called "ordinal").enumValues
- An array containing all possible values of the enum
.enum
-object associated with the given ordinal.InvalidEnumValueException
- if ordinalValue
is not a valid ordinal value for the enum
.public static <T> T fromName(String name, T[] enumValues)
This method takes an arbitrary String
name
and returns the appropriate enum
.
In case the String
name
is not a valid name of one of the enum
s values, an
InvalidEnumNameException
will be thrown.
Please Note: This method does a case-sensitive comparison of the name. For a non-case-sensitive approach,
see fromNameIgnoreCase(String, Object[])
.
T
- the enum
to be worked withname
- The name of one of the enum
s values.enumValues
- An array containing all possible values of the enum
.enum
-object associated with the given ordinal.InvalidEnumNameException
- if name is not a valid name of one of the enum
s values.fromNameIgnoreCase(String, Object[])
public static <T> T fromNameIgnoreCase(String name, T[] enumValues)
This method takes an arbitrary String
name
and returns the appropriate enum
.
In case the String
name
is not a valid name of one of the enum
s values, an
InvalidEnumNameException
will be thrown.
Please Note: This method does a non-case-sensitive comparison of the name. For a case-sensitive approach,
see fromName(String, Object[])
.
T
- the enum
to be worked withname
- The name of one of the enum
s values.enumValues
- An array containing all possible values of the enum
.enum
-object associated with the given ordinal.InvalidEnumNameException
- if name is not a valid name of one of the enum
s values.fromName(String, Object[])
Copyright © 2016. All Rights Reserved.