| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.context.typewrapper; |
| 20 | |
|
| 21 | |
import java.lang.reflect.InvocationTargetException; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
public final class EnumTypeWrapper extends AbstractSimpleTypeWrapper { |
| 29 | |
private static final String FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS = "Failed to retrieve values of enum class "; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
private static final long serialVersionUID = 1L; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
private EnumTypeWrapper(final Class<? extends Enum<?>> valueType) { |
| 43 | 209 | super(valueType); |
| 44 | 209 | } |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public static TypeWrapper getInstance( |
| 52 | |
final Class<? extends Enum<?>> valueType) { |
| 53 | 209 | return new EnumTypeWrapper(valueType); |
| 54 | |
} |
| 55 | |
|
| 56 | |
|
| 57 | |
@SuppressWarnings("unchecked") |
| 58 | |
@Override |
| 59 | |
public Object fromString(final String value) { |
| 60 | 418 | if (value == null) { |
| 61 | 0 | return null; |
| 62 | |
} |
| 63 | 418 | return Enum.valueOf((Class) this.getValueType(), value); |
| 64 | |
} |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public Object[] values() { |
| 72 | |
try { |
| 73 | 0 | return (Object[]) this.getValueType().getMethod("values").invoke( |
| 74 | |
null); |
| 75 | 0 | } catch (final InvocationTargetException e) { |
| 76 | 0 | throw new RuntimeException( |
| 77 | |
EnumTypeWrapper.FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS |
| 78 | |
+ this.getValueType(), e); |
| 79 | 0 | } catch (final IllegalAccessException e) { |
| 80 | 0 | throw new RuntimeException( |
| 81 | |
EnumTypeWrapper.FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS |
| 82 | |
+ this.getValueType(), e); |
| 83 | 0 | } catch (final NoSuchMethodException e) { |
| 84 | 0 | throw new RuntimeException( |
| 85 | |
EnumTypeWrapper.FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS |
| 86 | |
+ this.getValueType(), e); |
| 87 | |
} |
| 88 | |
} |
| 89 | |
} |