- Timestamp:
- 06/28/13 11:56:03 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/PrimitiveParam.java
r84 r87 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.util.FramsticksException; 4 import com.framsticks.util.lang.Numbers; 5 6 import javax.annotation.concurrent.Immutable; 3 7 4 8 /** 5 9 * @author Piotr Sniegowski 6 10 */ 7 public abstract class PrimitiveParam extends ValueParam { 11 @Immutable 12 public abstract class PrimitiveParam<S> extends ValueParam { 8 13 9 14 /** The minimum allowed value of parameter. */ 10 protected Object min;15 protected final Object min; 11 16 12 17 /** The maximum allowed value of parameter. */ 13 protected Object max;18 protected final Object max; 14 19 15 20 /** The default value of parameter. */ 16 protected Object def; 21 protected final Object def; 22 23 /** 24 * @param builder 25 */ 26 public PrimitiveParam(ParamBuilder builder) { 27 super(builder); 28 min = builder.getMin(); 29 max = builder.getMax(); 30 def = builder.getDef(); 31 } 17 32 18 33 public void save(SinkInterface sink, Object value) { … … 21 36 22 37 protected <T> T tryCastAndReturn(Object value, Class<T> type) { 23 if (value == null) 38 if (value == null) { 24 39 return null; 40 } 25 41 try { 26 return type.cast(value); 42 if (type.equals(Object.class)) { 43 return type.cast(value); 44 } 45 if (value instanceof String) { 46 if (type.equals(String.class)) { 47 return type.cast(value); 48 } 49 // if (Number.class.isAssignableFrom(type)) { 50 return Numbers.parse((String) value, type); 51 // } 52 // return null; 53 } else { 54 return type.cast(value); 55 } 27 56 } catch (ClassCastException e) { 28 throw new ClassCastException("property \"" + name 29 + "\" type is \"" + value.getClass().getName() 30 + "\", not \"" + type.getName() + "\""); 57 throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type); 31 58 } 32 59 } … … 64 91 * @throws ClassCastException the class cast exception, thrown when given getType is incorrect 65 92 */ 66 public <T> T getDef(Class<T> type) throws ClassCastException{93 public <T> T getDef(Class<T> type) { 67 94 return tryCastAndReturn(def, type); 68 95 }
Note: See TracChangeset
for help on using the changeset viewer.