Changeset 84 for java/main/src/main/java/com/framsticks/params
- Timestamp:
- 06/22/13 21:51:33 (11 years ago)
- Location:
- java/main
- Files:
-
- 5 added
- 2 deleted
- 31 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/AccessInterface.java
r78 r84 2 2 3 3 import java.util.Collection; 4 import java.util.List;5 4 6 5 … … 9 8 * framsticks. In descriptions of methods names "property" and "parameter" are 10 9 * synonyms. 11 * 10 * 12 11 * @author Jarek Szymczak <name.surname@gmail.com> (please replace name and 13 12 * surname with my personal data) … … 20 19 21 20 String getId(); 22 23 24 /*25 String getName();26 27 * This method provides more verbose (but not strict) names of classes.28 *29 * e.g. for a list it will return l Type, and if name of FramsClass missing,30 * it's id will be used31 *32 String getNiceName();33 */34 21 35 22 Param getGroupMember(int gi, int n); … … 44 31 <T> T get(String id, Class<T> type); 45 32 46 <T> T get( Param param, Class<T> type);33 <T> T get(ValueParam param, Class<T> type); 47 34 48 35 <T> int set(int i, T value); … … 50 37 <T> int set(String id, T value); 51 38 52 <T> int set( Param param, T value);39 <T> int set(ValueParam param, T value); 53 40 54 41 void setDefault(boolean numericOnly); … … 71 58 72 59 /** 73 * Return as a single line String.74 *75 * @param omitDefaultValues76 * the omit default values77 * @return the string78 */79 String save2(boolean omitDefaultValues);80 81 /**82 60 * Removes all the properties values. 83 61 */ 84 62 void clearValues(); 85 63 86 voidselect(Object object);64 AccessInterface select(Object object); 87 65 88 66 Object getSelected(); … … 90 68 AccessInterface cloneAccess(); 91 69 92 70 Object createAccessee(); 93 71 94 72 Collection<Param> getParams(); 95 73 96 74 FramsClass getFramsClass(); 97 75 98 76 -
java/main/src/main/java/com/framsticks/params/ArrayListAccess.java
r77 r84 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.util. Numbers;3 import com.framsticks.util.lang.Numbers; 4 4 5 5 import java.util.ArrayList; … … 13 13 public class ArrayListAccess extends ListAccess { 14 14 15 Listlist;15 List<Object> list; 16 16 17 public ArrayListAccess(AccessInterface elementAccess) { 18 super(elementAccess); 19 } 17 20 18 public ArrayListAccess(AccessInterface elementAccess) { 19 super(elementAccess); 20 } 21 @Override 22 public ArrayListAccess cloneAccess() { 23 return new ArrayListAccess(elementAccess.cloneAccess()); 24 } 21 25 22 23 public ArrayListAccess cloneAccess() {24 return new ArrayListAccess(elementAccess.cloneAccess());25 26 @Override 27 public List<?> createAccessee() { 28 return new ArrayList<Object>(); 29 } 26 30 27 28 public List createAccessee() {29 return new ArrayList();30 31 @Override 32 public Param getParam(int i) { 33 return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build(); 34 } 31 35 32 @Override 33 public Param getParam(int i) { 34 return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build(); 35 } 36 @Override 37 public Param getParam(String id) { 38 Integer i = Numbers.parse(id, Integer.class); 39 return (i == null ? null : getParam(i)); 40 } 36 41 37 @Override 38 public Param getParam(String id) { 39 Integer i = Numbers.parse(id, Integer.class); 40 return (i == null ? null : getParam(i)); 41 } 42 @Override 43 public String getId() { 44 return "l " + elementAccess.getId(); 45 } 42 46 43 44 public String getId() {45 return "l " + elementAccess.getId();46 47 @Override 48 public int getParamCount() { 49 return list.size(); 50 } 47 51 48 @Override 49 public int getParamCount() { 50 return list.size(); 51 } 52 @Override 53 public <T> T get(int i, Class<T> type) { 54 if (i < list.size()) { 55 return type.cast(list.get(i)); 56 } 57 return null; 58 } 52 59 53 @Override 54 public <T> T get(int i, Class<T> type) { 55 if (i < list.size()) { 56 return type.cast(list.get(i)); 57 } 58 return null; 59 } 60 @Override 61 public <T> T get(String id, Class<T> type) { 62 return get(Integer.parseInt(id), type); 63 } 60 64 61 62 public <T> T get(String id, Class<T> type) {63 return get(Integer.parseInt(id), type);64 65 @Override 66 public <T> T get(ValueParam param, Class<T> type) { 67 return get(param.getId(), type); 68 } 65 69 66 @Override 67 public <T> T get(Param param, Class<T> type) { 68 return get(param.getId(), type); 69 } 70 @Override 71 public <T> int set(int i, T value) { 72 while (i >= list.size()) { 73 list.add(null); 74 } 75 list.set(i, value); 76 return 0; 77 } 70 78 71 @Override 72 public <T> int set(int i, T value) { 73 while (i >= list.size()) { 74 list.add(null); 75 } 76 list.set(i, value); 77 return 0; 78 } 79 @Override 80 public <T> int set(String id, T value) { 81 return set(Integer.parseInt(id), value); 82 } 79 83 80 81 public <T> int set(String id, T value) {82 return set(Integer.parseInt(id), value);83 84 @Override 85 public <T> int set(ValueParam param, T value) { 86 return set(param.getId(), value); 87 } 84 88 85 86 public <T> int set(Param param, T value) {87 return set(param.getId(), value);88 89 @Override 90 public void clearValues() { 91 list.clear(); 92 } 89 93 90 @Override 91 public void clearValues() { 92 list.clear(); 93 } 94 /** covariant */ 95 @Override 96 public List<Object> getSelected() { 97 return list; 98 } 94 99 95 /** covariant */ 96 @Override 97 public List getSelected() { 98 return list; 99 } 100 @SuppressWarnings("unchecked") 101 @Override 102 public ArrayListAccess select(Object object) { 103 list = (List<Object>) object; 104 return this; 105 } 100 106 101 102 public void select(Object object) {103 list = (List)object;104 107 @Override 108 public String computeIdentifierFor(Object selected) { 109 return Integer.toString(list.size()); 110 } 105 111 106 @Override 107 public String computeIdentifierFor(Object selected) { 108 return Integer.toString(list.size()); 109 } 110 111 @Override 112 public Collection<Param> getParams() { 113 List<Param> result = new LinkedList<Param>(); 114 if (list == null) { 115 return result; 116 } 117 for (int i = 0; i < list.size(); ++i) { 118 result.add(getParam(i)); 119 } 120 return result; 121 } 112 @Override 113 public Collection<Param> getParams() { 114 List<Param> result = new LinkedList<Param>(); 115 if (list == null) { 116 return result; 117 } 118 for (int i = 0; i < list.size(); ++i) { 119 result.add(getParam(i)); 120 } 121 return result; 122 } 122 123 123 124 -
java/main/src/main/java/com/framsticks/params/CastFailure.java
r77 r84 5 5 */ 6 6 public class CastFailure extends Exception { 7 8 /** 9 * 10 */ 11 private static final long serialVersionUID = 2117813329617553801L; 12 7 13 } -
java/main/src/main/java/com/framsticks/params/Flags.java
r77 r84 3 3 import java.lang.reflect.Field; 4 4 import org.apache.log4j.Logger; 5 6 import com.framsticks.util.lang.Delimeted; 5 7 6 8 /** … … 9 11 * 10 12 * Based on c++ defines located in: cpp/gdk/param.h 11 * 13 * 12 14 * @author Jarek Szymczak <name.surname@gmail.com> 13 15 * (please replace name and surname with my personal data) … … 17 19 public final class Flags { 18 20 19 private final static Logger logger = Logger.getLogger(Flags.class.getName()); 20 21 private final static Logger log = Logger.getLogger(Flags.class.getName()); 21 22 22 23 public static final int READONLY = 1; … … 48 49 public static final int PSET_HITMAX = 8; 49 50 51 public static String write(int flags, String empty) { 52 Delimeted d = new Delimeted("+", empty); 53 try { 54 for (Field f : Flags.class.getDeclaredFields()) { 55 if (f.getType() != int.class) { 56 continue; 57 } 58 if ((flags & f.getInt(null)) != 0) { 59 d.append(f.getName()); 60 } 61 } 62 } catch (IllegalArgumentException e) { 63 e.printStackTrace(); 64 } catch (IllegalAccessException e) { 65 e.printStackTrace(); 66 } 67 return d.build(); 68 } 69 50 70 public static Integer read(String flags) { 51 71 Integer allFlags = 0; … … 59 79 allFlags |= Integer.parseInt(field.get(null).toString()); 60 80 } catch (SecurityException e) { 61 logger.warn("security exception was thrown while trying to read flag (" 62 + flag + ")"); 81 log.warn("security exception was thrown while trying to read flag (" + flag + ")"); 63 82 } catch (NoSuchFieldException e) { 64 log ger.warn("selected flag is not known (" + flag + ")");83 log.warn("selected flag is not known (" + flag + ")"); 65 84 } catch (IllegalArgumentException e) { 66 log ger.warn("selected flag is not known (" + flag + ")");85 log.warn("selected flag is not known (" + flag + ")"); 67 86 } catch (IllegalAccessException e) { 68 log ger.warn("selected flag is not known (" + flag + ")");87 log.warn("selected flag is not known (" + flag + ")"); 69 88 } 70 89 } -
java/main/src/main/java/com/framsticks/params/FramsClass.java
r78 r84 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.params.types.CompositeParam;4 import com.framsticks.params.types.DecimalParam;5 import com.framsticks.params.types.FloatParam;6 3 import com.framsticks.params.types.StringParam; 7 4 import com.framsticks.parsers.FileSource; 8 5 import com.framsticks.parsers.Loaders; 9 import com.framsticks.util.Casting; 6 import com.framsticks.util.lang.Casting; 7 10 8 import org.apache.log4j.Logger; 11 9 12 import javax.lang.model.element.TypeElement;13 10 import java.io.InputStream; 14 11 import java.lang.reflect.*; 15 12 import java.util.*; 16 import java.util.List;17 13 18 14 /** … … 30 26 public final class FramsClass { 31 27 32 private final static Logger LOGGER= Logger.getLogger(FramsClass.class.getName());33 34 35 36 28 private final static Logger log = Logger.getLogger(FramsClass.class.getName()); 29 30 /** 31 * The Class which represents group. 32 */ 37 33 38 34 /** The offset of the parameter (applied for newly added parameter). */ … … 75 71 /** 76 72 * Adds new param entry. 77 * 73 * 78 74 * @param param 79 75 * the new param entry … … 113 109 /** 114 110 * Gets the group member. 115 * 111 * 116 112 * @param gi 117 113 * the offset of group … … 123 119 if (gi < 0 || pi < 0 || gi >= groups.size()) { 124 120 return null; 125 121 } 126 122 Group group = groups.get(gi); 127 123 return (group != null ? group.getProperty(pi) : null); … … 130 126 /** 131 127 * Gets the group getName. 132 * 128 * 133 129 * @param gi 134 130 * the offset of group … … 149 145 } 150 146 151 152 153 147 public String getNiceName() { 148 return name != null ? name : id; 149 } 154 150 155 151 /** 156 152 * Gets the param entry. 157 * 153 * 158 154 * @param i 159 155 * the offset of parameter 160 156 * @return the param entry 161 157 */ 162 public Param getParamEntry(int i) {158 public <T extends Param> T getParamEntry(int i, Class<T> type) { 163 159 if (i < 0 || i >= paramList.size()) { 164 160 return null; 165 161 } 166 return paramList.get(i);162 return Casting.tryCast(type, paramList.get(i)); 167 163 } 168 164 169 165 /** 170 166 * Gets the param entry. 171 * 167 * 172 168 * @param id 173 169 * the getId of parameter 174 170 * @return the param entry 175 171 */ 176 public Param getParamEntry(String id) {177 return paramEntryMap.get(id);172 public <T extends Param> T getParamEntry(String id, Class<T> type) { 173 return Casting.tryCast(type, paramEntryMap.get(id)); 178 174 } 179 175 … … 206 202 } 207 203 208 209 210 211 212 213 214 215 216 return "l " + ((Class) containedType).getCanonicalName() + " name";217 218 204 public static String getParamTypeForNativeType(Type type) { 205 if (type instanceof ParameterizedType) { 206 ParameterizedType p = (ParameterizedType) type; 207 Type rawType = p.getRawType(); 208 if (rawType.equals(Map.class)) { 209 Type containedType = p.getActualTypeArguments()[1]; 210 //TODO uid should be passed along during construction 211 if (containedType instanceof Class) { 212 return "l " + ((Class<?>) containedType).getCanonicalName() + " name"; 213 } 214 } 219 215 if (rawType.equals(List.class)) { 220 216 Type containedType = p.getActualTypeArguments()[0]; 221 217 if (containedType instanceof Class) { 222 return "l " + ((Class ) containedType).getCanonicalName();223 } 224 } 225 226 227 228 229 230 231 232 233 234 235 236 237 238 return "o " + ((Class) type).getCanonicalName();239 240 241 242 243 218 return "l " + ((Class<?>) containedType).getCanonicalName(); 219 } 220 } 221 return null; 222 } 223 224 if (type.equals(Integer.class)) { 225 return "d"; 226 } 227 if (type.equals(String.class)) { 228 return "s"; 229 } 230 if (type.equals(Double.class)) { 231 return "f"; 232 } 233 if (type instanceof Class) { 234 return "o " + ((Class<?>) type).getCanonicalName(); 235 } 236 return null; 237 } 238 239 public static final String GENERATE_HELP_PREFIX = "automatically generated from: "; 244 240 245 241 public static FramsClass readFromStream(InputStream stream) { … … 248 244 249 245 public static class Constructor { 250 protected final FramsClass result; 251 protected Class currentClass; 252 public Constructor(Class src, String name) { 253 result = new FramsClass(name, name, GENERATE_HELP_PREFIX + src.toString()); 254 currentClass = src; 255 while (currentClass != null) { 256 try { 257 currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this); 258 } catch (Exception ignored) { 259 } 260 currentClass = currentClass.getSuperclass(); 261 } 246 protected final FramsClass result; 247 protected Class<?> currentClass; 248 249 public Constructor(Class<?> src, String name) { 250 result = new FramsClass(name, name, GENERATE_HELP_PREFIX + src.toString()); 262 251 currentClass = src; 263 } 264 265 public final FramsClass getResult() { 266 return result; 267 } 252 while (currentClass != null) { 253 try { 254 currentClass.getMethod("constructFramsClass", Constructor.class).invoke(null, this); 255 } catch (Exception ignored) { 256 } 257 currentClass = currentClass.getSuperclass(); 258 } 259 currentClass = src; 260 } 261 262 public final FramsClass getResult() { 263 return result; 264 } 268 265 269 266 public Constructor allFields() { … … 274 271 } 275 272 276 277 278 279 280 281 282 283 284 285 286 Class[] args = method.getParameterTypes();287 288 289 290 291 292 293 294 295 296 297 298 299 LOGGER.fatal("method " + name + " was not found in " + currentClass.toString());300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 LOGGER.fatal("field " + name + " was not found in " + currentClass.toString());319 320 321 322 273 public Constructor method(String name, Class<?> ... arguments) { 274 try { 275 Method method = currentClass.getMethod(name, arguments); 276 if (!Modifier.isPublic(method.getModifiers())) { 277 return this; 278 } 279 String returnParamClass = getParamTypeForNativeType(method.getGenericReturnType()); 280 if (returnParamClass == null) { 281 return this; 282 } 283 Class<?>[] args = method.getParameterTypes(); 284 if (args.length == 0) { 285 if (method.getName().startsWith("get")) { 286 String fieldName = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); 287 Param param = new ParamBuilder().setType(returnParamClass).setName(fieldName).setId(fieldName).setHelp(GENERATE_HELP_PREFIX + method.toString()).build(); 288 assert param != null; 289 result.append(param); 290 return this; 291 } 292 return this; 293 } 294 return this; 295 } catch (NoSuchMethodException e) { 296 log.fatal("method " + name + " was not found in " + currentClass.toString()); 297 } 298 return this; 299 } 300 public Constructor field(String name) { 301 try { 302 Field field = currentClass.getField(name); 303 if (!Modifier.isPublic(field.getModifiers())) { 304 return this; 305 } 306 String paramClass = getParamTypeForNativeType(field.getGenericType()); 307 if (paramClass == null) { 308 return this; 309 } 310 Param param = new ParamBuilder().setType(paramClass).setName(field.getName()).setId(field.getName()).setHelp(GENERATE_HELP_PREFIX + field.toString()).build(); 311 assert param != null; 312 result.append(param); 313 return this; 314 } catch (NoSuchFieldException e) { 315 log.fatal("field " + name + " was not found in " + currentClass.toString()); 316 } 317 return this; 318 } 319 } 323 320 } -
java/main/src/main/java/com/framsticks/params/ListAccess.java
r78 r84 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.core.Node;4 import com.framsticks.core.Path;5 import com.framsticks.params.types.CompositeParam;6 3 7 import java.util.List; 4 5 import static com.framsticks.util.lang.Containers.filterInstanceof; 8 6 9 7 /** … … 18 16 } 19 17 20 18 @Override 21 19 public Param getGroupMember(int gi, int n) { 22 20 return null; … … 52 50 53 51 @Override 54 public void save(SinkInterface sink) { 55 for (Param p : getParams()) { 56 assert p instanceof CompositeParam; 57 CompositeParam childParam = (CompositeParam)p; 58 Object child = get(childParam, Object.class); 59 //this is probably an assertion 60 assert child != null; 61 elementAccess.select(child); 62 elementAccess.save(sink); 63 } 64 } 52 public void save(SinkInterface sink) { 53 for (CompositeParam p : filterInstanceof(getParams(), CompositeParam.class)) { 54 Object child = get(p, Object.class); 55 //this is probably an assertion 56 assert child != null; 57 elementAccess.select(child); 58 elementAccess.save(sink); 59 } 60 } 65 61 66 62 @Override 67 63 public void load(SourceInterface stream) throws Exception { 68 }69 70 @Override71 public String save2(boolean omitDefaultValues) {72 return null;73 64 } 74 65 … … 77 68 } 78 69 79 70 public abstract String computeIdentifierFor(Object selected); 80 71 81 82 83 84 72 @Override 73 public final FramsClass getFramsClass() { 74 return elementAccess.getFramsClass(); 75 } 85 76 86 77 } -
java/main/src/main/java/com/framsticks/params/ListSink.java
r77 r84 8 8 */ 9 9 public class ListSink implements SinkInterface { 10 11 10 protected final List<String> out; 11 protected StringBuilder lineBuilder = new StringBuilder(); 12 12 13 14 15 13 public ListSink(List<String> out) { 14 this.out = out; 15 } 16 16 17 18 19 17 public ListSink() { 18 this.out = new LinkedList<String>(); 19 } 20 20 21 22 23 21 public final List<String> getOut() { 22 return out; 23 } 24 24 25 25 26 27 28 29 30 26 @Override 27 public SinkInterface print(String str) { 28 lineBuilder.append(str); 29 return this; 30 } 31 31 32 33 34 35 36 32 @Override 33 public SinkInterface print(Object obj) { 34 lineBuilder.append(obj); 35 return this; 36 } 37 37 38 39 40 41 42 38 @Override 39 public void breakLine() { 40 out.add(lineBuilder.toString()); 41 lineBuilder = new StringBuilder(); 42 } 43 43 44 45 46 44 @Override 45 public void close() { 46 } 47 47 } -
java/main/src/main/java/com/framsticks/params/ListSource.java
r77 r84 7 7 8 8 private Iterator<String> iterator = null; 9 private final List<String> source;9 //private final List<String> source; 10 10 11 11 public ListSource(List<String> source) { 12 this.source = source;12 //this.source = source; 13 13 iterator = source.iterator(); 14 14 } -
java/main/src/main/java/com/framsticks/params/Param.java
r78 r84 3 3 import com.framsticks.params.types.DecimalParam; 4 4 import com.framsticks.params.types.StringParam; 5 6 import java.util.HashMap;7 import java.util.Map;8 5 9 6 /** … … 42 39 protected Integer extra; 43 40 44 /** The minimum allowed value of parameter. */45 protected Object min;46 47 /** The maximum allowed value of parameter. */48 protected Object max;49 50 /** The default value of parameter. */51 protected Object def;52 53 54 55 /**56 * The defaults used for Integer, Double and String classes, if the default57 * value of parameter is not explicitly given.58 */59 @SuppressWarnings("rawtypes")60 private Map<Class, Object> defaults = new HashMap<Class, Object>();61 // static initialization of defaults62 {63 defaults.put(String.class, "");64 defaults.put(Integer.class, 0);65 defaults.put(Double.class, 0.0);66 }67 41 68 42 public Param() { … … 94 68 } 95 69 96 public abstract String getType();70 public abstract String getFramsTypeName(); 97 71 98 72 … … 101 75 } 102 76 103 104 private <T> T tryCastAndReturn(Object value, Class<T> type) {105 if (value == null)106 return null;107 try {108 return type.cast(value);109 } catch (ClassCastException e) {110 throw new ClassCastException("property \"" + name111 + "\" type is \"" + value.getClass().getName()112 + "\", not \"" + type.getName() + "\"");113 }114 }115 /**116 * Gets the minimum value of parameter.117 *118 * @param <T> the generic getType which must be correctly specified by user119 * @param type the getType of variable, can be checked by120 * @return the minimum allowed value of parameter121 * @throws ClassCastException the class cast exception, thrown when given getType is incorrect122 */123 public <T> T getMin(Class<T> type) {124 return tryCastAndReturn(min, type);125 }126 127 /**128 * Gets the maximum value of parameter.129 *130 * @param <T> the generic getType which must be correctly specified by user131 * @param type the getType of variable, can be checked by {@link #getStorageType()}132 * @return the maximum allowed value of parameter133 * @throws ClassCastException the class cast exception, thrown when given getType is incorrect134 */135 public <T> T getMax(Class<T> type) {136 return tryCastAndReturn(max, type);137 }138 139 /**140 * Gets the default value of parameter.141 *142 * @param <T> the generic getType which must be correctly specified by user143 * @param type the getType of variable, can be checked by {@link #getStorageType()}144 * @return the default value of parameter145 * @throws ClassCastException the class cast exception, thrown when given getType is incorrect146 */147 public <T> T getDef(Class<T> type) throws ClassCastException {148 return tryCastAndReturn(def, type);149 }150 77 151 78 public Integer getExtra() { … … 166 93 } 167 94 168 public boolean isEmptyAvailable() { 169 return false; 170 } 171 172 public abstract Class getStorageType(); 95 public abstract Class<?> getStorageType(); 173 96 174 97 public boolean hasFlag(int flag) { … … 176 99 } 177 100 178 public Object reassign(Object newValue, Object oldValue) throws CastFailure {179 if (newValue == null) {180 if (isEmptyAvailable()) {181 return null;182 }183 throw new CastFailure();184 }185 try {186 return getStorageType().cast(newValue);187 } catch (ClassCastException ignored) {188 throw new CastFailure();189 }190 }191 192 101 public boolean isUserHidden() { 193 102 return (flags & Flags.USERHIDDEN) != 0; 194 103 } 195 104 196 197 198 199 200 201 202 203 105 public static FramsClass getFramsClass() { 106 return new FramsClass("prop", "prop", null) 107 .append(new ParamBuilder().setId("name").setName("Name").setType(StringParam.class).build()) 108 .append(new ParamBuilder().setId("id").setName("Id").setType(StringParam.class).build()) 109 .append(new ParamBuilder().setId("type").setName("Type").setType(StringParam.class).build()) 110 .append(new ParamBuilder().setId("help").setName("Help").setType(StringParam.class).build()) 111 .append(new ParamBuilder().setId("flags").setName("Flags").setType(DecimalParam.class).build()); 112 } 204 113 205 114 } -
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; -
java/main/src/main/java/com/framsticks/params/PropertiesAccess.java
r78 r84 1 1 package com.framsticks.params; 2 2 3 import java.util.ArrayList;4 3 import java.util.HashMap; 5 import java.util.List;6 4 import java.util.Map; 5 7 6 8 7 /** 9 8 * The Class PropertiesAccess. 10 * 9 * 11 10 * @author Jarek Szymczak <name.surname@gmail.com> (please replace name and 12 11 * surname with my personal data) … … 18 17 public Map<String, Object> properties; 19 18 20 19 @Override 21 20 public Map<String, Object> createAccessee() { 22 21 return PropertiesAccess.createPropertiesMap(); … … 33 32 @Override 34 33 public void clearValues() { 35 36 34 assert properties != null; 35 properties.clear(); 37 36 } 38 37 39 38 @Override 40 public <T> T get( Param param, Class<T> type) {41 42 39 public <T> T get(ValueParam param, Class<T> type) { 40 assert properties != null; 41 assert param != null; 43 42 Object object = null; 44 43 try { … … 54 53 } 55 54 56 protected <T> void internalSet(Param param, T value) { 55 @Override 56 protected <T> void internalSet(ValueParam param, T value) { 57 57 properties.put(param.getId(), value); 58 58 } … … 63 63 * correct exceptions might occurred while getting / setting the parameters 64 64 * values 65 * 65 * 66 66 * @param object 67 67 * the properties with parameters values 68 68 */ 69 @SuppressWarnings("unchecked") 69 70 @Override 70 public voidselect(Object object) {71 public PropertiesAccess select(Object object) { 71 72 this.properties = (Map<String, Object>)object; 73 return this; 72 74 } 73 75 … … 81 83 public PropertiesAccess cloneAccess() { 82 84 return new PropertiesAccess(framsClass); 83 //properties = createProperties();84 //return access;85 85 } 86 86 -
java/main/src/main/java/com/framsticks/params/ReflectionAccess.java
r78 r84 4 4 import java.lang.reflect.InvocationTargetException; 5 5 import java.lang.reflect.Modifier; 6 import java.lang.reflect.Type;7 import java.util.List;8 6 9 import com.framsticks.params.types.ListParam;10 7 import org.apache.log4j.Logger; 8 9 import com.framsticks.util.lang.Containers; 11 10 12 11 13 12 /** 14 13 * The Class ReflectionAccess. Stores data in provided object using reflection. 15 * 14 * 16 15 * @author Mateusz Jarus <name.surname@gmail.com> (please replace name and 17 16 * surname with my personal data) … … 20 19 */ 21 20 public class ReflectionAccess extends SimpleAbstractAccess { 22 private final static Logger LOGGER = Logger.getLogger(ReflectionAccess.class 23 .getName()); 21 private final static Logger log = Logger.getLogger(ReflectionAccess.class.getName()); 24 22 25 26 protected final Class reflectedClass; 23 protected final Class<?> reflectedClass; 27 24 private Object object; 28 25 29 public ReflectionAccess(Class reflectedClass, FramsClass framsClass) {30 26 public ReflectionAccess(Class<?> reflectedClass, FramsClass framsClass) { 27 this.reflectedClass = reflectedClass; 31 28 setFramsClass(framsClass); 32 29 } … … 37 34 38 35 @Override 39 public <T> T get( Param param, Class<T> type) {36 public <T> T get(ValueParam param, Class<T> type) { 40 37 if (object == null) { 41 38 return null; … … 54 51 } catch (InvocationTargetException e) { 55 52 //e.printStackTrace(); 56 53 } 57 54 58 55 } catch (IllegalAccessException ex) { 59 LOGGER.warn("illegal access error occurred while trying to access returnedObject");56 log.warn("illegal access error occurred while trying to access returnedObject"); 60 57 ex.printStackTrace(); 61 58 } catch (ClassCastException ignored) { 62 59 63 60 } 64 61 return null; 65 62 } 66 63 67 64 @Override 68 protected <T> void internalSet( Param param, T value) {65 protected <T> void internalSet(ValueParam param, T value) { 69 66 setValue(param, value); 70 67 } 71 68 72 private <T> void setValue( Param param, T value) {69 private <T> void setValue(ValueParam param, T value) { 73 70 if (object == null) { 74 71 return; 75 72 } 76 73 try { 77 74 String id = param.getId(); 78 75 try { 79 76 Field f = reflectedClass.getField(id); 80 Class t = f.getType();77 Class<?> t = f.getType(); 81 78 if (Modifier.isFinal(f.getModifiers())) { 82 79 return; … … 111 108 112 109 try { 113 for ( Param p : framsClass.getParamEntries()) {110 for (ValueParam p : Containers.filterInstanceof(framsClass.getParamEntries(), ValueParam.class)) { 114 111 setValue(p, p.getDef(Object.class)); 115 112 } … … 121 118 /** 122 119 * Sets the new object to operate on. 123 * 120 * 124 121 * @param object 125 122 * new object to operate on 126 123 */ 127 124 @Override 128 public voidselect(Object object) {125 public ReflectionAccess select(Object object) { 129 126 assert object == null || reflectedClass.isInstance(object); 130 127 this.object = object; 128 return this; 131 129 } 132 130 … … 159 157 } 160 158 161 162 163 164 165 166 167 168 169 170 LOGGER.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId());171 172 159 @Override 160 public Object createAccessee() { 161 try { 162 return reflectedClass.newInstance(); 163 } catch (InstantiationException e) { 164 e.printStackTrace(); 165 } catch (IllegalAccessException e) { 166 e.printStackTrace(); 167 } 168 log.fatal("failed to create reflected object of class " + reflectedClass.getCanonicalName() + " for frams type " + framsClass.getId()); 169 return null; 170 } 173 171 } -
java/main/src/main/java/com/framsticks/params/Registry.java
r78 r84 10 10 */ 11 11 public class Registry { 12 private static final Logger LOGGER= Logger.getLogger(Registry.class.getName());12 private static final Logger log = Logger.getLogger(Registry.class.getName()); 13 13 14 protected final Map<String, Class > reflectedClasses = new HashMap<String, Class>();14 protected final Map<String, Class<?>> reflectedClasses = new HashMap<String, Class<?>>(); 15 15 protected final Map<String, String> invertedReflectedClasses = new HashMap<String, String>(); 16 16 protected final Map<String, FramsClass> infoCache = new HashMap<String, FramsClass>(); … … 20 20 registerReflectedClass(name, id, Class.forName(className)); 21 21 } catch (ClassNotFoundException e) { 22 LOGGER.fatal("class not found during registration: " + e);22 log.fatal("class not found during registration: " + e); 23 23 } 24 24 } 25 25 26 public void registerReflectedClass(String name, String id, Class reflectedClass) { 26 public void registerReflectedClass(String name, String id, 27 Class<?> reflectedClass) { 27 28 if (name != null) { 28 29 reflectedClasses.put(name, reflectedClass); … … 43 44 public void putInfoIntoCache(FramsClass framsClass) { 44 45 if (infoCache.containsKey(framsClass.getId())) { 45 LOGGER.info("already cached " + framsClass);46 log.info("already cached " + framsClass); 46 47 return; 47 48 } 48 LOGGER.debug("caching info for " + framsClass);49 log.debug("caching info for " + framsClass); 49 50 infoCache.put(framsClass.getId(), framsClass); 50 51 infoCache.put(framsClass.getName(), framsClass); … … 63 64 return null; 64 65 } 66 67 public static AccessInterface wrapAccessWithListIfNeeded(CompositeParam param, AccessInterface access) { 68 if (access == null) { 69 return null; 70 } 71 return param.prepareAccessInterface(access); 72 } 73 74 public AccessInterface prepareAccess(CompositeParam param) { 75 return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName())); 76 } 77 78 public AccessInterface createAccess(String name) { 79 if (name == null) { 80 return null; 81 } 82 FramsClass framsClass = getInfoFromCache(name); 83 if (framsClass == null) { 84 return null; 85 } 86 87 return createAccess(name, framsClass); 88 } 89 65 90 } -
java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java
r78 r84 2 2 3 3 import java.io.IOException; 4 import java.text.DecimalFormat;5 import java.text.DecimalFormatSymbols;6 import java.util.ArrayList;7 4 import java.util.Collection; 8 import java.util.List; 9 10 import com.framsticks.params.types.*; 5 6 import static com.framsticks.util.lang.Containers.filterInstanceof; 7 11 8 import org.apache.log4j.Logger; 12 import java.util.Locale;13 9 14 10 /** … … 26 22 public abstract class SimpleAbstractAccess implements AccessInterface { 27 23 28 private final static Logger LOGGER = Logger.getLogger(SimpleAbstractAccess.class.getName()); 29 30 31 32 @Override 24 private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName()); 25 26 @Override 33 27 public final FramsClass getFramsClass() { 34 28 return framsClass; … … 38 32 this.framsClass = framsClass; 39 33 } 40 34 /** 41 35 * Simple String key, value class. 42 36 */ … … 64 58 } 65 59 66 60 @Override 67 61 public int getParamCount() { 68 62 return framsClass.getParamCount(); … … 71 65 @Override 72 66 public Param getParam(int i) { 73 return framsClass.getParamEntry(i );67 return framsClass.getParamEntry(i, Param.class); 74 68 } 75 69 76 70 @Override 77 71 public Param getParam(String id) { 78 return framsClass.getParamEntry(id); 79 } 80 72 return framsClass.getParamEntry(id, Param.class); 73 } 81 74 82 75 @Override … … 87 80 @Override 88 81 public <T> T get(int i, Class<T> type) { 89 return get(getParam(i), type); 82 Param p = getParam(i); 83 assert p instanceof ValueParam; 84 return get((ValueParam) p, type); 90 85 } 91 86 92 87 @Override 93 88 public <T> T get(String id, Class<T> type) { 94 return get(getParam(id), type); 95 } 96 89 Param p = getParam(id); 90 assert p instanceof ValueParam; 91 return get((ValueParam) p, type); 92 } 97 93 98 94 @Override 99 95 public <T> int set(String id, T value) { 100 return set(getParam(id), value); 96 Param p = getParam(id); 97 assert p instanceof ValueParam; 98 return set((ValueParam) p, value); 101 99 } 102 100 103 101 @Override 104 102 public <T> int set(int i, T value) { 105 return set(getParam(i), value); 106 } 107 108 @SuppressWarnings("unchecked") 109 @Override 110 public <T> int set(Param param, T value) { 103 Param p = getParam(i); 104 assert p instanceof ValueParam; 105 return set((ValueParam) p, value); 106 } 107 108 @Override 109 public <T> int set(ValueParam param, T value) { 111 110 int flags = 0; 111 112 112 //String id = param.getEffectiveId(); 113 113 try { 114 Object oldValue = get(param, param.getStorageType()); 115 Object casted = param.reassign(value, oldValue); 116 if (casted != oldValue) { 117 internalSet(param, casted); 118 } 119 } catch (CastFailure e) { 120 LOGGER.error("casting failure while set: ", e); 121 122 } 123 return flags; 124 } 125 126 127 114 Object oldValue = get(param, param.getStorageType()); 115 ReassignResult<?> result = param.reassign(value, oldValue); 116 Object casted = result.getValue(); 117 if (casted != oldValue) { 118 internalSet(param, casted); 119 } 120 flags = result.getFlags(); 121 } catch (CastFailure e) { 122 log.error("casting failure while set: ", e); 123 } 124 return flags; 125 } 128 126 129 127 @Override 130 128 public void setDefault(boolean numericOnly) { 131 for (int i = 0; i < framsClass.getParamCount(); i++) 129 for (int i = 0; i < framsClass.getParamCount(); i++) { 132 130 setDefault(i, numericOnly); 131 } 133 132 } 134 133 135 134 @Override 136 135 public void setDefault(int i, boolean numericOnly) { 137 Param entry = framsClass.getParamEntry(i);136 ValueParam entry = framsClass.getParamEntry(i, ValueParam.class); 138 137 if ((entry != null) && (!numericOnly || entry.isNumeric())) { 139 138 set(i, entry.getDef(entry.getStorageType())); … … 148 147 } 149 148 150 @SuppressWarnings("unchecked")151 149 @Override 152 150 public void setMin(int i) { 153 Param entry = framsClass.getParamEntry(i); 151 PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class); 152 if (entry == null) { 153 return; 154 } 154 155 Object min = entry.getMin(entry.getStorageType()); 155 156 if (min != null) { … … 165 166 } 166 167 167 @SuppressWarnings("unchecked")168 168 @Override 169 169 public void setMax(int i) { 170 Param entry = framsClass.getParamEntry(i); 170 PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class); 171 if (entry == null) { 172 return; 173 } 171 174 Object max = entry.getMax(entry.getStorageType()); 172 175 if (max != null) { … … 191 194 @Override 192 195 public void save(SinkInterface sink) { 193 assert framsClass != null; 194 sink.print(framsClass.getId()).print(":").breakLine(); 195 for (Param p : framsClass.getParamEntries()) { 196 if (p instanceof ValueParam) { 197 Object value = get(p, Object.class); 198 if (value == null) { 199 continue; 200 } 201 sink.print(p.getId()).print(":"); 202 ((ValueParam)p).save(sink, value); 203 sink.breakLine(); 204 205 206 //continue; 207 } 208 /* 209 if (p instanceof CompositeParam) { 210 if (!(p instanceof ListParam)) { 211 continue; 212 } 213 Collection c = get(p, Collection.class); 214 if (c != null) { 215 save(stream, p, c.size()); 216 } 217 } 218 */ 219 220 } 221 sink.breakLine(); 222 } 223 224 @Override 225 public String save2(boolean omitDefaultValues) { 226 StringBuilder stringBuilder = new StringBuilder(); 227 DecimalFormat df = new DecimalFormat("#.###", new DecimalFormatSymbols( 228 Locale.ENGLISH)); 229 boolean canOmitName = false; 230 int n = 0; 231 for (int i = 0; i < framsClass.getParamCount(); i++) { 232 Object value = this.get(i, Object.class); 233 Object def = framsClass.getParamEntry(i).getDef(Object.class); 234 if (value != null && (!omitDefaultValues || !value.equals(def))) { 235 if ((n != i && !canOmitName) 236 || (getParam(i).getFlags() & Flags.CANOMITNAME) == 0) { 237 stringBuilder.append(getParam(i).getId()); 238 stringBuilder.append("="); 196 assert framsClass != null; 197 sink.print(framsClass.getId()).print(":").breakLine(); 198 for (PrimitiveParam p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) { 199 Object value = get(p, Object.class); 200 if (value == null) { 201 continue; 202 } 203 sink.print(p.getId()).print(":"); 204 p.save(sink, value); 205 sink.breakLine(); 206 } 207 sink.breakLine(); 208 } 209 210 private Entry readEntry(SourceInterface source) 211 throws IOException { 212 213 String line; 214 String key = null; 215 StringBuilder value = null; 216 while ((line = source.readLine()) != null) 217 { 218 if (key == null) { 219 int colonIndex = line.indexOf(':'); 220 if (colonIndex == -1) { 221 return null; 239 222 } 240 n++; 241 canOmitName = true; 242 if (this.get(i, Object.class) instanceof String) { 243 String valueString = this.get(i, String.class); 244 valueString = valueString.replaceAll("\\\"", "\\\\\""); 245 valueString = valueString.contains(",") ? "\"" 246 + valueString + "\"" : valueString; 247 // if ("".equals(valueString.trim())) 248 // value = "\"\""; 249 stringBuilder.append(valueString); 250 } else if (this.get(i, Object.class) instanceof Double) { 251 stringBuilder.append(df.format(this.get(i, Double.class))); 252 } else 253 stringBuilder.append(this.get(i, Object.class)); 254 stringBuilder.append(", "); 223 key = line.substring(0, colonIndex); 224 String inlineValue = line.substring(colonIndex + 1); 225 226 227 if (!inlineValue.startsWith("~")) { 228 return new Entry(key, inlineValue); 229 } 230 value = new StringBuilder(); 231 value.append(inlineValue.substring(1)); 232 continue; 233 } 234 if (value.length() != 0) { 235 value.append(System.getProperty("line.separator")); 236 } 237 if (line.contains("~")) { 238 value.append(line.substring(0, line.indexOf("~"))); 239 return new Entry(key, value.toString()); 240 } 241 value.append(line); 242 /* 243 if (line.contains("~")) { 244 String lastLine = line.substring(0, line.indexOf("~")); 245 if (lastLine.length() > 0) { 246 appendToValue(value, lastLine); 247 } 248 return new Entry(key, value.toString()); 249 } 250 appendToValue(value, line); 251 */ 252 } 253 return null; 254 } 255 256 @Override 257 public void load(SourceInterface source) throws Exception { 258 //TODO not clearing values, because get from manager gives only fields, not children 259 //this.clearValues(); 260 261 Entry entry; 262 while ((entry = readEntry(source)) != null) { 263 Param param = getParam(entry.key); 264 if (param == null) { 265 continue; 266 } 267 if (!(param instanceof ValueParam)) { 268 log.warn("param " + param + " is not a ValueParam"); 269 continue; 270 } 271 if ((param.getFlags() & Flags.DONTLOAD) != 0) { 272 log.debug("DontLoad flag was set - not loading..."); 255 273 } else { 256 canOmitName = false; 257 } 258 } 259 260 String params = ""; 261 if (stringBuilder.length() > 1) 262 params = stringBuilder.substring(0, stringBuilder.length() - 2); 263 264 return getId() + ":" + params; 265 } 266 267 /* 268 private static void appendToValue(StringBuilder value, String line) { 269 270 if (value.length() != 0) { 271 value.append(System.getProperty("line.separator")); 272 } 273 value.append(line); 274 } 275 */ 276 277 private Entry readEntry(SourceInterface source) 278 throws IOException { 279 280 String line; 281 String key = null; 282 StringBuilder value = null; 283 while ((line = source.readLine()) != null) 284 { 285 if (key == null) { 286 int colonIndex = line.indexOf(':'); 287 if (colonIndex == -1) { 288 return null; 289 } 290 key = line.substring(0, colonIndex); 291 String inlineValue = line.substring(colonIndex + 1); 292 293 294 if (!inlineValue.startsWith("~")) { 295 return new Entry(key, inlineValue); 296 } 297 value = new StringBuilder(); 298 value.append(inlineValue.substring(1)); 299 continue; 300 } 301 if (value.length() != 0) { 302 value.append(System.getProperty("line.separator")); 303 } 304 if (line.contains("~")) { 305 value.append(line.substring(0, line.indexOf("~"))); 306 return new Entry(key, value.toString()); 307 } 308 value.append(line); 309 /* 310 if (line.contains("~")) { 311 String lastLine = line.substring(0, line.indexOf("~")); 312 if (lastLine.length() > 0) { 313 appendToValue(value, lastLine); 314 } 315 return new Entry(key, value.toString()); 316 } 317 appendToValue(value, line); 318 */ 319 } 320 return null; 321 } 322 323 @Override 324 public void load(SourceInterface source) throws Exception { 325 //TODO not clearing values, because get from manager gives only fields, not children 326 //this.clearValues(); 327 328 Entry entry; 329 while ((entry = readEntry(source)) != null) { 330 Param param = getParam(entry.key); 331 if (param == null) { 332 continue; 333 } 334 if ((param.getFlags() & Flags.DONTLOAD) != 0) { 335 LOGGER.debug("DontLoad flag was set - not loading..."); 336 } else { 337 int retFlags = this.set(param, entry.value); 338 if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) { 339 String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" 340 : "big"; 341 LOGGER.warn("value of key '" + entry.key 342 + "' was too " + which + ", adjusted"); 343 } 344 } 345 } 346 } 347 348 349 350 protected abstract <T> void internalSet(Param param, T value); 351 352 @Override 353 public Collection<Param> getParams() { 354 return framsClass.getParamEntries(); 355 } 356 357 /* 274 int retFlags = this.set((ValueParam) param, entry.value); 275 if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) { 276 String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" : "big"; 277 log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted"); 278 } 279 } 280 } 281 } 282 283 protected abstract <T> void internalSet(ValueParam param, T value); 284 285 @Override 286 public Collection<Param> getParams() { 287 return framsClass.getParamEntries(); 288 } 289 290 /* 358 291 protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) { 359 292 int flags = 0; -
java/main/src/main/java/com/framsticks/params/UniqueListAccess.java
r77 r84 1 1 package com.framsticks.params; 2 2 3 import com.framsticks.util. Casting;4 import com.framsticks.util. Numbers;3 import com.framsticks.util.lang.Casting; 4 import com.framsticks.util.lang.Numbers; 5 5 import org.apache.log4j.Logger; 6 6 … … 12 12 public class UniqueListAccess extends ListAccess { 13 13 14 private static final Logger LOGGER= Logger.getLogger(UniqueListAccess.class.getName());14 private static final Logger log = Logger.getLogger(UniqueListAccess.class.getName()); 15 15 16 16 Map<String, Object> map; 17 17 18 18 final String uidName; 19 19 20 21 22 23 20 public UniqueListAccess(AccessInterface elementAccess, String uidName) { 21 super(elementAccess); 22 this.uidName = uidName; 23 } 24 24 25 26 27 28 25 @Override 26 public Map<String, Object> createAccessee() { 27 return new HashMap<String, Object>(); 28 } 29 29 30 31 32 LOGGER.error("accesing unique list through index");33 34 35 //LOGGER.error("accesing unique list through index");36 37 38 30 @Override 31 public Param getParam(int i) { 32 log.error("accesing unique list through index"); 33 assert false; 34 return null; 35 //log.error("accesing unique list through index"); 36 //return null; 37 //return new ParamBuilder().setId(Integer.toString(i)).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build(); 38 } 39 39 40 41 42 43 44 45 46 47 40 @Override 41 public Param getParam(String id) { 42 Integer i = Numbers.parse(id, Integer.class); 43 if (i != null) { 44 return getParam(i); 45 } 46 return new ParamBuilder().setId(id).setName(elementAccess.getId()).setType("o " + elementAccess.getId()).build(); 47 } 48 48 49 50 51 52 49 @Override 50 public String getId() { 51 return "l " + elementAccess.getId() + " " + uidName; 52 } 53 53 54 55 56 57 54 @Override 55 public int getParamCount() { 56 return map.size(); 57 } 58 58 59 60 61 LOGGER.error("accesing unique list through index");62 63 64 65 66 59 @Override 60 public <T> T get(int i, Class<T> type) { 61 log.error("accesing unique list through index"); 62 assert false; 63 return null; 64 //Object[] values = map.values().toArray(); 65 //return Casting.tryCast(i < values.length ? values[i] : null, type); 66 } 67 67 68 69 70 71 72 73 74 75 68 @Override 69 public <T> T get(String id, Class<T> type) { 70 Integer i = Numbers.parse(id, Integer.class); 71 if (i != null) { 72 return get(i, type); 73 } 74 return Casting.tryCast(type, map.containsKey(id) ? map.get(id) : null); 75 } 76 76 77 78 public <T> T get(Param param, Class<T> type) {79 80 77 @Override 78 public <T> T get(ValueParam param, Class<T> type) { 79 return get(param.getId(), type); 80 } 81 81 82 83 84 85 86 87 88 89 90 91 82 public String getUidOf(Object value) { 83 Object tmp = elementAccess.getSelected(); 84 elementAccess.select(value); 85 String uid = elementAccess.get(uidName, String.class); 86 elementAccess.select(tmp); 87 if (uid == null) { 88 return null; 89 } 90 return uid; 91 } 92 92 93 94 95 96 97 LOGGER.error("failed to set - missing uid");98 99 100 101 102 103 104 105 106 107 93 protected int setByUid(Object object, String uid) { 94 if (uid == null) { 95 uid = getUidOf(object); 96 if (uid == null) { 97 log.error("failed to set - missing uid"); 98 return 0; 99 } 100 } 101 if (object == null) { 102 map.remove(uid); 103 } else { 104 map.put(uid, object); 105 } 106 return 0; 107 } 108 108 109 110 111 LOGGER.error("accesing unique list through index");112 113 114 109 @Override 110 public <T> int set(int i, T value) { 111 log.error("accesing unique list through index"); 112 //return setByUid(value, null); 113 return 0; 114 } 115 115 116 117 118 119 120 121 122 123 124 125 126 127 128 LOGGER.error("uid mismatch with set key");129 130 131 132 133 134 135 136 137 138 139 140 141 142 LOGGER.error("missing both uid and id - failed to set");143 144 116 @Override 117 public <T> int set(String id, T value) { 118 Integer i = Numbers.parse(id, Integer.class); 119 if (i != null) { 120 return set(i, value); 121 } 122 if (value == null) { 123 return setByUid(null, id); 124 } 125 String uid = getUidOf(value); 126 if (uid != null && id != null) { 127 if (!id.equals(uid)) { 128 log.error("uid mismatch with set key"); 129 return 0; 130 } 131 setByUid(value, uid); 132 return 0; 133 } 134 if (uid != null) { 135 setByUid(value, uid); 136 return 0; 137 } 138 if (id != null) { 139 setByUid(value, id); 140 return 0; 141 } 142 log.error("missing both uid and id - failed to set"); 143 return 0; 144 } 145 145 146 147 public <T> int set(Param param, T value) {148 149 146 @Override 147 public <T> int set(ValueParam param, T value) { 148 return set(param.getId(), value); 149 } 150 150 151 152 153 154 151 @Override 152 public void clearValues() { 153 map.clear(); 154 } 155 155 156 @Override 157 public void select(Object object) { 158 map = (Map<String, Object>)object; 159 } 156 @SuppressWarnings("unchecked") 157 @Override 158 public UniqueListAccess select(Object object) { 159 assert (object instanceof Map); 160 map = (Map<String, Object>) object; 161 return this; 162 } 160 163 161 162 163 164 164 @Override 165 public Object getSelected() { 166 return map; 167 } 165 168 166 167 168 169 169 @Override 170 public UniqueListAccess cloneAccess() { 171 return new UniqueListAccess(elementAccess.cloneAccess(), uidName); 172 } 170 173 171 172 173 174 175 LOGGER.error("missing uid field");176 177 178 179 174 @Override 175 public String computeIdentifierFor(Object selected) { 176 String uid = getUidOf(selected); 177 if (uid == null) { 178 log.error("missing uid field"); 179 return null; 180 } 181 return uid; 182 } 180 183 181 182 183 184 185 186 187 188 189 190 191 184 @Override 185 public Collection<Param> getParams() { 186 List<Param> result = new LinkedList<Param>(); 187 if (map == null) { 188 return result; 189 } 190 for (String k : map.keySet()) { 191 result.add(getParam(k)); 192 } 193 return result; 194 } 192 195 } -
java/main/src/main/java/com/framsticks/params/Util.java
r78 r84 4 4 import java.util.List; 5 5 6 import static com.framsticks.util.lang.Containers.filterInstanceof; 7 6 8 /** 7 9 * @author Piotr Sniegowski 8 10 */ 9 11 public abstract class Util { 10 public static String readSourceToString(SourceInterface source) { 11 StringBuilder result = new StringBuilder(); 12 String line; 13 while ((line = source.readLine()) != null) { 14 result.append(line).append(" "); 15 } 16 source.close(); 17 return result.toString(); 18 } 12 13 public static String readSourceToString(SourceInterface source) { 14 StringBuilder result = new StringBuilder(); 15 String line; 16 while ((line = source.readLine()) != null) { 17 result.append(line).append(" "); 18 } 19 source.close(); 20 return result.toString(); 21 } 22 19 23 public static List<Object> stripAccessInterface(List<AccessInterface> accesses) { 20 24 List<Object> result = new ArrayList<Object>(); … … 24 28 return result; 25 29 } 30 26 31 public static int copyParams(AccessInterface to, AccessInterface from) { 27 32 int copied = 0; 28 for ( Param f : from.getParams()) {33 for (ValueParam f : filterInstanceof(from.getParams(), ValueParam.class)) { 29 34 Param t = from.getParam(f.getId()); 30 if ( t == null) {35 if (!(t instanceof ValueParam)) { 31 36 continue; 32 37 } … … 34 39 continue; 35 40 } 36 to.set( t, from.get(f, Object.class));41 to.set((ValueParam) t, from.get(f, Object.class)); 37 42 ++copied; 38 43 } -
java/main/src/main/java/com/framsticks/params/types/ArrayListParam.java
r77 r84 4 4 import com.framsticks.params.ArrayListAccess; 5 5 import com.framsticks.params.CastFailure; 6 import com.framsticks.util.Casting; 7 import com.framsticks.util.Containers; 8 import com.framsticks.util.Numbers; 6 import com.framsticks.params.ReassignResult; 7 import com.framsticks.util.lang.Casting; 8 import com.framsticks.util.lang.Containers; 9 import com.framsticks.util.lang.Numbers; 9 10 10 11 import java.util.ArrayList; … … 16 17 public class ArrayListParam extends ListParam { 17 18 18 19 20 19 public ArrayListParam(String containedTypeName) { 20 super(containedTypeName); 21 } 21 22 22 23 23 24 public ClassgetStorageType() {25 26 24 @Override 25 public Class<?> getStorageType() { 26 return List.class; 27 } 27 28 28 29 30 31 29 @Override 30 public AccessInterface prepareAccessInterface(AccessInterface access) { 31 return new ArrayListAccess(access); 32 } 32 33 33 34 35 36 34 @Override 35 public String computeAccessId() { 36 return "l " + containedTypeName; 37 } 37 38 38 39 public Objectreassign(Object newValue, Object oldValue) throws CastFailure {40 41 42 43 44 45 46 Listlist;47 48 list = new ArrayList();49 50 51 52 53 54 55 56 return list;57 58 59 return oldValue;60 61 return newValue;62 39 @Override 40 public ReassignResult<List<?>> reassign(Object newValue, Object oldValue) throws CastFailure { 41 if (newValue == null) { 42 throw new CastFailure(); 43 } 44 Integer size = Numbers.cast(newValue, Integer.class); 45 if (size != null) { 46 //return oldValue; 47 List<?> list; 48 if (oldValue == null) { 49 list = new ArrayList<Object>(); 50 } else { 51 list = Casting.tryCast(List.class, oldValue); 52 if (list == null) { 53 throw new CastFailure(); 54 } 55 } 56 Containers.resizeList(list, size); 57 return new ReassignResult<List<?>>(list); 58 } 59 if (oldValue != null) { 60 return new ReassignResult<List<?>>(Casting.throwCast(List.class, oldValue)); 61 } 62 return new ReassignResult<List<?>>(Casting.throwCast(List.class, newValue)); 63 } 63 64 64 65 public String getType() {66 67 65 @Override 66 public String getFramsTypeName() { 67 return "l " + getContainedTypeName(); 68 } 68 69 } -
java/main/src/main/java/com/framsticks/params/types/BinaryParam.java
r77 r84 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.util.Numbers; 4 import com.framsticks.params.PrimitiveParam; 5 import com.framsticks.params.ReassignResult; 6 import com.framsticks.util.lang.Numbers; 5 7 6 8 /** 7 9 * @author Piotr Sniegowski 8 10 */ 9 public class BinaryParam extends ValueParam { 11 public class BinaryParam extends PrimitiveParam { 12 10 13 @Override 11 public Class getStorageType() {14 public Class<?> getStorageType() { 12 15 return Integer.class; 13 16 } 14 17 15 16 public String getType() {17 18 18 @Override 19 public String getFramsTypeName() { 20 return "db"; 21 } 19 22 20 @Override 21 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 22 if (newValue instanceof String) { 23 Integer v = Numbers.parse((String) newValue, Integer.class); 24 if (v != null) { 25 return v; 26 } 27 throw new CastFailure(); 28 } 29 return super.reassign(newValue, oldValue); 30 } 23 @Override 24 public ReassignResult<?> reassign(Object newValue, Object oldValue) throws CastFailure { 25 if (newValue instanceof String) { 26 Integer v = Numbers.parse((String) newValue, Integer.class); 27 if (v != null) { 28 return ReassignResult.create(v); 29 } 30 throw new CastFailure(); 31 } 32 if (newValue instanceof Integer) { 33 return ReassignResult.create((Integer) newValue); 34 } 35 throw new CastFailure(); 36 } 31 37 32 38 } -
java/main/src/main/java/com/framsticks/params/types/BooleanParam.java
r77 r84 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.PrimitiveParam; 5 import com.framsticks.params.ReassignResult; 4 6 import com.framsticks.params.SinkInterface; 7 import com.framsticks.util.lang.Numbers; 5 8 6 9 /** 7 10 * @author Piotr Sniegowski 8 11 */ 9 public class BooleanParam extends DecimalParam { 10 @Override 11 public Class getStorageType() { 12 return Boolean.class; 13 } 12 public class BooleanParam extends PrimitiveParam { 14 13 15 @Override 16 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 14 public BooleanParam() { 15 def = false; 16 } 17 18 @Override 19 public Class<?> getStorageType() { 20 return Boolean.class; 21 } 22 23 @Override 24 public ReassignResult<Boolean> reassign(Object newValue, Object oldValue) throws CastFailure { 17 25 if (newValue instanceof Boolean) { 18 return newValue;26 return ReassignResult.create((Boolean) newValue); 19 27 } 20 Object casted = super.reassign(newValue, oldValue); 21 if (casted instanceof Integer) { 22 Integer i = (Integer)casted; 23 if (i == 0) return false; 24 if (i == 1) return true; 25 throw new CastFailure(); 26 } 27 /* 28 if (newValue instanceof String) { 29 if (newValue.equals("true")) return true; 30 if (newValue.equals("false")) return false; 31 throw new CastFailure(); 32 } 33 */ 34 throw new CastFailure(); 35 } 28 if (newValue instanceof Integer) { 29 return ReassignResult.create(((Integer) newValue) != 0); 30 } 31 if (newValue instanceof String) { 32 if ("true".equals(newValue)) { 33 return ReassignResult.create(true); 34 } 35 if ("false".equals(newValue)) { 36 return ReassignResult.create(false); 37 } 38 Integer i = Numbers.cast(newValue, Integer.class); 39 if (i != null) { 40 return ReassignResult.create(i != 0); 41 } 42 throw new CastFailure(); 43 } 44 throw new CastFailure(); 45 } 36 46 37 38 public String getType() {39 40 47 @Override 48 public String getFramsTypeName() { 49 return "d 0 1"; 50 } 41 51 42 43 44 45 46 52 @Override 53 public void save(SinkInterface sink, Object value) { 54 assert value instanceof Boolean; 55 sink.print(((Boolean)value) ? "1" : "0"); 56 } 47 57 } -
java/main/src/main/java/com/framsticks/params/types/ColorParam.java
r77 r84 1 1 package com.framsticks.params.types; 2 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.PrimitiveParam; 5 import com.framsticks.params.ReassignResult; 2 6 3 7 /** 4 8 * @author Piotr Sniegowski 5 9 */ 6 public class ColorParam extends ValueParam {10 public class ColorParam extends PrimitiveParam { 7 11 8 12 @Override 9 public Class getStorageType() {10 return Void.class;13 public Class<?> getStorageType() { 14 return Object.class; 11 15 } 12 16 13 14 public String getType() {15 16 17 @Override 18 public String getFramsTypeName() { 19 return "dc"; 20 } 17 21 22 public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure { 23 return ReassignResult.create(newValue); 24 } 18 25 19 26 -
java/main/src/main/java/com/framsticks/params/types/DecimalParam.java
r77 r84 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks. util.Numbers;4 import com.framsticks.params.ReassignResult; 5 5 6 6 /** 7 7 * @author Piotr Sniegowski 8 8 */ 9 public class DecimalParam extends ValueParam{9 public class DecimalParam extends NumberParam<Integer> { 10 10 11 @Override 12 public boolean isNumeric() { 13 return true; 11 public DecimalParam() { 12 def = 0; 14 13 } 15 14 16 15 @Override 17 public Class getStorageType() {16 public Class<?> getStorageType() { 18 17 return Integer.class; 19 18 } 20 19 20 @Override 21 public ReassignResult<Integer> reassign(Object newValue, Object oldValue) throws CastFailure { 22 return reassignNumber(newValue, oldValue, Integer.class); 23 } 21 24 22 23 @Override 24 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 25 if (newValue instanceof String) { 26 Integer v = Numbers.parse((String)newValue, Integer.class); 27 if (v != null) { 28 return v; 29 } 30 throw new CastFailure(); 31 } 32 return super.reassign(newValue, oldValue); 33 } 34 35 @Override 36 public String getType() { 37 return "d"; 38 } 25 @Override 26 public String getFramsTypeName() { 27 return "d"; 28 } 39 29 40 30 -
java/main/src/main/java/com/framsticks/params/types/EnumParam.java
r77 r84 1 1 package com.framsticks.params.types; 2 2 3 import java.util.ArrayList;4 3 import java.util.List; 5 4 … … 9 8 public class EnumParam extends DecimalParam { 10 9 11 ArrayList<String> enums;10 List<String> enums; 12 11 13 12 /** … … 16 15 * @param enums 17 16 */ 18 public EnumParam( ArrayList<String> enums) {17 public EnumParam(List<String> enums) { 19 18 assert(enums != null); 20 19 this.enums = enums; -
java/main/src/main/java/com/framsticks/params/types/EventParam.java
r77 r84 7 7 */ 8 8 public class EventParam extends Param { 9 9 10 @Override 10 public Class getStorageType() {11 public Class<?> getStorageType() { 11 12 return Void.class; 12 13 } 13 14 14 @Override 15 public String getType() { 16 return "e"; 17 } 18 19 20 15 @Override 16 public String getFramsTypeName() { 17 return "e"; 18 } 21 19 } -
java/main/src/main/java/com/framsticks/params/types/FloatParam.java
r77 r84 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks. util.Numbers;4 import com.framsticks.params.ReassignResult; 5 5 6 6 /** 7 7 * @author Piotr Sniegowski 8 8 */ 9 public class FloatParam extends ValueParam { 9 public class FloatParam extends NumberParam<Double> { 10 11 public FloatParam() { 12 def = 0.0; 13 } 14 10 15 @Override 11 public boolean isNumeric() { 12 return true; 13 } 14 @Override 15 public Class getStorageType() { 16 public Class<?> getStorageType() { 16 17 return Double.class; 17 18 } 18 19 19 20 @Override 20 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 21 if (newValue instanceof String) { 22 Double v = Numbers.parse((String)newValue, Double.class); 23 if (v != null) { 24 return v; 25 } 26 throw new CastFailure(); 27 } 28 return super.reassign(newValue, oldValue); 21 public ReassignResult<Double> reassign(Object newValue, Object oldValue) throws CastFailure { 22 return reassignNumber(newValue, oldValue, Double.class); 29 23 } 30 24 31 32 public String getType() {33 34 25 @Override 26 public String getFramsTypeName() { 27 return "f"; 28 } 35 29 36 30 -
java/main/src/main/java/com/framsticks/params/types/ListParam.java
r77 r84 1 1 package com.framsticks.params.types; 2 2 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.util.Casting; 5 import com.framsticks.util.Containers; 6 import com.framsticks.util.Numbers; 7 8 import java.util.List; 3 import com.framsticks.params.CompositeParam; 9 4 10 5 /** … … 17 12 } 18 13 19 20 21 22 23 24 14 } -
java/main/src/main/java/com/framsticks/params/types/ObjectParam.java
r77 r84 3 3 import com.framsticks.params.AccessInterface; 4 4 import com.framsticks.params.CastFailure; 5 import com.framsticks.params.CompositeParam; 6 import com.framsticks.params.ReassignResult; 5 7 6 8 /** … … 13 15 } 14 16 15 16 17 18 17 @Override 18 public String computeAccessId() { 19 return containedTypeName; 20 } 19 21 20 21 public Class getStorageType() {22 @Override 23 public Class<?> getStorageType() { 22 24 return Object.class; 23 25 } 24 26 25 27 @Override 26 public boolean isEmptyAvailable(){27 return true;28 public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure { 29 return ReassignResult.create(newValue); 28 30 } 29 31 30 32 @Override 31 public Object reassign(Object newValue, Object oldValue) throws CastFailure{32 return newValue;33 public AccessInterface prepareAccessInterface(AccessInterface access) { 34 return access; 33 35 } 34 36 35 @Override 36 public AccessInterface prepareAccessInterface(AccessInterface access) { 37 return access; 38 } 39 40 @Override 41 public String getType() { 42 return "o " + containedTypeName; 43 } 44 45 46 37 @Override 38 public String getFramsTypeName() { 39 return "o " + containedTypeName; 40 } 47 41 48 42 } -
java/main/src/main/java/com/framsticks/params/types/ProcedureParam.java
r77 r84 1 1 package com.framsticks.params.types; 2 2 3 import com.framsticks.params.CastFailure;4 import com.framsticks.params.FramsClass;5 3 import com.framsticks.params.Param; 6 4 import com.framsticks.params.ParamBuilder; 7 import com.framsticks.util. Strings;5 import com.framsticks.util.lang.Strings; 8 6 9 7 import java.util.ArrayList; … … 58 56 59 57 @Override 60 public Class getStorageType() {58 public Class<?> getStorageType() { 61 59 return Void.class; 62 60 } … … 70 68 } 71 69 72 @Override 73 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 74 throw new CastFailure(); 75 } 76 77 @Override 78 public String getType() { 79 return "p"; 80 } 70 @Override 71 public String getFramsTypeName() { 72 return "p"; 73 } 81 74 82 75 } -
java/main/src/main/java/com/framsticks/params/types/StringParam.java
r77 r84 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.params.PrimitiveParam; 5 import com.framsticks.params.ReassignResult; 4 6 import com.framsticks.params.SinkInterface; 5 7 … … 7 9 * @author Piotr Sniegowski 8 10 */ 9 public class StringParam extends ValueParam {11 public class StringParam extends PrimitiveParam { 10 12 @Override 11 public Class getStorageType() {13 public Class<?> getStorageType() { 12 14 return String.class; 13 15 } 14 16 15 17 @Override 16 public boolean isEmptyAvailable() { 17 return true; 18 public ReassignResult<String> reassign(Object newValue, Object oldValue) throws CastFailure { 19 if (newValue == null) { 20 return new ReassignResult<String>(null); 21 } 22 if (newValue instanceof String) { 23 return ReassignResult.create((String) newValue); 24 } 25 return ReassignResult.create(newValue.toString()); 26 // return super.reassign(newValue, oldValue); 18 27 } 19 28 20 @Override 21 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 22 if (newValue instanceof String) { 23 return (String)newValue; 24 } 25 return super.reassign(newValue, oldValue); 26 } 29 @Override 30 public String getFramsTypeName() { 31 return "s"; 32 } 27 33 28 @Override 29 public String getType() { 30 return "s"; 31 } 34 private static final String SEPARATOR = System.getProperty("line.separator"); 32 35 33 private static final String SEPARATOR = System.getProperty("line.separator"); 34 35 @Override 36 public void save(SinkInterface sink, Object value) { 37 assert value instanceof String; 38 String s = (String)value; 39 sink.print((s.contains(SEPARATOR) ? (s = "~" + SEPARATOR + s + "~") : s)); 40 } 36 @Override 37 public void save(SinkInterface sink, Object value) { 38 assert value instanceof String; 39 String s = (String)value; 40 sink.print((s.contains(SEPARATOR) ? (s = "~" + SEPARATOR + s + "~") : s)); 41 } 41 42 } -
java/main/src/main/java/com/framsticks/params/types/UniqueListParam.java
r77 r84 3 3 import com.framsticks.params.AccessInterface; 4 4 import com.framsticks.params.CastFailure; 5 import com.framsticks.params.ReassignResult; 5 6 import com.framsticks.params.UniqueListAccess; 6 import com.framsticks.util. Numbers;7 import com.framsticks.util.lang.Numbers; 7 8 8 9 import java.util.HashMap; 9 import java.util.List;10 10 import java.util.Map; 11 11 … … 15 15 public class UniqueListParam extends ListParam { 16 16 17 17 final String uidName; 18 18 19 20 21 22 19 public UniqueListParam(String containedTypeName, String uidName) { 20 super(containedTypeName); 21 this.uidName = uidName; 22 } 23 23 24 25 26 27 24 @Override 25 public String computeAccessId() { 26 return "l " + containedTypeName + " " + uidName; 27 } 28 28 29 29 30 31 public ClassgetStorageType() {32 33 30 @Override 31 public Class<?> getStorageType() { 32 return Map.class; 33 } 34 34 35 36 37 38 35 @Override 36 public AccessInterface prepareAccessInterface(AccessInterface access) { 37 return new UniqueListAccess(access, uidName); 38 } 39 39 40 @Override 41 public ReassignResult<? extends Map<?,?>> reassign(Object newValue, Object oldValue) throws CastFailure { 42 if (newValue instanceof Map) { 43 return new ReassignResult<Map<?,?>>((Map<?,?>) newValue); 44 } 45 Integer size = Numbers.cast(newValue, Integer.class); 46 if (size != null) { 47 //return oldValue; 48 /* 49 the integer value should be ignored, because this may cause, that object is created before 50 information about it's elements is available, which would break resolution flow 51 */ 52 if (oldValue != null) { 53 return new ReassignResult<Map<?,?>>((Map<?,?>) oldValue); 54 } 55 return ReassignResult.create(new HashMap<Object, Object>()); 56 } 40 57 41 @Override 42 public Object reassign(Object newValue, Object oldValue) throws CastFailure { 43 if (newValue instanceof Map) { 44 return newValue; 45 } 46 Integer size = Numbers.cast(newValue, Integer.class); 47 if (size != null) { 48 //return oldValue; 49 /* 50 the integer value should be ignored, because this may cause, that object is created before 51 information about it's elements is available, which would break resolution flow 52 */ 53 if (oldValue != null) { 54 return oldValue; 55 } 56 return new HashMap(); 57 } 58 throw new CastFailure(); 59 } 58 60 59 throw new CastFailure(); 60 } 61 62 @Override 63 public String getType() { 64 return "l " + containedTypeName + " " + uidName; 65 } 61 @Override 62 public String getFramsTypeName() { 63 return "l " + containedTypeName + " " + uidName; 64 } 66 65 67 66 -
java/main/src/main/java/com/framsticks/params/types/UniversalParam.java
r77 r84 2 2 3 3 import com.framsticks.params.CastFailure; 4 import com.framsticks.util.Numbers; 4 import com.framsticks.params.PrimitiveParam; 5 import com.framsticks.params.ReassignResult; 5 6 6 7 /** 7 8 * @author Piotr Sniegowski 8 9 */ 9 public class UniversalParam extends ValueParam {10 public class UniversalParam extends PrimitiveParam { 10 11 @Override 11 public Class getStorageType() {12 public Class<?> getStorageType() { 12 13 return Object.class; 13 14 } 14 15 15 16 @Override 16 public boolean isEmptyAvailable() {17 return true;17 public String getFramsTypeName() { 18 return "x"; 18 19 } 19 20 20 @Override 21 public String getType() { 22 return "x"; 23 } 24 21 @Override 22 public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure { 23 return ReassignResult.create(newValue); 24 } 25 25 26 26 }
Note: See TracChangeset
for help on using the changeset viewer.