| 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 NumberTypeWrapper extends AbstractSimpleTypeWrapper { |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
private static final long serialVersionUID = 1L; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public NumberTypeWrapper(final Class<? extends Number> valueType) { |
| 41 | 1045 | super(valueType); |
| 42 | 1045 | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
public static TypeWrapper getInstance( |
| 50 | |
final Class<? extends Number> valueType) { |
| 51 | 1045 | return new NumberTypeWrapper(valueType); |
| 52 | |
} |
| 53 | |
|
| 54 | |
|
| 55 | |
@Override |
| 56 | |
public Object fromString(final String value) { |
| 57 | 1254 | if (value == null) { |
| 58 | 0 | return null; |
| 59 | |
} |
| 60 | |
try { |
| 61 | 1254 | return this.getValueType().getConstructor( |
| 62 | |
new Class[] { String.class }).newInstance( |
| 63 | |
new Object[] { value }); |
| 64 | 0 | } catch (final NoSuchMethodException e) { |
| 65 | 0 | throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT |
| 66 | |
+ value + TypeWrapper.TO + this.getValueType(), e); |
| 67 | 0 | } catch (final IllegalAccessException e) { |
| 68 | 0 | throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT |
| 69 | |
+ value + TypeWrapper.TO + this.getValueType(), e); |
| 70 | 0 | } catch (final InstantiationException e) { |
| 71 | 0 | throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT |
| 72 | |
+ value + TypeWrapper.TO + this.getValueType(), e); |
| 73 | 0 | } catch (final InvocationTargetException e) { |
| 74 | 0 | throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT |
| 75 | |
+ value + TypeWrapper.TO + this.getValueType(), e); |
| 76 | |
} |
| 77 | |
} |
| 78 | |
} |