Changeset 87 for java/main/src/main/java/com/framsticks/params
- Timestamp:
- 06/28/13 11:56:03 (11 years ago)
- Location:
- java/main/src/main/java/com/framsticks/params
- Files:
-
- 1 added
- 31 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/AccessInterface.java
r86 r87 74 74 FramsClass getFramsClass(); 75 75 76 boolean tryAutoAppend(Object object); 77 76 78 77 79 } -
java/main/src/main/java/com/framsticks/params/ArrayListAccess.java
r86 r87 31 31 @Override 32 32 public Param getParam(int i) { 33 //TODO listAccessParam 33 34 return Param.build().id(Integer.toString(i)).name(elementAccess.getId()).type("o " + elementAccess.getId()).finish(); 34 35 } -
java/main/src/main/java/com/framsticks/params/CompositeParam.java
r84 r87 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.util.lang.Strings;3 import javax.annotation.concurrent.Immutable; 4 4 5 5 /** 6 6 * @author Piotr Sniegowski 7 7 */ 8 @Immutable 8 9 public abstract class CompositeParam extends ValueParam { 10 9 11 protected final String containedTypeName; 10 12 11 public CompositeParam(String containedTypeName) { 12 this.containedTypeName = Strings.notEmpty(containedTypeName) ? containedTypeName : null; 13 public CompositeParam(ParamBuilder builder) { 14 super(builder); 15 this.containedTypeName = builder.getContainedTypeName(); 13 16 } 14 17 -
java/main/src/main/java/com/framsticks/params/FramsClass.java
r86 r87 8 8 9 9 import javax.annotation.Nonnull; 10 import javax.annotation.concurrent.Immutable; 11 12 import org.apache.log4j.Logger; 10 13 11 14 /** … … 21 24 * @author Piotr Sniegowski 22 25 */ 26 @Immutable 23 27 @FramsClassAnnotation(id = "class", name = "class") 24 28 public final class FramsClass { 25 29 26 /** The offset of the parameter (applied for newly added parameter). */ 27 protected int fieldsNumber; 30 private final static Logger log = Logger.getLogger(FramsClass.class); 28 31 29 /** The groups. */ 30 protected List<Group> groups = new ArrayList<Group>(); 32 protected final String id; 33 34 protected final String name; 35 36 protected final String description; 37 38 protected final List<Group> groups; 39 40 /** The param list (for accessing parameters by offset in O(1) time. */ 41 protected final List<Param> paramList; 31 42 32 43 /** … … 36 47 protected Map<String, Param> paramEntryMap = new LinkedHashMap<String, Param>(); 37 48 38 /** The param list (for accessing parameters by offset in O(1) time. */ 39 protected List<Param> paramList = new ArrayList<Param>(); 40 41 /** The param getId map (for fast lookup of offset based on name */ 42 protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>(); 43 44 protected String id; 45 46 protected String name; 47 48 protected String description; 49 // /** The param getId map (for fast lookup of offset based on name */ 50 // protected Map<String, Integer> paramIdMap = new HashMap<String, Integer>(); 49 51 50 52 public Collection<Param> getParamEntries() { … … 52 54 } 53 55 54 public FramsClass( ) {56 public FramsClass(String id, String name, String description, List<Param> params, List<Group> groups) { 55 57 56 } 58 this.id = id; 59 this.name = name; 60 this.description = description; 61 this.groups = groups; 62 this.paramList = params; 57 63 58 public FramsClass(String id, String name, String description) { 59 this.setId(id); 60 this.setName(name); 61 this.setDescription(description); 62 } 64 for (Param param : params) { 65 paramEntryMap.put(param.getId(), param); 66 try { 67 Group group = groups.get(param.getGroup()); 68 if (group != null) { 69 group.addProperty(param); 70 } 71 } catch (IndexOutOfBoundsException ignored) { 63 72 64 /**65 * Adds new param entry.66 *67 * @param param68 * the new param entry69 */70 public FramsClass append(Param param) {71 // if (param.hasFlag(Flags.USERHIDDEN)) {72 // return this;73 // }74 paramEntryMap.put(param.getId(), param);75 //paramEntryMap.put(param.getInternalId(), param);76 paramList.add(param);77 try {78 Group group = groups.get(param.getGroup());79 if (group != null) {80 group.addProperty(param);81 73 } 82 } catch (IndexOutOfBoundsException ignored) {83 84 74 } 85 75 86 return this; 87 } 76 log.trace("created framsclass " + this); 88 77 89 public FramsClass append(ParamBuilder builder) {90 return append(builder.finish());91 }92 93 /**94 * Adds new group.95 */96 public FramsClass appendGroup(Group group) {97 groups.add(group);98 return this;99 78 } 100 79 … … 206 185 @Override 207 186 public String toString() { 208 return id ;187 return id + "(" + name + ")"; 209 188 } 210 189 211 @ParamAnnotation 212 public void setId(String id) { 213 this.id = id; 214 } 215 216 @ParamAnnotation 217 public void setName(String name) { 218 this.name = name; 219 } 220 221 @ParamAnnotation(id = "desc") 222 public void setDescription(String description) { 223 this.description = description; 190 public static FramsClassBuilder build() { 191 return new FramsClassBuilder(); 224 192 } 225 193 } -
java/main/src/main/java/com/framsticks/params/FramsClassBuilder.java
r86 r87 7 7 import java.lang.reflect.ParameterizedType; 8 8 import java.lang.reflect.Type; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.HashMap; 12 import java.util.LinkedList; 9 13 import java.util.List; 10 14 import java.util.Map; … … 12 16 import org.apache.log4j.Logger; 13 17 18 import com.framsticks.params.annotations.AutoAppendAnnotation; 14 19 import com.framsticks.params.annotations.FramsClassAnnotation; 15 20 import com.framsticks.params.annotations.ParamAnnotation; … … 18 23 import com.framsticks.util.lang.Strings; 19 24 25 @FramsClassAnnotation(id = "class", name = "class") 20 26 public class FramsClassBuilder { 21 27 private static final Logger log = 22 28 Logger.getLogger(FramsClassBuilder.class); 23 24 29 25 30 public static String getName(FramsClassAnnotation fca, Class<?> javaClass) { … … 63 68 } 64 69 65 if (type.equals(Integer.class) || type.equals(int.class)) {66 return "d";67 }68 if (type.equals(String.class)) {69 return "s";70 }71 if (type.equals(Double.class) || type.equals(double.class)) {72 return "f";73 }74 if (type.equals(Boolean.class) || type.equals(boolean.class)) {75 return "d 0 1";76 }77 if (type.equals(Object.class)) {78 return "x";79 }80 70 if (type instanceof Class) { 71 72 Class<?> cl = (Class<?>) type; 73 74 // TODO: future support for enum 75 // if (cl.isEnum()) { 76 // Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>) cl; 77 // Enum<?>[] enums = enumType.getEnumConstants(); 78 // StringBuilder b = new StringBuilder(); 79 80 // b.append("d 0 ").append(enums.length - 1).append(" 0 "); 81 // for (Enum<?> e : enums) { 82 // b.append("~").append(e.name()); 83 // } 84 // return b.toString(); 85 // } 86 if (cl.equals(Integer.class) || cl.equals(int.class)) { 87 return "d"; 88 } 89 if (cl.equals(String.class)) { 90 return "s"; 91 } 92 if (cl.equals(Double.class) || cl.equals(double.class)) { 93 return "f"; 94 } 95 if (cl.equals(Boolean.class) || cl.equals(boolean.class)) { 96 return "d 0 1"; 97 } 98 if (cl.equals(Object.class)) { 99 return "x"; 100 } 101 81 102 return "o " + ((Class<?>) type).getCanonicalName(); 82 103 } 83 return null; 104 105 throw new ConstructionException().msg("failed to find framsticks for native type").arg("type", type); 84 106 } 85 107 … … 141 163 } 142 164 143 public static FramsClass buildForClass(Class<?> javaClass) throws ConstructionException { 165 public static Map<Class<?>, FramsClass> synchronizedCacheForBasedOnForJavaClass = Collections.synchronizedMap(new HashMap<Class<?>, FramsClass>()); 166 167 public FramsClass forClass(Class<?> javaClass) throws ConstructionException { 168 FramsClass result = synchronizedCacheForBasedOnForJavaClass.get(javaClass); 169 if (result != null) { 170 return result; 171 } 172 173 log.info("building for class " + javaClass); 144 174 145 175 FramsClassAnnotation fca = javaClass.getAnnotation(FramsClassAnnotation.class); 146 176 if (fca == null) { 147 log.error(javaClass.getName() + " is not annotated with FramsClassAnnotation");148 return null;149 } 150 151 FramsClass framsClass = new FramsClass(getId(fca, javaClass), getName(fca, javaClass), "");177 throw new ConstructionException().msg("java class is not annotated with FramsClassAnnotation").arg("java", javaClass); 178 } 179 180 id(getId(fca, javaClass)); 181 name(getName(fca, javaClass)); 152 182 153 183 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(javaClass); 154 184 155 185 for (ParamCandidate pc : candidates.values()) { 156 framsClass.append(Param.build().id(pc.getId()).name(pc.getName()).type(getParamTypeForNativeType(pc.getType())).flags(pc.getFlags())); 157 } 158 159 return framsClass; 160 } 186 param(Param.build().id(pc.getId()).name(pc.getName()).type(getParamTypeForNativeType(pc.getType())).flags(pc.getFlags())); 187 } 188 189 result = finish(); 190 191 synchronizedCacheForBasedOnForJavaClass.put(javaClass, result); 192 193 return result; 194 } 195 196 197 protected String id; 198 199 protected String name; 200 201 protected String description; 202 203 protected final List<Param> params = new LinkedList<>(); 204 205 protected List<Group> groups = new ArrayList<Group>(); 206 207 @ParamAnnotation 208 public FramsClassBuilder id(String id) { 209 this.id = id; 210 return this; 211 } 212 213 @ParamAnnotation 214 public FramsClassBuilder name(String name) { 215 this.name = name; 216 return this; 217 } 218 219 public FramsClassBuilder idAndName(String v) { 220 this.id = v; 221 this.name = v; 222 return this; 223 } 224 225 @ParamAnnotation(id = "desc") 226 public FramsClassBuilder description(String description) { 227 this.description = description; 228 return this; 229 } 230 231 public FramsClassBuilder() { 232 } 233 234 public FramsClass finish() { 235 return new FramsClass(id, name, description, params, groups); 236 } 237 238 public FramsClassBuilder append(Param param) { 239 params.add(param); 240 return this; 241 } 242 243 @AutoAppendAnnotation 244 public FramsClassBuilder param(ParamBuilder builder) { 245 return append(builder.finish()); 246 } 247 248 @AutoAppendAnnotation 249 public FramsClassBuilder group(Group group) { 250 groups.add(group); 251 return this; 252 } 253 254 /** 255 * @return the id 256 */ 257 @ParamAnnotation 258 public String getId() { 259 return id; 260 } 261 262 /** 263 * @return the name 264 */ 265 @ParamAnnotation 266 public String getName() { 267 return name; 268 } 269 270 /** 271 * @return the description 272 */ 273 @ParamAnnotation(id = "desc") 274 public String getDescription() { 275 return description; 276 } 277 278 public FramsClassBuilder group(String group) { 279 return group(new Group(group)); 280 } 281 282 public ParamBuilder param(String id) { 283 return new ParamBuilder(this).id(id); 284 } 285 161 286 } -
java/main/src/main/java/com/framsticks/params/Group.java
r77 r87 4 4 import java.util.List; 5 5 6 import com.framsticks.params.annotations.FramsClassAnnotation; 7 import com.framsticks.params.annotations.ParamAnnotation; 8 6 9 /** 7 10 * @author Piotr Sniegowski 8 11 */ 12 @FramsClassAnnotation 9 13 public class Group { 10 String name; 14 15 @ParamAnnotation 16 protected String name; 11 17 12 18 /** … … 40 46 } 41 47 42 /*43 * (non-Javadoc)44 *45 * @see java.lang.Object#toString()46 */47 48 @Override 48 49 public String toString() { … … 50 51 } 51 52 53 /** 54 * @return the name 55 */ 56 @ParamAnnotation 57 public String getName() { 58 return name; 59 } 60 52 61 } -
java/main/src/main/java/com/framsticks/params/ListAccess.java
r84 r87 75 75 } 76 76 77 } 77 //TODO it could actually be used also 78 @Override 79 public boolean tryAutoAppend(Object object) { 80 return false; 81 } 82 83 }; -
java/main/src/main/java/com/framsticks/params/Param.java
r86 r87 2 2 3 3 4 import com.framsticks.params.types.DecimalParam; 5 import com.framsticks.params.types.StringParam; 4 import javax.annotation.concurrent.Immutable; 5 6 import com.framsticks.params.annotations.FramsClassAnnotation; 7 import com.framsticks.params.annotations.ParamAnnotation; 6 8 7 9 /** … … 14 16 * @author Piotr Śniegowski 15 17 */ 18 @Immutable 19 @FramsClassAnnotation(id = "prop", name = "prop") 16 20 public abstract class Param { 17 21 18 22 /** The parameter id. */ 19 protected String id;23 protected final String id; 20 24 25 //TODO 21 26 /** 22 27 * The parameter internal id. It's set by a user to use user's own getId in 23 28 * code 24 29 */ 25 protected String internalId;30 protected final String internalId; 26 31 27 32 /** The parameter name. */ 28 protected String name;33 protected final String name; 29 34 30 35 /** The help (description) concerning parameter. */ 31 protected String help;36 protected final String help; 32 37 33 38 /** The number of group, that parameter belongs to. */ 34 protected Integer group;39 protected final Integer group; 35 40 36 41 /** The getFlags stored as a bit sum. */ 37 protected Integer flags;42 protected final Integer flags; 38 43 44 //TODO 39 45 /** The variable determining whether the parameter is an extra parameter. */ 40 protected Integer extra;46 protected final Integer extra; 41 47 42 43 public Param() { 44 48 public Param(ParamBuilder builder) { 49 id = builder.getId(); 50 internalId = builder.getInternalId(); 51 name = builder.getName(); 52 help = builder.getHelp(); 53 group = builder.getGroup(); 54 flags = builder.getFlags(); 55 extra = 0; 45 56 } 46 57 58 @ParamAnnotation 47 59 public String getId() { 48 60 return id; … … 53 65 } 54 66 67 @ParamAnnotation 55 68 public String getName() { 56 69 return name; 57 70 } 58 71 72 @ParamAnnotation 59 73 public String getHelp() { 60 74 return help; 61 75 } 62 76 77 @ParamAnnotation 63 78 public Integer getGroup() { 64 79 return group; 65 80 } 66 81 82 @ParamAnnotation 67 83 public Integer getFlags() { 68 84 return flags; … … 79 95 public Integer getExtra() { 80 96 return extra; 81 }82 83 public void setInternalId(String internalId) {84 this.internalId = internalId;85 97 } 86 98 … … 104 116 } 105 117 106 public static FramsClass getFramsClass() {107 return new FramsClass("prop", "prop", null)108 .append(Param.build().id("name").name("Name").type(StringParam.class).finish())109 .append(Param.build().id("id").name("Id").type(StringParam.class).finish())110 .append(Param.build().id("type").name("Type").type(StringParam.class).finish())111 .append(Param.build().id("help").name("Help").type(StringParam.class).finish())112 .append(Param.build().id("flags").name("Flags").type(DecimalParam.class).finish());113 }114 115 118 public static ParamBuilder build() { 116 119 return new ParamBuilder(); 117 120 } 118 121 122 public static ParamBuilder build(FramsClassBuilder builder) { 123 return new ParamBuilder(builder); 124 } 125 119 126 } -
java/main/src/main/java/com/framsticks/params/ParamBuilder.java
r86 r87 4 4 import com.framsticks.params.annotations.ParamAnnotation; 5 5 import com.framsticks.params.types.*; 6 import com.framsticks.util.lang.Numbers; 6 import com.framsticks.util.FramsticksException; 7 import com.framsticks.util.lang.Strings; 7 8 8 9 import org.apache.log4j.Logger; 9 10 10 import java. util.ArrayList;11 import java.lang.reflect.InvocationTargetException; 11 12 import java.util.Arrays; 12 13 import java.util.List; 13 14 14 15 /** … … 56 57 private Class<? extends Param> paramType; 57 58 58 private Param param; 59 private PrimitiveParam primitiveParam; 60 private String typeString; 59 private Object min; 60 61 private Object max; 62 63 private Object def; 64 65 String containedTypeName; 66 67 protected FramsClassBuilder classBuilder; 61 68 62 69 public ParamBuilder() { 70 this(null); 71 } 72 73 74 /** 75 * @param classBuilder 76 */ 77 public ParamBuilder(FramsClassBuilder classBuilder) { 78 this.classBuilder = classBuilder; 79 } 80 81 82 /** 83 * @return the min 84 */ 85 public Object getMin() { 86 return min; 87 } 88 89 /** 90 * @return the max 91 */ 92 public Object getMax() { 93 return max; 94 } 95 96 /** 97 * @return the def 98 */ 99 public Object getDef() { 100 return def; 101 } 102 103 public String getContainedTypeName() { 104 return Strings.notEmpty(containedTypeName) ? containedTypeName : null; 105 } 106 107 /** 108 * @return the enumValues 109 */ 110 public List<String> getEnumValues() { 111 return enumValues; 112 } 113 114 /** 115 * @return the procedureSignature 116 */ 117 public String getProcedureSignature() { 118 return procedureSignature; 119 } 120 121 /** 122 * @return the uid 123 */ 124 public String getUid() { 125 return uid; 63 126 } 64 127 … … 70 133 * when Param getType is not defined 71 134 */ 72 public Param finish() { 73 assert param != null; 74 param.id = id; 75 param.name = name; 76 param.help = help; 77 param.group = group; 78 param.flags = flags; 79 param.internalId = internalId; 80 return param; 135 public Param finish() { 136 try { 137 return paramType.getConstructor(ParamBuilder.class).newInstance(this); 138 } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { 139 throw new FramsticksException().msg("failed to create param").cause(e).arg("name", name); 140 } 81 141 } 82 142 … … 87 147 } 88 148 89 protected <T extends Param> ParamBuilder internalSetType(Class<? extends Param> type, T param) {90 this.paramType = type;91 this.param = param;92 if (param instanceof PrimitiveParam) {93 primitiveParam = (PrimitiveParam) param;94 }95 typeString = param.getFramsTypeName();96 return this;97 }98 99 149 public <T extends Param> ParamBuilder type(Class<T> type) { 100 150 assert type != null; 101 try { 102 return internalSetType(type, type.newInstance()); 103 } catch (InstantiationException | IllegalAccessException e) { 104 e.printStackTrace(); 105 } 106 return this; 107 } 108 109 public <T extends Param> ParamBuilder type(T param) { 110 return internalSetType(param.getClass(), param); 111 } 151 this.paramType = type; 152 return this; 153 } 154 155 // public <T extends Param> ParamBuilder type(T param) { 156 // return internalSetType(param.getClass(), param); 157 // } 112 158 113 159 … … 120 166 } 121 167 168 /** 169 * @return the internalId 170 */ 171 public String getInternalId() { 172 return internalId; 173 } 174 122 175 public ParamBuilder setInternalId(String internalId) { 123 176 this.internalId = internalId; … … 144 197 145 198 protected <T extends Number> void parseMinMaxDefNumber(Class<T> type, String second, String third) { 146 if (primitiveParam == null) {147 log.warn("param is not a primitive param");148 return;149 }150 199 if (second != null) { 151 this.primitiveParam.min = Numbers.parse(second, type); 152 if (this.primitiveParam.min == null) { 153 log.warn("value of min attribute was invalid"); 154 } 200 min = second; 155 201 } 156 202 if (third != null) { 157 this.primitiveParam.max = Numbers.parse(third, type); 158 if (this.primitiveParam.max == null) { 159 log.warn("value of min attribute was invalid"); 160 } 161 } 162 } 203 max = third; 204 } 205 } 206 207 protected List<String> enumValues; 208 209 public ParamBuilder enums(List<String> values) { 210 enumValues = values; 211 return type(EnumParam.class); 212 } 213 214 protected String procedureSignature; 215 216 protected String uid; 163 217 164 218 @ParamAnnotation 165 219 public ParamBuilder type(String type) { 166 typeString = type;220 // typeString = type; 167 221 168 222 log.trace("parsing type: " + type); … … 175 229 switch (first.charAt(0)) { 176 230 case 'o': { 177 type(new ObjectParam(second != null ? second : first.substring(1))); 231 containedTypeName = second != null ? second : first.substring(1); 232 type(ObjectParam.class); 178 233 break; 179 234 } 180 235 case 'p': { 181 ProcedureParam procedureParam = new ProcedureParam(); 182 String signature = type.substring(1); 183 try { 184 procedureParam.parseSignature(signature); 185 } catch (Exception e) { 186 log.error("invalid procedure signature '" + signature + "': " + e); 187 } 188 type(procedureParam); 236 procedureSignature = type.substring(1); 237 type(ProcedureParam.class); 189 238 break; 190 239 } … … 193 242 int tildeIndex = type.indexOf("~"); 194 243 if (tildeIndex != -1) { 195 type(new EnumParam(new ArrayList<String>(Arrays.asList(type.substring(tildeIndex + 1).split("~")))));244 enums(Arrays.asList(type.substring(tildeIndex + 1).split("~"))); 196 245 } else { 197 246 if (first.length() >= 2) { … … 214 263 type(BooleanParam.class); 215 264 } 216 if (param == null) {265 if (paramType == null) { 217 266 type(DecimalParam.class); 218 267 } 219 268 } 220 if ( this.param instanceof DecimalParam) {269 if (DecimalParam.class.isAssignableFrom(this.paramType)) { 221 270 parseMinMaxDefNumber(Integer.class, second, third); 222 271 } … … 243 292 } 244 293 case 'l': { 245 type(third != null ? new UniqueListParam(second, third) : new ArrayListParam(second)); 294 containedTypeName = second; 295 if (third != null) { 296 type(UniqueListParam.class); 297 uid = third; 298 } else { 299 type(ArrayListParam.class); 300 } 246 301 break; 247 302 } … … 294 349 @ParamAnnotation 295 350 public String getType() { 296 return typeString;351 return "?"; 297 352 } 298 353 … … 304 359 } 305 360 306 public <T> ParamBuilder min(T min) { 307 if (primitiveParam != null) { 308 this.primitiveParam.min = min; 309 } 310 return this; 311 } 312 313 public <T> ParamBuilder max(T max) { 314 if (primitiveParam != null) { 315 this.primitiveParam.max = max; 316 } 317 return this; 318 } 319 320 public <T> ParamBuilder def(T def) { 321 if (def != null && primitiveParam != null) { 322 this.primitiveParam.def = def; 323 } 324 return this; 325 } 326 327 public Class<?> getStorageType() { 328 assert param != null; 329 return param.getStorageType(); 330 } 361 public ParamBuilder min(Object min) { 362 this.min = min; 363 return this; 364 } 365 366 public ParamBuilder max(Object max) { 367 this.max = max; 368 return this; 369 } 370 371 public ParamBuilder def(Object def) { 372 this.def = def; 373 return this; 374 } 375 376 // public Class<?> getStorageType() { 377 // assert param != null; 378 // return param.getStorageType(); 379 // } 331 380 332 381 public Param build(String line) throws Exception { … … 386 435 } 387 436 388 437 public ParamBuilder defaultDef(Object def) { 438 if (this.def == null) { 439 return def(def); 440 } 441 return this; 442 } 389 443 390 444 } -
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 } -
java/main/src/main/java/com/framsticks/params/PropertiesAccess.java
r85 r87 27 27 28 28 public PropertiesAccess(FramsClass framsClass) { 29 s etFramsClass(framsClass);29 super(framsClass); 30 30 } 31 31 … … 88 88 } 89 89 90 @Override 91 public boolean tryAutoAppend(Object object) { 92 return false; 93 } 90 94 91 95 } -
java/main/src/main/java/com/framsticks/params/ReflectionAccess.java
r86 r87 4 4 import java.lang.reflect.InvocationTargetException; 5 5 import java.lang.reflect.Method; 6 import java.util.Collections; 6 7 import java.util.HashMap; 7 8 import java.util.Map; 8 9 10 import javax.annotation.concurrent.Immutable; 11 9 12 import org.apache.log4j.Logger; 10 13 14 import com.framsticks.params.annotations.AutoAppendAnnotation; 11 15 import com.framsticks.util.FramsticksException; 16 import com.framsticks.util.lang.Pair; 12 17 13 18 import static com.framsticks.util.lang.Containers.*; … … 22 27 */ 23 28 public class ReflectionAccess extends SimpleAbstractAccess { 24 private final static Logger log = Logger.getLogger(ReflectionAccess.class 25 .getName()); 29 private final static Logger log = Logger.getLogger(ReflectionAccess.class.getName()); 26 30 27 31 protected final Class<?> reflectedClass; 32 protected final Backend backend; 33 28 34 private Object object; 29 35 30 31 protected interface ReflectedSetter { 32 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 33 } 34 35 protected interface ReflectedGetter { 36 public abstract <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 37 } 38 39 protected static class ReflectedValueParam { 40 public ReflectedSetter setter; 41 public ReflectedGetter getter; 42 } 43 44 protected final Map<ValueParam, ReflectedValueParam> reflectedValueParams = new HashMap<>(); 36 @Immutable 37 public static class Backend { 38 39 protected static Map<Pair<Class<?>, FramsClass>, Backend> synchronizedCache = Collections.synchronizedMap(new HashMap<Pair<Class<?>, FramsClass>, Backend>()); 40 41 public static class ReflectedValueParam { 42 public ReflectedSetter setter; 43 public ReflectedGetter getter; 44 } 45 46 public interface ReflectedSetter { 47 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 48 } 49 50 public interface ReflectedGetter { 51 public abstract <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException; 52 } 53 54 protected final Map<ValueParam, ReflectedValueParam> params; 55 protected final Map<Class<?>, Method> autoAppendMethods; 56 57 /** 58 * @param params 59 */ 60 public Backend(Map<ValueParam, ReflectedValueParam> params, Map<Class<?>, Method> autoAppendMethods) { 61 // this.params = Collections.unmodifiableMap(params); 62 this.params = params; 63 this.autoAppendMethods = autoAppendMethods; 64 } 65 66 public static Backend getOrCreateFor(Class<?> reflectedClass, FramsClass framsClass) { 67 68 Pair<Class<?>, FramsClass> id = new Pair<Class<?>, FramsClass>(reflectedClass, framsClass); 69 Backend backend = synchronizedCache.get(id); 70 if (backend != null) { 71 return backend; 72 } 73 74 log.debug("constructing backend for " + id); 75 final Map<ValueParam, ReflectedValueParam> params = new HashMap<>(); 76 77 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(reflectedClass); 78 79 try { 80 for (final ValueParam vp : filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { 81 if (!candidates.containsKey(vp.getId())) { 82 throw new ConstructionException().msg("missing candidate for param").arg("param", vp); 83 } 84 ParamCandidate pc = candidates.get(vp.getId()); 85 if (pc.isReadOnly() && !vp.hasFlag(Flags.READONLY)) { 86 throw new ConstructionException().msg("readonly state conflict").arg("param", vp); 87 } 88 if (!typeMatch(pc.getRawType(), vp.getStorageType())) { 89 throw new ConstructionException().msg("types mismatch for param").arg("param", vp).arg("candidate", pc.getType()).arg("storage", vp.getStorageType()); 90 } 91 92 ReflectedValueParam rvp = new ReflectedValueParam(); 93 params.put(vp, rvp); 94 final boolean primitive = pc.isPrimitive(); 95 if (pc.getField() != null) { 96 final Field f = pc.getField(); 97 rvp.getter = new ReflectedGetter() { 98 @Override 99 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException { 100 return type.cast(f.get(object)); 101 } 102 }; 103 if (!pc.isFinal()) { 104 rvp.setter = new ReflectedSetter() { 105 @Override 106 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException { 107 if (value == null && primitive) { 108 throw new FramsticksException().msg("setting null to primitive value"); 109 } 110 f.set(object, value); 111 } 112 }; 113 } 114 } else { 115 final Method g = pc.getGetter(); 116 117 rvp.getter = new ReflectedGetter() { 118 @Override 119 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 120 return type.cast(g.invoke(object)); 121 } 122 }; 123 124 if (!pc.isFinal()) { 125 final Method s = pc.getSetter(); 126 rvp.setter = new ReflectedSetter() { 127 @Override 128 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 129 if (value == null && primitive) { 130 throw new FramsticksException().msg("setting null to primitive value"); 131 } 132 s.invoke(object, value); 133 } 134 }; 135 } 136 } 137 } 138 } catch (ConstructionException e) { 139 throw e.arg("java class", reflectedClass).arg("framsClass", framsClass); 140 } 141 142 Map<Class<?>, Method> autoAppendMethods = new HashMap<>(); 143 144 Class<?> javaClass = reflectedClass; 145 while (javaClass != null) { 146 147 for (Method m : javaClass.getDeclaredMethods()) { 148 AutoAppendAnnotation a = m.getAnnotation(AutoAppendAnnotation.class); 149 if (a == null) { 150 continue; 151 } 152 Class<?>[] args = m.getParameterTypes(); 153 if (args.length != 1) { 154 throw new ConstructionException().msg("invalid number of arguments in AutoAppend marked method").arg("method", m).arg("arguments", args.length); 155 } 156 autoAppendMethods.put(args[0], m); 157 } 158 159 javaClass = javaClass.getSuperclass(); 160 } 161 162 backend = new Backend(params, autoAppendMethods); 163 synchronizedCache.put(id, backend); 164 return backend; 165 } 166 167 } 45 168 46 169 public ReflectionAccess(Class<?> reflectedClass) throws ConstructionException { 47 this(reflectedClass, FramsClass Builder.buildForClass(reflectedClass));170 this(reflectedClass, FramsClass.build().forClass(reflectedClass)); 48 171 } 49 172 … … 68 191 69 192 public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) throws ConstructionException { 193 super(framsClass); 70 194 this.reflectedClass = reflectedClass; 71 setFramsClass(framsClass); 72 73 Map<String, ParamCandidate> candidates = ParamCandidate.getAllCandidates(reflectedClass); 74 75 try { 76 for (final ValueParam vp : filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { 77 if (!candidates.containsKey(vp.getId())) { 78 throw new ConstructionException().msg("missing candidate for param").arg("param", vp); 79 } 80 ParamCandidate pc = candidates.get(vp.getId()); 81 if (pc.isReadOnly() && !vp.hasFlag(Flags.READONLY)) { 82 throw new ConstructionException().msg("readonly state conflict").arg("param", vp); 83 } 84 if (!typeMatch(pc.getRawType(), vp.getStorageType())) { 85 throw new ConstructionException().msg("types mismatch for param").arg("param", vp).arg("candidate", pc.getType()).arg("storage", vp.getStorageType()); 86 } 87 88 ReflectedValueParam rvp = new ReflectedValueParam(); 89 reflectedValueParams.put(vp, rvp); 90 final boolean primitive = pc.isPrimitive(); 91 if (pc.getField() != null) { 92 final Field f = pc.getField(); 93 rvp.getter = new ReflectedGetter() { 94 @Override 95 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException { 96 return type.cast(f.get(object)); 97 } 98 }; 99 if (!pc.isFinal()) { 100 rvp.setter = new ReflectedSetter() { 101 @Override 102 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException { 103 if (value == null && primitive) { 104 throw new FramsticksException().msg("setting null to primitive value"); 105 } 106 f.set(object, value); 107 } 108 }; 109 } 110 } else { 111 final Method g = pc.getGetter(); 112 113 rvp.getter = new ReflectedGetter() { 114 @Override 115 public <T> T get(Object object, Class<T> type) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 116 return type.cast(g.invoke(object)); 117 } 118 }; 119 120 if (!pc.isFinal()) { 121 final Method s = pc.getSetter(); 122 rvp.setter = new ReflectedSetter() { 123 @Override 124 public <T> void set(Object object, T value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { 125 if (value == null && primitive) { 126 throw new FramsticksException().msg("setting null to primitive value"); 127 } 128 s.invoke(object, value); 129 } 130 }; 131 } 132 } 133 } 134 } catch (ConstructionException e) { 135 throw e.arg("java class", reflectedClass).arg("framsClass", framsClass); 136 } 195 this.backend = Backend.getOrCreateFor(reflectedClass, framsClass); 196 // log.info("created ReflectionAccess " + this); 137 197 } 138 198 … … 149 209 } 150 210 151 return reflectedValueParams.get(param).getter.get(object, type);211 return backend.params.get(param).getter.get(object, type); 152 212 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 153 213 throw new FramsticksException().msg("failed to get").cause(e); … … 169 229 throw new FramsticksException().msg("no object set"); 170 230 } 171 ReflectedSetter s = reflectedValueParams.get(param).setter;231 Backend.ReflectedSetter s = backend.params.get(param).setter; 172 232 if (s == null) { 173 233 throw new FramsticksException().msg("trying to set final"); … … 238 298 } 239 299 240 241 300 @Override 242 301 public ReflectionAccess cloneAccess() throws ConstructionException { … … 265 324 } 266 325 326 @Override 327 public boolean tryAutoAppend(Object value) { 328 assert object != null; 329 for (Map.Entry<Class<?>, Method> a : backend.autoAppendMethods.entrySet()) { 330 if (a.getKey().isAssignableFrom(value.getClass())) { 331 try { 332 a.getValue().invoke(object, value); 333 return true; 334 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { 335 throw new FramsticksException().msg("failed to auto append").cause(e).arg("value", value).arg("into object", object).arg("with method", a.getValue()); 336 } 337 } 338 } 339 return false; 340 } 267 341 } 268 342 -
java/main/src/main/java/com/framsticks/params/Registry.java
r86 r87 5 5 import com.framsticks.params.annotations.FramsClassAnnotation; 6 6 7 import java.util.Collections; 7 8 import java.util.HashMap; 9 import java.util.HashSet; 8 10 import java.util.Map; 11 import java.util.Set; 9 12 10 13 /** … … 14 17 private static final Logger log = Logger.getLogger(Registry.class.getName()); 15 18 16 protected final Map<String, Class<?>> reflectedClasses = new HashMap<String, Class<?>>(); 17 protected final Map<String, String> invertedReflectedClasses = new HashMap<String, String>(); 18 protected final Map<String, FramsClass> infoCache = new HashMap<String, FramsClass>(); 19 public static class DoubleSet<K1, K2, V> { 20 protected final Map<K1, V> byId = new HashMap<>(); 21 protected final Map<K2, V> byName = new HashMap<>(); 22 protected final Set<V> values = new HashSet<>(); 19 23 20 public final void registerReflectedClass(String name, String id, String className) { 21 try { 22 registerReflectedClass(name, id, Class.forName(className)); 23 } catch (ClassNotFoundException e) { 24 log.fatal("class not found during registration: " + e); 24 public int size() { 25 return values.size(); 26 } 27 28 public void put(K1 id, K2 name, V value) { 29 values.add(value); 30 if (id != null) { 31 byId.put(id, value); 32 } 33 if (name != null) { 34 byName.put(name, value); 35 } 36 } 37 38 public boolean containsValue(V value) { 39 return values.contains(value); 40 41 } 42 43 public boolean containsKey(String identifier) { 44 return byId.containsKey(identifier) || byName.containsKey(identifier); 45 } 46 47 public V get(String identifier) { 48 if (byId.containsKey(identifier)) { 49 return byId.get(identifier); 50 } 51 if (byName.containsKey(identifier)) { 52 return byName.get(identifier); 53 } 54 return null; 55 } 56 57 public Set<V> getValues() { 58 return Collections.unmodifiableSet(values); 25 59 } 26 60 } 27 61 28 public void registerReflectedClass(String name, String id, 29 Class<?> reflectedClass) { 30 if (name != null) { 31 reflectedClasses.put(name, reflectedClass); 32 invertedReflectedClasses.put(reflectedClass.getCanonicalName(), name); 33 } 34 if (id != null) { 35 reflectedClasses.put(id, reflectedClass); 36 } 62 // protected void internalRegisterClass(FramsClass framsClass, @Nullable Class<?> javaClass) { 63 // assert framsClass.getName() != null; 64 // assert framsClass.getId() != null; 65 // infoCacheByName.put(framsClass.getName(), framsClass); 66 // infoCacheById.put(framsClass.getId(), framsClass); 67 68 // if (javaClass != null) { 69 // reflectedClasses.put(framsClass, javaClass); 70 // infoCacheByJavaName.put(javaClass.getCanonicalName(), framsClass); 71 // } 72 // } 73 protected final DoubleSet<String, String, Class<?>> javaClasses = new DoubleSet<>(); 74 protected final DoubleSet<String, String, FramsClass> framsClasses = new DoubleSet<>(); 75 76 public void registerReflectedClass(String name, String id, Class<?> reflectedClass) { 77 javaClasses.put(id, name, reflectedClass); 37 78 } 38 79 … … 50 91 51 92 public AccessInterface createAccess(String name, FramsClass framsClass) throws ConstructionException { 52 if (reflectedClasses.containsKey(name)) { 53 return new ReflectionAccess(reflectedClasses.get(name), framsClass); 93 assert framsClasses.containsValue(framsClass); 94 if (javaClasses.containsKey(name)) { 95 return new ReflectionAccess(javaClasses.get(name), framsClass); 54 96 } 55 97 return new PropertiesAccess(framsClass); … … 57 99 58 100 public void putInfoIntoCache(FramsClass framsClass) { 59 if (infoCache.containsKey(framsClass.getId())) {60 log.info("already cached " + framsClass);61 return;62 }63 101 log.debug("caching info for " + framsClass); 64 infoCache.put(framsClass.getId(), framsClass); 65 infoCache.put(framsClass.getName(), framsClass); 102 framsClasses.put(framsClass.getId(), framsClass.getName(), framsClass); 66 103 } 67 104 68 public FramsClass getInfoFromCache(String id ) {69 if (id == null) {105 public FramsClass getInfoFromCache(String identifier) { 106 if (identifier == null) { 70 107 return null; 71 108 } 72 if (infoCache.containsKey(id)) { 73 return infoCache.get(id); 74 } 75 if (invertedReflectedClasses.containsKey(id)) { 76 return getInfoFromCache(invertedReflectedClasses.get(id)); 77 } 78 return null; 109 return framsClasses.get(identifier); 79 110 } 80 111 … … 102 133 } 103 134 135 public Set<Class<?>> getReflectedClasses() { 136 return javaClasses.getValues(); 137 } 138 139 public Set<FramsClass> getInfoCache() { 140 return framsClasses.getValues(); 141 } 142 104 143 } -
java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java
r86 r87 24 24 private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName()); 25 25 26 protected final FramsClass framsClass; 27 28 /** 29 * @param framsClass 30 */ 31 public SimpleAbstractAccess(FramsClass framsClass) { 32 this.framsClass = framsClass; 33 } 34 26 35 @Override 27 36 public final FramsClass getFramsClass() { … … 29 38 } 30 39 31 public void setFramsClass(FramsClass framsClass) {32 this.framsClass = framsClass;33 }34 40 /** 35 41 * Simple String key, value class. … … 51 57 } 52 58 53 protected FramsClass framsClass;54 59 55 60 @Override … … 141 146 @Override 142 147 public void setMin(int i) { 143 PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class);148 PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class); 144 149 if (entry == null) { 145 150 return; … … 160 165 @Override 161 166 public void setMax(int i) { 162 PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class);167 PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class); 163 168 if (entry == null) { 164 169 return; … … 188 193 assert framsClass != null; 189 194 sink.print(framsClass.getId()).print(":").breakLine(); 190 for (PrimitiveParam p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) {195 for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) { 191 196 Object value = get(p, Object.class); 192 197 if (value == null) { -
java/main/src/main/java/com/framsticks/params/UniqueListAccess.java
r85 r87 44 44 return getParam(i); 45 45 } 46 //TODO list access here 46 47 return Param.build().id(id).name(elementAccess.getId()).type("o " + elementAccess.getId()).finish(); 47 48 } -
java/main/src/main/java/com/framsticks/params/ValueParam.java
r84 r87 1 1 package com.framsticks.params; 2 2 3 4 import javax.annotation.concurrent.Immutable; 3 5 4 6 /** 5 7 * @author Piotr Sniegowski 6 8 */ 9 @Immutable 7 10 public abstract class ValueParam extends Param { 11 12 /** 13 * @param builder 14 */ 15 public ValueParam(ParamBuilder builder) { 16 super(builder); 17 } 8 18 9 19 public ReassignResult<?> reassign(Object newValue, Object oldValue) throws CastFailure { … … 11 21 } 12 22 13 public abstract <T> T getDef(Class<T> type) throws ClassCastException;23 public abstract <T> T getDef(Class<T> type); 14 24 15 25 } -
java/main/src/main/java/com/framsticks/params/types/ArrayListParam.java
r84 r87 4 4 import com.framsticks.params.ArrayListAccess; 5 5 import com.framsticks.params.CastFailure; 6 import com.framsticks.params.ParamBuilder; 6 7 import com.framsticks.params.ReassignResult; 7 8 import com.framsticks.util.lang.Casting; … … 12 13 import java.util.List; 13 14 15 import javax.annotation.concurrent.Immutable; 16 14 17 /** 15 18 * @author Piotr Sniegowski 16 19 */ 20 @Immutable 17 21 public class ArrayListParam extends ListParam { 18 22 19 public ArrayListParam( String containedTypeName) {20 super( containedTypeName);23 public ArrayListParam(ParamBuilder builder) { 24 super(builder); 21 25 } 22 26 -
java/main/src/main/java/com/framsticks/params/types/BinaryParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.ParamBuilder; 4 5 import com.framsticks.params.PrimitiveParam; 5 6 import com.framsticks.params.ReassignResult; 6 7 import com.framsticks.util.lang.Numbers; 7 8 9 import javax.annotation.concurrent.Immutable; 10 8 11 /** 9 12 * @author Piotr Sniegowski 10 13 */ 11 public class BinaryParam extends PrimitiveParam { 14 @Immutable 15 public class BinaryParam extends PrimitiveParam<Integer> { 16 17 /** 18 * @param builder 19 */ 20 public BinaryParam(ParamBuilder builder) { 21 super(builder); 22 } 12 23 13 24 @Override -
java/main/src/main/java/com/framsticks/params/types/BooleanParam.java
r84 r87 1 1 package com.framsticks.params.types; 2 2 3 import javax.annotation.concurrent.Immutable; 4 3 5 import com.framsticks.params.CastFailure; 6 import com.framsticks.params.ParamBuilder; 4 7 import com.framsticks.params.PrimitiveParam; 5 8 import com.framsticks.params.ReassignResult; … … 10 13 * @author Piotr Sniegowski 11 14 */ 12 public class BooleanParam extends PrimitiveParam { 15 @Immutable 16 public class BooleanParam extends PrimitiveParam<Boolean> { 13 17 14 public BooleanParam() { 15 def = false; 18 /** 19 * @param builder 20 */ 21 public BooleanParam(ParamBuilder builder) { 22 super(builder.defaultDef(false)); 16 23 } 17 24 -
java/main/src/main/java/com/framsticks/params/types/ColorParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.ParamBuilder; 4 5 import com.framsticks.params.PrimitiveParam; 5 6 import com.framsticks.params.ReassignResult; 7 8 import javax.annotation.concurrent.Immutable; 6 9 7 10 /** 8 11 * @author Piotr Sniegowski 9 12 */ 10 public class ColorParam extends PrimitiveParam { 13 @Immutable 14 public class ColorParam extends PrimitiveParam<Object> { 15 16 17 18 /** 19 * @param builder 20 */ 21 public ColorParam(ParamBuilder builder) { 22 super(builder); 23 } 11 24 12 25 @Override -
java/main/src/main/java/com/framsticks/params/types/DecimalParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.ParamBuilder; 4 5 import com.framsticks.params.ReassignResult; 6 7 import javax.annotation.concurrent.Immutable; 5 8 6 9 /** 7 10 * @author Piotr Sniegowski 8 11 */ 12 @Immutable 9 13 public class DecimalParam extends NumberParam<Integer> { 10 14 11 public DecimalParam() { 12 def = 0; 15 /** 16 * @param builder 17 */ 18 public DecimalParam(ParamBuilder builder) { 19 super(builder.defaultDef(0)); 13 20 } 14 21 -
java/main/src/main/java/com/framsticks/params/types/EnumParam.java
r84 r87 3 3 import java.util.List; 4 4 5 import com.framsticks.params.ParamBuilder; 6 7 import javax.annotation.concurrent.Immutable; 8 5 9 /** 6 10 * @author Piotr Sniegowski 7 11 */ 12 @Immutable 8 13 public class EnumParam extends DecimalParam { 9 14 10 List<String> enums; 15 16 protected final List<String> enums; 11 17 12 18 /** … … 15 21 * @param enums 16 22 */ 17 public EnumParam(List<String> enums) { 18 assert(enums != null); 19 this.enums = enums; 23 public EnumParam(ParamBuilder builder) { 24 super(builder); 25 this.enums = builder.getEnumValues(); 26 assert(this.enums != null); 20 27 } 21 28 -
java/main/src/main/java/com/framsticks/params/types/EventParam.java
r84 r87 2 2 3 3 import com.framsticks.params.Param; 4 import com.framsticks.params.ParamBuilder; 5 6 import javax.annotation.concurrent.Immutable; 4 7 5 8 /** 6 9 * @author Piotr Sniegowski 7 10 */ 11 @Immutable 8 12 public class EventParam extends Param { 13 14 15 16 /** 17 * @param builder 18 */ 19 public EventParam(ParamBuilder builder) { 20 super(builder); 21 } 9 22 10 23 @Override -
java/main/src/main/java/com/framsticks/params/types/FloatParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.ParamBuilder; 4 5 import com.framsticks.params.ReassignResult; 6 7 import javax.annotation.concurrent.Immutable; 5 8 6 9 /** 7 10 * @author Piotr Sniegowski 8 11 */ 12 @Immutable 9 13 public class FloatParam extends NumberParam<Double> { 10 14 11 public FloatParam() { 12 def = 0.0; 15 /** 16 * @param builder 17 */ 18 public FloatParam(ParamBuilder builder) { 19 super(builder.defaultDef(0.0)); 13 20 } 14 21 -
java/main/src/main/java/com/framsticks/params/types/ListParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CompositeParam; 4 import com.framsticks.params.ParamBuilder; 5 6 import javax.annotation.concurrent.Immutable; 4 7 5 8 /** 6 9 * @author Piotr Sniegowski 7 10 */ 11 @Immutable 8 12 public abstract class ListParam extends CompositeParam { 9 13 10 public ListParam( String containedTypeName) {11 super( containedTypeName);14 public ListParam(ParamBuilder builder) { 15 super(builder); 12 16 } 13 17 -
java/main/src/main/java/com/framsticks/params/types/NumberParam.java
r84 r87 3 3 import com.framsticks.params.CastFailure; 4 4 import com.framsticks.params.Flags; 5 import com.framsticks.params.ParamBuilder; 5 6 import com.framsticks.params.PrimitiveParam; 6 7 import com.framsticks.params.ReassignResult; … … 8 9 import com.framsticks.util.lang.Numbers; 9 10 11 import javax.annotation.concurrent.Immutable; 12 10 13 /** 11 14 * @author Piotr Sniegowski 12 15 */ 13 public abstract class NumberParam<T extends Number & Comparable<T>> extends PrimitiveParam { 16 @Immutable 17 public abstract class NumberParam<T extends Number & Comparable<T>> extends PrimitiveParam<T> { 18 19 /** 20 * @param builder 21 */ 22 public NumberParam(ParamBuilder builder) { 23 super(builder); 24 } 14 25 15 26 @Override … … 21 32 T v = null; 22 33 if (newValue instanceof String) { 23 v = Numbers.parse((String) newValue, type);34 v = Numbers.parse((String) newValue, type); 24 35 } else { 25 36 v = Casting.tryCast(type, newValue); -
java/main/src/main/java/com/framsticks/params/types/ObjectParam.java
r84 r87 4 4 import com.framsticks.params.CastFailure; 5 5 import com.framsticks.params.CompositeParam; 6 import com.framsticks.params.ParamBuilder; 6 7 import com.framsticks.params.ReassignResult; 8 9 import javax.annotation.concurrent.Immutable; 7 10 8 11 /** 9 12 * @author Piotr Sniegowski 10 13 */ 14 @Immutable 11 15 public class ObjectParam extends CompositeParam { 12 16 13 public ObjectParam( String containedTypeName) {14 super( containedTypeName);17 public ObjectParam(ParamBuilder builder) { 18 super(builder); 15 19 } 16 20 -
java/main/src/main/java/com/framsticks/params/types/ProcedureParam.java
r85 r87 2 2 3 3 import com.framsticks.params.Param; 4 import com.framsticks.params.ParamBuilder; 5 import com.framsticks.util.FramsticksException; 4 6 import com.framsticks.util.lang.Strings; 5 7 … … 9 11 import java.util.regex.Pattern; 10 12 13 import javax.annotation.concurrent.Immutable; 14 11 15 /** 12 16 * @author Piotr Sniegowski 13 17 */ 18 @Immutable 14 19 public class ProcedureParam extends Param { 15 private Param resultType;16 private List<Param> argumentsType = new ArrayList<Param>();20 private final Param resultType; 21 private final List<Param> argumentsType = new ArrayList<Param>(); 17 22 18 23 private static Pattern addressPattern = Pattern.compile("^([^\\(]+)?\\(([^\\)]*)\\)$"); 24 25 /** 26 * @param builder 27 */ 28 public ProcedureParam(ParamBuilder builder) { 29 super(builder); 30 String signature = builder.getProcedureSignature(); 31 if (Strings.notEmpty(signature)) { 32 Matcher matcher = addressPattern.matcher(signature); 33 if (!matcher.matches()) { 34 throw new FramsticksException().msg("invalid signature"); 35 } 36 String result = Strings.collapse(matcher.group(1)); 37 resultType = (result != null) ? parseType(result, null) : null; 38 String arguments = matcher.group(2); 39 if (!Strings.notEmpty(arguments)) { 40 return; 41 } 42 for (String a : arguments.split(",")) { 43 int space = a.indexOf(' '); 44 String type; 45 String name; 46 if (space == -1) { 47 type = a; 48 name = null; 49 } else { 50 type = a.substring(0, space); 51 name = a.substring(space + 1); 52 } 53 argumentsType.add(parseType(type, name)); 54 } 55 } else { 56 resultType = null; 57 } 58 } 19 59 20 60 protected static Param parseType(String type, String name) { 21 61 return Param.build().type(type).name(name).finish(); 22 62 } 23 24 public void parseSignature(String signature) throws Exception {25 if (!Strings.notEmpty(signature)) {26 return;27 }28 Matcher matcher = addressPattern.matcher(signature);29 if (!matcher.matches()) {30 throw new Exception("invalid signature");31 }32 String result = Strings.collapse(matcher.group(1));33 if (result != null) {34 resultType = parseType(result, null);35 }36 String arguments = matcher.group(2);37 if (!Strings.notEmpty(arguments)) {38 return;39 }40 for (String a : arguments.split(",")) {41 int space = a.indexOf(' ');42 String type;43 String name;44 if (space == -1) {45 type = a;46 name = null;47 } else {48 type = a.substring(0, space);49 name = a.substring(space + 1);50 }51 argumentsType.add(parseType(type, name));52 }53 }54 55 63 56 64 @Override -
java/main/src/main/java/com/framsticks/params/types/StringParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.ParamBuilder; 4 5 import com.framsticks.params.PrimitiveParam; 5 6 import com.framsticks.params.ReassignResult; 6 7 import com.framsticks.params.SinkInterface; 7 8 9 import javax.annotation.concurrent.Immutable; 10 8 11 /** 9 12 * @author Piotr Sniegowski 10 13 */ 11 public class StringParam extends PrimitiveParam { 14 @Immutable 15 public class StringParam extends PrimitiveParam<String> { 16 17 18 19 /** 20 * @param builder 21 */ 22 public StringParam(ParamBuilder builder) { 23 super(builder); 24 } 25 12 26 @Override 13 27 public Class<?> getStorageType() { -
java/main/src/main/java/com/framsticks/params/types/UniqueListParam.java
r84 r87 3 3 import com.framsticks.params.AccessInterface; 4 4 import com.framsticks.params.CastFailure; 5 import com.framsticks.params.ParamBuilder; 5 6 import com.framsticks.params.ReassignResult; 6 7 import com.framsticks.params.UniqueListAccess; … … 10 11 import java.util.Map; 11 12 13 import javax.annotation.concurrent.Immutable; 14 12 15 /** 13 16 * @author Piotr Sniegowski 14 17 */ 18 @Immutable 15 19 public class UniqueListParam extends ListParam { 16 20 17 21 final String uidName; 18 22 19 public UniqueListParam( String containedTypeName, String uidName) {20 super( containedTypeName);21 this.uidName = uidName;23 public UniqueListParam(ParamBuilder builder) { 24 super(builder); 25 this.uidName = builder.getUid(); 22 26 } 23 27 -
java/main/src/main/java/com/framsticks/params/types/UniversalParam.java
r84 r87 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.ParamBuilder; 4 5 import com.framsticks.params.PrimitiveParam; 5 6 import com.framsticks.params.ReassignResult; 7 8 import javax.annotation.concurrent.Immutable; 6 9 7 10 /** 8 11 * @author Piotr Sniegowski 9 12 */ 10 public class UniversalParam extends PrimitiveParam { 13 @Immutable 14 public class UniversalParam extends PrimitiveParam<Object> { 15 16 17 /** 18 * @param builder 19 */ 20 public UniversalParam(ParamBuilder builder) { 21 super(builder); 22 } 23 11 24 @Override 12 25 public Class<?> getStorageType() {
Note: See TracChangeset
for help on using the changeset viewer.