- Timestamp:
- 06/22/13 21:51:33 (11 years ago)
- Location:
- java/main
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main
-
Property
svn:ignore
set to
target
-
Property
svn:ignore
set to
-
java/main/src/main/java/com/framsticks/params/ParamBuilder.java
r77 r84 2 2 3 3 import com.framsticks.params.types.*; 4 import com.framsticks.util.lang.Numbers; 5 4 6 import org.apache.log4j.Logger; 5 7 … … 9 11 /** 10 12 * The class ParamBuilder helps building Param objects. 11 * 13 * 12 14 * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and 13 15 * surname with my personal data) … … 17 19 18 20 public class ParamBuilder { 19 private final static Logger LOGGER= Logger.getLogger(ParamBuilder.class.getName());21 private final static Logger log = Logger.getLogger(ParamBuilder.class.getName()); 20 22 21 23 private static final String ID_FIELD = "id"; … … 48 50 49 51 /** The getType of parameter. */ 50 @SuppressWarnings("rawtypes") 51 private Class paramType; 52 private Class<? extends Param> paramType; 52 53 53 54 private Param param; 55 private PrimitiveParam primitiveParam; 54 56 55 57 /** 56 58 * Build Param based on provided data. 57 * 59 * 58 60 * @return Param object 59 61 * @throws Exception … … 61 63 */ 62 64 public Param build() { 63 65 assert param != null; 64 66 param.id = id; 65 67 param.name = name; … … 68 70 param.flags = flags; 69 71 param.internalId = internalId; 70 71 72 return param; 72 73 } … … 77 78 } 78 79 80 protected <T extends Param> ParamBuilder internalSetType(Class<? extends Param> type, T param) { 81 this.paramType = type; 82 this.param = param; 83 if (param instanceof PrimitiveParam) { 84 primitiveParam = (PrimitiveParam) param; 85 } 86 return this; 87 } 88 79 89 public <T extends Param> ParamBuilder setType(Class<T> type) { 80 this.paramType = type;81 90 try { 82 this.param = (Param)paramType.newInstance();91 return internalSetType(type, type.newInstance()); 83 92 } catch (InstantiationException e) { 84 93 e.printStackTrace(); … … 89 98 } 90 99 91 public <T extends Param> ParamBuilder setType(T type) { 92 this.paramType = type.getClass(); 93 this.param = type; 94 return this; 100 public <T extends Param> ParamBuilder setType(T param) { 101 return internalSetType(param.getClass(), param); 95 102 } 96 103 … … 116 123 } 117 124 125 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) { 126 if (primitiveParam == null) { 127 log.warn("param is not a primitive param"); 128 return; 129 } 130 if (second != null) { 131 this.primitiveParam.min = Numbers.parse(second, type); 132 if (this.primitiveParam.min == null) { 133 log.warn("value of min attribute was invalid"); 134 } 135 } 136 if (third != null) { 137 this.primitiveParam.max = Numbers.parse(third, type); 138 if (this.primitiveParam.max == null) { 139 log.warn("value of min attribute was invalid"); 140 } 141 } 142 } 143 118 144 public ParamBuilder setType(String type) { 119 145 120 LOGGER.trace("parsing type: " + type);146 log.trace("parsing type: " + type); 121 147 122 148 String[] typeSplitted = type.split(" "); … … 136 162 procedureParam.parseSignature(signature); 137 163 } catch (Exception e) { 138 LOGGER.error("invalid procedure signature '" + signature + "': " + e);164 log.error("invalid procedure signature '" + signature + "': " + e); 139 165 } 140 166 setType(procedureParam); … … 148 174 } else { 149 175 if (first.length() >= 2) { 150 176 switch (first.charAt(1)) { 151 177 case 'b': { 152 178 setType(BinaryParam.class); … … 159 185 } 160 186 } 161 162 163 187 if ("0".equals(second) && "1".equals(third)) { 188 setType(BooleanParam.class); 189 } 164 190 if (param == null) { 165 191 setType(DecimalParam.class); … … 167 193 } 168 194 if (this.param instanceof DecimalParam) { 169 try { 170 if (second != null) { 171 this.param.min = Integer.parseInt(second); 172 } 173 174 if (third != null) { 175 this.param.max = Integer.parseInt(third); 176 } 177 } catch (NumberFormatException e) { 178 LOGGER.warn("value of min or max attribute should be an Integer"); 179 } 195 parseMinMaxDefNumber(Integer.class, second, third); 180 196 } 181 197 break; … … 183 199 case 'f': { 184 200 setType(FloatParam.class); 185 try { 186 if (second != null) { 187 this.param.min = Double.parseDouble(second); 188 } 189 190 if (third != null) { 191 this.param.max = Double.parseDouble(third); 192 } 193 } catch (NumberFormatException ex) { 194 LOGGER.warn("Value of min or max attribute should be Double"); 195 } 201 parseMinMaxDefNumber(Double.class, second, third); 196 202 break; 197 203 } … … 202 208 case 's': { 203 209 setType(StringParam.class); 204 this.param.min = second;205 this.param.max = third;210 setMin(second); 211 setMax(third); 206 212 break; 207 213 } … … 211 217 } 212 218 case 'l': { 213 214 219 setType(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second)); 215 220 break; … … 225 230 } 226 231 232 /** 233 * @return the paramType 234 */ 235 public Class<? extends Param> getParamType() { 236 return paramType; 237 } 238 227 239 public <T> ParamBuilder setMin(T min) { 228 this.param.min = min; 240 if (primitiveParam != null) { 241 this.primitiveParam.min = min; 242 } 229 243 return this; 230 244 } 231 245 232 246 public <T> ParamBuilder setMax(T max) { 233 this.param.max = max; 247 if (primitiveParam != null) { 248 this.primitiveParam.max = max; 249 } 234 250 return this; 235 251 } 236 252 237 253 public <T> ParamBuilder setDef(T def) { 238 this.param.def = def; 239 return this; 254 if (def != null && primitiveParam != null) { 255 this.primitiveParam.def = def; 256 } 257 return this; 258 } 259 260 public Class<?> getStorageType() { 261 assert param != null; 262 return param.getStorageType(); 240 263 } 241 264 … … 244 267 245 268 if (paramEntryValues.length == 0) { 246 LOGGER.warn("field empty or wrong format (" + line269 log.warn("field empty or wrong format (" + line 247 270 + ") - omitting"); 248 271 return null; … … 263 286 /** everything is ok, parameters have just finished*/ 264 287 } catch (NumberFormatException ex) { 265 LOGGER.warn("wrong format of entry: " + line288 log.warn("wrong format of entry: " + line 266 289 + ", omitting"); 267 290 return null;
Note: See TracChangeset
for help on using the changeset viewer.