public class InvalidEnumValueException extends InvalidValueException
Exception-class for int-to-enum conversions, when the given int-value exceeds the valid values of the enum.
Example:
enum ExampleEnum {
ONE,
TWO,
THREE,
FOUR;
// cache all the values defined above in static array for fast int-to-enum conversion
private static final JavaType[] VALUES = values();
/ **
* This method takes an arbitrary int value and returns the appropriate enum-value In case the
* int-value is not a valid value of the enum, an Exception will be thrown.
*
* @param value
* The integer value to convert into the enum (also called "ordinal")
*
* @return the enum-object associated with the given ordinal.
*
* @throws de.dimensionv.java.libraries.common.exceptions.InvalidEnumValueException
* if value is not a valid value for the enum.
* /
public static JavaType fromOrdinal(int value) {
try {
return VALUES[value];
} catch(ArrayIndexOutOfBoundsException ex) {
throw new InvalidEnumValueException(value, VALUES.length-1);
}
}
}
Since class version 1.1, API version 1.3.0, this class extends InvalidValueException. Therefore, the method
getValue() now returns an Object (which contains an Integer-instance). In
order to retrieve the int-value directly, please use the method getIntValue().
| Constructor and Description |
|---|
InvalidEnumValueException(int value,
int maximum)
Constructor that accepts the invalid value and the allowed maximum for the appropriate
enum. |
| Modifier and Type | Method and Description |
|---|---|
int |
getIntValue()
Returns the value that is said to be invalid explicitly as an integer primitive.
|
int |
getMaximum()
Returns the allowed maximum value.
|
getValueaddSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toStringpublic InvalidEnumValueException(int value,
int maximum)
enum.value - The invalid value.maximum - The allowed maximum for the enum.public int getIntValue()
public int getMaximum()
Copyright © 2015. All Rights Reserved.