Changeset 107 for java/main/src/main/java/com/framsticks/params
- Timestamp:
- 09/23/13 18:54:07 (11 years ago)
- Location:
- java/main/src/main/java/com/framsticks/params
- Files:
-
- 4 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/AccessOperations.java
r105 r107 99 99 public static Access loadAll(@Nullable final Access rootAccess, Source source, final Registry registry) { 100 100 final MultiParamLoader loader = new MultiParamLoader(); 101 log.trace("loading all from {} into {}", source, rootAccess); 101 102 loader.setNewSource(source); 102 103 final LinkedList<Access> accessesStack = new LinkedList<>(); … … 146 147 147 148 if (p instanceof ListParam) { 148 ListAccess listAccess = Casting.assertCast(ListAccess.class, registry.prepareAccess(p)); 149 listAccess.select(a.get(p, Object.class)); 149 ListAccess listAccess = Casting.assertCast(ListAccess.class, registry.prepareAccess(p, true)); 150 Object list = a.get(p, Object.class); 151 if (list == null) { 152 list = listAccess.createAccessee(); 153 a.set(p, list); 154 } 155 listAccess.select(list); 150 156 parent.set(new Pair<Access, CompositeParam>(listAccess, listAccess.prepareParamFor(Integer.toString(listAccess.getParamCount())))); 151 157 … … 157 163 158 164 if (parent.get() == null) { 165 log.trace("{} cannot be placed in {}", name, a); 159 166 accessIterator.remove(); 160 167 } … … 162 169 163 170 if (parent.get() == null) { 164 throw new FramsticksException().msg("failed to find place for loaded object").arg("name", name); 165 } 166 167 currentAccess.set(registry.prepareAccess(parent.get().second ));171 throw new FramsticksException().msg("failed to find place for loaded object").arg("name", name); //.arg("in", accessesStack); 172 } 173 174 currentAccess.set(registry.prepareAccess(parent.get().second, true)); 168 175 Object object = parent.get().first.get(parent.get().second, Object.class); 169 176 if (object != null) { … … 200 207 } 201 208 202 public static void saveAll(Access access, Sinksink, Registry registry) {209 public static <S extends Sink> S saveAll(Access access, S sink, Registry registry) { 203 210 if (access instanceof ObjectAccess) { 204 211 savePrimitives(access, sink); … … 209 216 continue; 210 217 } 211 saveAll(registry.prepareAccess(p).select(child), sink, registry); 212 } 218 saveAll(registry.prepareAccess(p, true).select(child), sink, registry); 219 } 220 return sink; 213 221 } 214 222 … … 219 227 continue; 220 228 } 221 savePrimitives(registry.prepareAccess(p ).select(child), sink);229 savePrimitives(registry.prepareAccess(p, true).select(child), sink); 222 230 } 223 231 } … … 297 305 } 298 306 299 return registry.prepareAccess(result ).select(objectAccess.get(result, Object.class));307 return registry.prepareAccess(result, true).select(objectAccess.get(result, Object.class)); 300 308 } 301 309 }); … … 323 331 } 324 332 if ((param.getFlags() & ParamFlags.DONTLOAD) == 0) { 325 int retFlags = access.set((ValueParam) param, entry.value); 333 ValueParam valueParam = (ValueParam) param; 334 // Object object = valueParam.deserialize(entry.value, null, Object.class); 335 int retFlags = access.set(valueParam, entry.value); 326 336 if ((retFlags & (PSET_HITMIN | PSET_HITMAX)) != 0) { 327 337 String which = ((retFlags & PSET_HITMIN) != 0) ? "small" : "big"; … … 440 450 */ 441 451 public static <T, F> T convert(Class<T> toJavaClass, F from, Registry registry) { 452 log.trace("converting from {} to {}", from, toJavaClass); 442 453 if (toJavaClass.equals(from.getClass())) { 443 454 return toJavaClass.cast(from); … … 478 489 479 490 if (avp instanceof CompositeParam) { 480 assureEquality(registry.prepareAccess((CompositeParam) avp ).select(oa), registry.prepareAccess((CompositeParam) bvp).select(ob), registry);491 assureEquality(registry.prepareAccess((CompositeParam) avp, false).select(oa), registry.prepareAccess((CompositeParam) bvp, false).select(ob), registry); 481 492 continue; 482 493 } -
java/main/src/main/java/com/framsticks/params/ArrayListAccess.java
r105 r107 137 137 return new Iterator<Param>() { 138 138 139 protected ListIterator<Object> internal = list.listIterator();139 protected final ListIterator<Object> internal = list.listIterator(); 140 140 141 141 @Override -
java/main/src/main/java/com/framsticks/params/FramsClassBuilder.java
r101 r107 42 42 43 43 public static FramsClass readFromStream(InputStream stream) { 44 return Loaders.loadFramsClass(new FileSource(stream)); 44 return readFromSource(new FileSource(stream)); 45 } 46 47 public static FramsClass readFromSource(Source source) { 48 return Loaders.loadFramsClass(source); 45 49 } 46 50 -
java/main/src/main/java/com/framsticks/params/ParamCandidate.java
r105 r107 80 80 protected final OneTime<Method> remover = new OneTime<>("remover"); 81 81 protected final OneTime<Class<? extends Param>> paramType = new OneTime<>("paramType"); 82 protected final OneTime<String> stringType = new OneTime<>("stringType"); 82 83 83 84 protected int flags = 0; … … 278 279 if (!paramAnnotation.paramType().equals(Param.class)) { 279 280 paramType.set(paramAnnotation.paramType()); 281 } 282 if (!"".equals(paramAnnotation.stringType())) { 283 stringType.set(paramAnnotation.stringType()); 280 284 } 281 285 if (member instanceof Field) { … … 371 375 protected final List<ParamCandidate> order; 372 376 protected final java.util.Set<Class<?>> dependantClasses = new HashSet<>(); 377 protected final java.util.Set<String> dependantClassesFromInfo = new HashSet<>(); 373 378 374 379 /** … … 398 403 return dependantClasses; 399 404 } 405 406 public java.util.Set<String> getDependentClassesFromInfo() { 407 return dependantClassesFromInfo; 408 } 400 409 } 401 410 … … 432 441 result.dependantClasses.add(r); 433 442 } 434 443 for (String i : fa.registerFromInfo()) { 444 result.dependantClassesFromInfo.add(i); 445 } 435 446 436 447 final List<String> order = Arrays.asList(fa.order()); … … 575 586 576 587 public ParamBuilder induceParamType(ParamBuilder builder) { 588 589 if (stringType.has()) { 590 return builder.type(stringType.get()); 591 } 592 577 593 Method method = getCaller(); 578 594 if (method == null) { -
java/main/src/main/java/com/framsticks/params/ParamsUtil.java
r105 r107 2 2 3 3 import java.util.ArrayList; 4 // import java.util.HashMap; 4 5 import java.util.List; 5 6 import java.util.Map; 7 import java.util.TreeMap; 8 import java.util.regex.Matcher; 9 import java.util.regex.Pattern; 6 10 7 11 import javax.annotation.Nonnull; 8 12 13 // import org.apache.logging.log4j.Logger; 14 // import org.apache.logging.log4j.LogManager; 15 9 16 import com.framsticks.util.FramsticksException; 17 import com.framsticks.util.lang.Numbers; 10 18 11 19 … … 16 24 */ 17 25 public abstract class ParamsUtil { 26 // private final static Logger log = LogManager.getLogger(ParamsUtil.class.getName()); 27 18 28 19 29 public static String readSourceToString(Source source) { … … 212 222 return true; 213 223 } 224 if (type.equals(OpaqueObject.class)) { 225 builder.append(value.toString()); 226 return true; 227 } 214 228 throw new FramsticksException().msg("invalid type for serialization").arg("type", type); 215 229 } … … 233 247 } 234 248 249 public static class DeserializationContext { 250 251 public static final Pattern OPAQUE_OBJECT_PATTERN = Pattern.compile("^(\\w+)<0x([a-fA-F0-9]+)>$"); 252 253 @SuppressWarnings("serial") 254 public static class Exception extends FramsticksException { 255 } 256 257 protected final ArrayList<Object> objects = new ArrayList<>(); 258 protected int cursor = 0; 259 protected final String input; 260 261 /** 262 * @param input 263 */ 264 public DeserializationContext(String input) { 265 this.input = input; 266 cursor = 0; 267 } 268 269 protected boolean isFinished() { 270 return cursor == input.length(); 271 } 272 273 protected boolean is(char value) { 274 if (isFinished()) { 275 throw fail().msg("input ended"); 276 } 277 return input.charAt(cursor) == value; 278 } 279 280 protected boolean isOneOf(String values) { 281 if (isFinished()) { 282 throw fail().msg("input ended"); 283 } 284 return values.indexOf(input.charAt(cursor)) != -1; 285 } 286 287 protected char at() { 288 return input.charAt(cursor); 289 } 290 291 protected char at(int pos) { 292 return input.charAt(pos); 293 } 294 295 protected FramsticksException fail() { 296 return new Exception().arg("at", cursor).arg("input", input); 297 } 298 299 protected void force(char value) { 300 if (!is(value)) { 301 throw fail().msg("invalid character").arg("expected", value).arg("found", at()); 302 } 303 next(); 304 } 305 306 protected boolean isAndNext(char value) { 307 if (!is(value)) { 308 return false; 309 } 310 next(); 311 return true; 312 } 313 314 protected void next() { 315 ++cursor; 316 // log.info("at: {}|{}", input.substring(0, cursor), input.substring(cursor)); 317 } 318 319 protected void goToNext(char value) { 320 while (cursor < input.length()) { 321 if (is(value)) { 322 return; 323 } 324 next(); 325 } 326 throw fail().msg("passed end").arg("searching", value); 327 } 328 329 330 protected String forceStringRemaining() { 331 int start = cursor; 332 while (true) { 333 goToNext('"'); 334 if (at(cursor - 1) != '\\') { 335 next(); 336 return input.substring(start, cursor - 1); 337 } 338 next(); 339 // it is finishing that loop, because of throwing in goToNext() 340 } 341 // throw fail(); 342 } 343 344 public Object deserialize() { 345 if (isAndNext('[')) { 346 List<Object> list = new ArrayList<>(); 347 objects.add(list); 348 while (!isAndNext(']')) { 349 if (!list.isEmpty()) { 350 force(','); 351 } 352 Object child = deserialize(); 353 list.add(child); 354 } 355 return list; 356 } 357 358 if (isAndNext('{')) { 359 Map<String, Object> map = new TreeMap<>(); 360 objects.add(map); 361 while (!isAndNext('}')) { 362 if (!map.isEmpty()) { 363 force(','); 364 } 365 force('"'); 366 String key = forceStringRemaining(); 367 force(':'); 368 Object value = deserialize(); 369 map.put(key, value); 370 } 371 return map; 372 } 373 374 if (isAndNext('"')) { 375 return forceStringRemaining(); 376 } 377 int start = cursor; 378 while (!isFinished() && !isOneOf("]},")) { 379 next(); 380 } 381 if (start == cursor) { 382 throw fail().msg("empty value"); 383 } 384 385 String value = input.substring(start, cursor); 386 if (value.equals("null")) { 387 //TODO: add this to list? 388 return null; 389 } 390 391 Matcher matcher = OPAQUE_OBJECT_PATTERN.matcher(value); 392 if (matcher.matches()) { 393 return new OpaqueObject(matcher.group(1), Long.parseLong(matcher.group(2), 16)); 394 } 395 396 397 Object number = DeserializationContext.tryParseNumber(value); 398 if (number != null) { 399 return number; 400 } 401 402 if (value.charAt(0) == '^') { 403 Integer reference = Numbers.parse(value.substring(1), Integer.class); 404 if (reference == null) { 405 throw fail().msg("invalid reference").arg("reference", reference); 406 } 407 return objects.get(reference); 408 } 409 //TODO: parse ^ 410 //TODO: parse opaque object 411 throw fail().msg("unknown entity"); 412 413 } 414 415 public static Object tryParseNumber(String value) { 416 Integer i = Numbers.parse(value, Integer.class); 417 if (i != null) { 418 return i; 419 } 420 421 Double d = Numbers.parse(value, Double.class); 422 if (d != null) { 423 return d; 424 } 425 426 return null; 427 } 428 } 429 430 431 public static Object deserialize(String value) { 432 Object number = DeserializationContext.tryParseNumber(value); 433 if (number != null) { 434 return number; 435 } 436 437 if (!value.startsWith(SERIALIZED)) { 438 return value; 439 } 440 return new DeserializationContext(value.substring(SERIALIZED.length())).deserialize(); 441 } 442 235 443 public static <T> T deserialize(String value, Class<T> type) { 236 return null; 444 Object object = deserialize(value); 445 446 return type.cast(object); 237 447 } 238 448 -
java/main/src/main/java/com/framsticks/params/PrimitiveParam.java
r105 r107 3 3 import com.framsticks.util.FramsticksException; 4 4 import com.framsticks.util.Misc; 5 import com.framsticks.util.lang.Casting;6 5 import com.framsticks.util.lang.Numbers; 7 6 import com.framsticks.util.lang.Strings; … … 51 50 return type.cast(value); 52 51 } 53 // if (Number.class.isAssignableFrom(type)) {54 52 return Numbers.parse((String) value, type); 55 // }56 // return null;53 } if ((value instanceof Integer) && (type.equals(Double.class))) { 54 return type.cast(new Double((Integer) value)); 57 55 } else { 58 56 return type.cast(value); 59 57 } 60 58 } catch (ClassCastException e) { 61 throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type) ;59 throw new FramsticksException().msg("failed to cast").cause(e).arg("param", this).arg("actual", value.getClass()).arg("requested", type).arg("value", value); 62 60 } 63 61 } … … 99 97 } 100 98 101 @Override102 99 public <T> String serialize(T value) { 103 100 return Strings.toStringNullProof(value); 104 101 } 105 102 106 @Override107 public <T> T deserialize(String text, T currentValue, Class<T> type) {108 try {109 return Casting.nullOrThrowCast(type, reassign(text, currentValue).getValue());110 } catch (CastFailure e) {111 return null;112 }113 }114 103 } -
java/main/src/main/java/com/framsticks/params/PropertiesAccess.java
r105 r107 112 112 } 113 113 } 114 -
java/main/src/main/java/com/framsticks/params/PropertiesObject.java
r105 r107 1 1 package com.framsticks.params; 2 2 3 import java.util.Map; 4 import java.util.TreeMap; 5 6 import com.framsticks.util.lang.Casting; 7 8 public class PropertiesObject { 9 10 protected final String framsTypeName; 11 protected final Map<String, Object> values = new TreeMap<>(); 3 public class PropertiesObject extends MapBasedObject { 12 4 13 5 /** 14 * @param framsTypeName6 * 15 7 */ 16 8 public PropertiesObject(String framsTypeName) { 17 this.framsTypeName = framsTypeName;9 super(framsTypeName); 18 10 } 19 11 20 public <T> void set(String id, T value) {21 values.put(id, value);22 }23 24 public <T> T get(String id, Class<T> type) {25 return Casting.nullOrThrowCast(type, values.get(id));26 }27 28 public void clear() {29 values.clear();30 }31 32 public int size() {33 return values.size();34 }35 36 @Override37 public String toString() {38 return values.toString();39 }40 41 /**42 * @return the framsTypeName43 */44 public String getFramsTypeName() {45 return framsTypeName;46 }47 12 } -
java/main/src/main/java/com/framsticks/params/ReflectionAccess.java
r105 r107 11 11 import com.framsticks.params.types.ProcedureParam; 12 12 import com.framsticks.util.FramsticksException; 13 import com.framsticks.util.Misc; 13 14 14 15 import static com.framsticks.util.lang.Containers.*; … … 90 91 } 91 92 } catch (FramsticksException e) { 92 throw e.arg("param", param).arg("value", value).arg(" access", this);93 throw e.arg("param", param).arg("value", value).arg("value type", Misc.getClass(value)).arg("access", this); 93 94 } 94 95 } -
java/main/src/main/java/com/framsticks/params/Registry.java
r103 r107 8 8 import com.framsticks.util.DoubleMap; 9 9 import com.framsticks.util.FramsticksException; 10 import com.framsticks.util.lang.Casting; 10 11 import com.framsticks.util.lang.Strings; 11 12 … … 53 54 registerAndBuild(r); 54 55 } 56 57 for (String i : ParamCandidate.getAllCandidates(javaClass).getDependentClassesFromInfo()) { 58 putFramsClass(FramsClassBuilder.readFromStream(getClass().getResourceAsStream("/info/" + i + ".info"))); 59 } 55 60 return this; 56 61 } … … 76 81 try { 77 82 if (!javaClasses.containsValue(javaClass)) { 83 registerAndBuild(javaClass); 84 } 85 86 if (!javaClasses.containsValue(javaClass)) { 78 87 throw new FramsticksException().msg("java class is not registered"); 79 88 } … … 114 123 } 115 124 116 public @Nonnull Access prepareAccess(CompositeParam param) throws ConstructionException { 117 return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName())); 118 } 119 120 public @Nonnull Access createAccess(@Nonnull String name) throws ConstructionException { 125 public @Nonnull Access prepareAccess(CompositeParam param, boolean force) throws ConstructionException { 126 return wrapAccessWithListIfNeeded(param, createAccess(param.getContainedTypeName(), force)); 127 } 128 129 public @Nonnull Access createAccess(@Nonnull Object object) throws ConstructionException { 130 if (object instanceof MapBasedObject) { 131 return createAccess(((MapBasedObject) object).getFramsTypeName()); 132 } 133 return createAccess(object.getClass()); 134 } 135 136 public @Nonnull Access createAccess(@Nonnull String name, boolean force) throws ConstructionException { 121 137 try { 122 138 Strings.assureNotEmpty(name); 123 139 FramsClass framsClass = getFramsClass(name); 124 140 if (framsClass == null) { 125 throw new ConstructionException().msg("framsclass is missing"); 141 if (!force) { 142 throw new ConstructionException().msg("framsclass is missing"); 143 } 144 return new FreeAccess(name); 126 145 } 127 146 … … 131 150 throw new FramsticksException().msg("failed to create access for name").arg("name", name).cause(e); 132 151 } 152 } 153 154 public @Nonnull Access createAccess(@Nonnull String name) throws ConstructionException { 155 return createAccess(name, false); 133 156 } 134 157 … … 166 189 167 190 public Access bindAccessFor(Object object) { 168 return createAccess(object.getClass()).select(object); 191 return createAccess(object).select(object); 192 } 193 194 public Registry register(ParamsPackage paramsPackage) { 195 paramsPackage.register(this); 196 return this; 197 } 198 199 public Registry registerAndBuild(ParamsPackage paramsPackage) { 200 paramsPackage.setBuild(true); 201 paramsPackage.register(this); 202 return this; 203 } 204 205 public Access bindAccessFor(Access parent, String paramName) { 206 CompositeParam childParam = Casting.throwCast(CompositeParam.class, parent.getParam(paramName)); 207 return prepareAccess(childParam, true).select(parent.get(childParam, Object.class)); 169 208 } 170 209 -
java/main/src/main/java/com/framsticks/params/UniqueListAccess.java
r105 r107 187 187 } 188 188 189 public int setUidOf(Object value, String uid) { 190 return uidAccess.select(value).set(uidName, uid); 191 } 192 193 protected int uidCounter = 1; 194 195 public String generateNextUid() { 196 return containedTypeName.substring(0, 1).toUpperCase() + Integer.toString(uidCounter++); 197 } 198 199 public String findNextFreeUid() { 200 String uid; 201 do { 202 uid = generateNextUid(); 203 } while (get(uid, Object.class) != null); 204 return uid; 205 } 206 189 207 protected int setByUid(Object object, String uid) { 190 208 if (uid == null) { 191 209 uid = getUidOf(object); 192 210 if (uid == null) { 193 log.error("failed to set - missing uid");194 return 0;211 uid = findNextFreeUid(); 212 setUidOf(object, uid); 195 213 } 196 214 } … … 205 223 @Override 206 224 public <T> int set(int i, T value) { 207 if (i != map.size()) { 208 throw new FramsticksUnsupportedOperationException().msg("setting element in unique list through index is available only for addition"); 209 } 210 set(getUidOf(value), value); 211 return 0; 225 if (value != null) { 226 if (i != map.size()) { 227 throw new FramsticksUnsupportedOperationException().msg("setting element in unique list through index is available only for addition"); 228 } 229 set(getUidOf(value), value); 230 return 0; 231 } 232 if (i >= map.size()) { 233 throw new FramsticksUnsupportedOperationException().msg("invalid index for removal"); 234 } 235 Object current = get(i, Object.class); 236 return setByUid(null, getUidOf(current)); 212 237 } 213 238 214 239 @Override 215 240 public <T> int set(String id, T value) { 241 if (id == null) { 242 return setByUid(value, null); 243 } 216 244 Integer i = Numbers.parse(id, Integer.class); 217 245 if (i != null) { -
java/main/src/main/java/com/framsticks/params/ValueParam.java
r105 r107 28 28 } 29 29 30 public abstract <T> String serialize(T value); 31 32 public abstract <T> T deserialize(String text, T currentValue, Class<T> type); 30 // public abstract <T> String serialize(T value); 33 31 34 32 } -
java/main/src/main/java/com/framsticks/params/annotations/FramsClassAnnotation.java
r99 r107 14 14 String[] order() default {}; 15 15 Class<?>[] register() default {}; 16 String[] registerFromInfo() default {}; 16 17 } -
java/main/src/main/java/com/framsticks/params/types/ListParam.java
r105 r107 16 16 } 17 17 18 @Override19 public <T> String serialize(T value) {20 return null;21 }18 // @Override 19 // public <T> String serialize(T value) { 20 // return null; 21 // } 22 22 23 @Override24 public <T> T deserialize(String text, T currentValue, Class<T> type) {25 return null;26 }27 23 } -
java/main/src/main/java/com/framsticks/params/types/ObjectParam.java
r105 r107 5 5 import com.framsticks.params.CompositeParam; 6 6 import com.framsticks.params.ParamBuilder; 7 import com.framsticks.params.ParamsUtil;8 7 import com.framsticks.params.ReassignResult; 9 8 … … 51 50 } 52 51 53 @Override 54 public <T> String serialize(T value) { 55 return ParamsUtil.serialize(value); 56 } 57 58 @Override 59 public <T> T deserialize(String text, T currentValue, Class<T> type) { 60 return ParamsUtil.deserialize(text, type); 61 } 52 // @Override 53 // public <T> String serialize(T value) { 54 // return ParamsUtil.serialize(value); 55 // } 62 56 63 57 } -
java/main/src/main/java/com/framsticks/params/types/StringParam.java
r105 r107 49 49 @Override 50 50 public String serialize(Object value) { 51 if (value == null) { 52 return ""; 53 } 51 54 assert value instanceof String; 52 55 String s = (String) value; -
java/main/src/main/java/com/framsticks/params/types/UniversalParam.java
r105 r107 35 35 @Override 36 36 public ReassignResult<Object> reassign(Object newValue, Object oldValue) throws CastFailure { 37 if (newValue instanceof String) { 38 return ReassignResult.create(ParamsUtil.deserialize((String) newValue, Object.class)); 39 } 37 40 return ReassignResult.create(newValue); 38 41 } … … 43 46 } 44 47 45 @Override46 public <T> T deserialize(String text, T currentValue, Class<T> type) {47 return ParamsUtil.deserialize(text, type);48 }49 48 50 49 }
Note: See TracChangeset
for help on using the changeset viewer.