[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
| 3 | |
---|
[84] | 4 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
| 5 | |
---|
[77] | 6 | import org.apache.log4j.Logger; |
---|
| 7 | |
---|
[99] | 8 | import com.framsticks.params.types.ObjectParam; |
---|
| 9 | import com.framsticks.util.FramsticksException; |
---|
| 10 | import static com.framsticks.params.SetStateFlags.*; |
---|
[96] | 11 | |
---|
[77] | 12 | /** |
---|
| 13 | * The Class SimpleAbstractAccess implements all the methods of AccessInterface |
---|
| 14 | * which actions can be implemented with usage of {@link AccessInterface} methods |
---|
| 15 | * or concern schema, which is stored in {@link #framsClass} |
---|
| 16 | * |
---|
| 17 | * Based on c++ class SimpleAbstractParam located in: cpp/gdk/param.* |
---|
| 18 | * |
---|
| 19 | * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please |
---|
| 20 | * replace name and surname with my personal data) |
---|
| 21 | * |
---|
| 22 | * @author Piotr Sniegowski |
---|
| 23 | */ |
---|
| 24 | public abstract class SimpleAbstractAccess implements AccessInterface { |
---|
| 25 | |
---|
[84] | 26 | private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName()); |
---|
[77] | 27 | |
---|
[87] | 28 | protected final FramsClass framsClass; |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * @param framsClass |
---|
| 32 | */ |
---|
| 33 | public SimpleAbstractAccess(FramsClass framsClass) { |
---|
| 34 | this.framsClass = framsClass; |
---|
| 35 | } |
---|
| 36 | |
---|
[84] | 37 | @Override |
---|
[77] | 38 | public final FramsClass getFramsClass() { |
---|
| 39 | return framsClass; |
---|
| 40 | } |
---|
| 41 | |
---|
[84] | 42 | /** |
---|
[77] | 43 | * Simple String key, value class. |
---|
| 44 | */ |
---|
[78] | 45 | public static class Entry { |
---|
[77] | 46 | |
---|
[78] | 47 | public final String key; |
---|
| 48 | public final String value; |
---|
[77] | 49 | |
---|
[78] | 50 | public Entry(String key, String value) { |
---|
[77] | 51 | this.key = key; |
---|
| 52 | this.value = value; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | @Override |
---|
| 56 | public String toString() { |
---|
| 57 | return key + " = " + value; |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | @Override |
---|
| 63 | public String getId() { |
---|
| 64 | return framsClass.getId(); |
---|
| 65 | } |
---|
| 66 | |
---|
[84] | 67 | @Override |
---|
[77] | 68 | public int getParamCount() { |
---|
| 69 | return framsClass.getParamCount(); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | @Override |
---|
| 73 | public Param getParam(int i) { |
---|
[86] | 74 | return framsClass.getParam(i); |
---|
[77] | 75 | } |
---|
| 76 | |
---|
| 77 | @Override |
---|
| 78 | public Param getParam(String id) { |
---|
[86] | 79 | return framsClass.getParam(id); |
---|
[77] | 80 | } |
---|
| 81 | |
---|
[90] | 82 | // @Override |
---|
| 83 | // public Param getGroupMember(int gi, int n) { |
---|
| 84 | // return framsClass.getGroupMember(gi, n); |
---|
| 85 | // } |
---|
[77] | 86 | |
---|
| 87 | @Override |
---|
| 88 | public <T> T get(int i, Class<T> type) { |
---|
[86] | 89 | return get(framsClass.getParamEntry(i, ValueParam.class), type); |
---|
[77] | 90 | } |
---|
| 91 | |
---|
| 92 | @Override |
---|
| 93 | public <T> T get(String id, Class<T> type) { |
---|
[86] | 94 | return get(framsClass.getParamEntry(id, ValueParam.class), type); |
---|
[77] | 95 | } |
---|
| 96 | |
---|
| 97 | @Override |
---|
[86] | 98 | public <T> int set(int i, T value) { |
---|
| 99 | return set(framsClass.getParamEntry(i, ValueParam.class), value); |
---|
[77] | 100 | } |
---|
| 101 | |
---|
| 102 | @Override |
---|
[86] | 103 | public <T> int set(String id, T value) { |
---|
| 104 | return set(framsClass.getParamEntry(id, ValueParam.class), value); |
---|
[77] | 105 | } |
---|
| 106 | |
---|
| 107 | @Override |
---|
[84] | 108 | public <T> int set(ValueParam param, T value) { |
---|
| 109 | |
---|
[77] | 110 | //String id = param.getEffectiveId(); |
---|
| 111 | try { |
---|
[84] | 112 | Object oldValue = get(param, param.getStorageType()); |
---|
| 113 | ReassignResult<?> result = param.reassign(value, oldValue); |
---|
| 114 | Object casted = result.getValue(); |
---|
[98] | 115 | if (casted != null && !casted.equals(oldValue)) { |
---|
[84] | 116 | internalSet(param, casted); |
---|
| 117 | } |
---|
[99] | 118 | return result.getFlags(); |
---|
[84] | 119 | } catch (CastFailure e) { |
---|
[99] | 120 | throw new FramsticksException() |
---|
| 121 | .msg("casting failure while set") |
---|
| 122 | .arg("param", param) |
---|
| 123 | .arg("value", value) |
---|
| 124 | .arg("value's type", (value == null ? "<null>" : value.getClass().getCanonicalName())) |
---|
| 125 | .arg("in", this).cause(e); |
---|
[84] | 126 | } |
---|
| 127 | } |
---|
[77] | 128 | |
---|
| 129 | @Override |
---|
| 130 | public void setDefault(boolean numericOnly) { |
---|
[84] | 131 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
[77] | 132 | setDefault(i, numericOnly); |
---|
[84] | 133 | } |
---|
[77] | 134 | } |
---|
| 135 | |
---|
| 136 | @Override |
---|
| 137 | public void setDefault(int i, boolean numericOnly) { |
---|
[84] | 138 | ValueParam entry = framsClass.getParamEntry(i, ValueParam.class); |
---|
[77] | 139 | if ((entry != null) && (!numericOnly || entry.isNumeric())) { |
---|
| 140 | set(i, entry.getDef(entry.getStorageType())); |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | @Override |
---|
| 145 | public void setMin() { |
---|
| 146 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
| 147 | setMin(i); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | @Override |
---|
| 152 | public void setMin(int i) { |
---|
[87] | 153 | PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class); |
---|
[77] | 154 | Object min = entry.getMin(entry.getStorageType()); |
---|
| 155 | if (min != null) { |
---|
| 156 | set(i, min); |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | @Override |
---|
| 161 | public void setMax() { |
---|
| 162 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
| 163 | setMax(i); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | @Override |
---|
| 168 | public void setMax(int i) { |
---|
[87] | 169 | PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class); |
---|
[77] | 170 | Object max = entry.getMax(entry.getStorageType()); |
---|
| 171 | if (max != null) { |
---|
| 172 | set(i, max); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | @Override |
---|
| 177 | public void save(SinkInterface sink) { |
---|
[84] | 178 | assert framsClass != null; |
---|
| 179 | sink.print(framsClass.getId()).print(":").breakLine(); |
---|
[87] | 180 | for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) { |
---|
[84] | 181 | Object value = get(p, Object.class); |
---|
[96] | 182 | if ((value == null) || value.equals(p.getDef(Object.class))) { |
---|
[84] | 183 | continue; |
---|
| 184 | } |
---|
| 185 | sink.print(p.getId()).print(":"); |
---|
| 186 | p.save(sink, value); |
---|
| 187 | sink.breakLine(); |
---|
| 188 | } |
---|
| 189 | sink.breakLine(); |
---|
| 190 | } |
---|
[77] | 191 | |
---|
[97] | 192 | private Entry readEntry(SourceInterface source) { |
---|
[77] | 193 | |
---|
[84] | 194 | String line; |
---|
| 195 | String key = null; |
---|
| 196 | StringBuilder value = null; |
---|
| 197 | while ((line = source.readLine()) != null) |
---|
| 198 | { |
---|
| 199 | if (key == null) { |
---|
| 200 | int colonIndex = line.indexOf(':'); |
---|
| 201 | if (colonIndex == -1) { |
---|
| 202 | return null; |
---|
| 203 | } |
---|
| 204 | key = line.substring(0, colonIndex); |
---|
| 205 | String inlineValue = line.substring(colonIndex + 1); |
---|
[77] | 206 | |
---|
[84] | 207 | |
---|
| 208 | if (!inlineValue.startsWith("~")) { |
---|
| 209 | return new Entry(key, inlineValue); |
---|
| 210 | } |
---|
| 211 | value = new StringBuilder(); |
---|
| 212 | value.append(inlineValue.substring(1)); |
---|
| 213 | continue; |
---|
| 214 | } |
---|
| 215 | if (value.length() != 0) { |
---|
| 216 | value.append(System.getProperty("line.separator")); |
---|
| 217 | } |
---|
| 218 | if (line.contains("~")) { |
---|
| 219 | value.append(line.substring(0, line.indexOf("~"))); |
---|
| 220 | return new Entry(key, value.toString()); |
---|
| 221 | } |
---|
| 222 | value.append(line); |
---|
| 223 | /* |
---|
| 224 | if (line.contains("~")) { |
---|
| 225 | String lastLine = line.substring(0, line.indexOf("~")); |
---|
| 226 | if (lastLine.length() > 0) { |
---|
| 227 | appendToValue(value, lastLine); |
---|
| 228 | } |
---|
| 229 | return new Entry(key, value.toString()); |
---|
| 230 | } |
---|
| 231 | appendToValue(value, line); |
---|
| 232 | */ |
---|
| 233 | } |
---|
| 234 | return null; |
---|
[77] | 235 | } |
---|
| 236 | |
---|
| 237 | @Override |
---|
[97] | 238 | public void load(SourceInterface source) { |
---|
[84] | 239 | //TODO not clearing values, because get from manager gives only fields, not children |
---|
| 240 | //this.clearValues(); |
---|
| 241 | |
---|
| 242 | Entry entry; |
---|
| 243 | while ((entry = readEntry(source)) != null) { |
---|
| 244 | Param param = getParam(entry.key); |
---|
| 245 | if (param == null) { |
---|
| 246 | continue; |
---|
| 247 | } |
---|
| 248 | if (!(param instanceof ValueParam)) { |
---|
| 249 | log.warn("param " + param + " is not a ValueParam"); |
---|
| 250 | continue; |
---|
| 251 | } |
---|
[99] | 252 | if ((param.getFlags() & ParamFlags.DONTLOAD) != 0) { |
---|
[84] | 253 | log.debug("DontLoad flag was set - not loading..."); |
---|
| 254 | } else { |
---|
| 255 | int retFlags = this.set((ValueParam) param, entry.value); |
---|
[99] | 256 | if ((retFlags & (PSET_HITMIN | PSET_HITMAX)) != 0) { |
---|
| 257 | String which = ((retFlags & PSET_HITMIN) != 0) ? "small" : "big"; |
---|
[84] | 258 | log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted"); |
---|
[77] | 259 | } |
---|
| 260 | } |
---|
| 261 | } |
---|
[84] | 262 | } |
---|
[77] | 263 | |
---|
[84] | 264 | protected abstract <T> void internalSet(ValueParam param, T value); |
---|
[77] | 265 | |
---|
[84] | 266 | @Override |
---|
[98] | 267 | public Iterable<Param> getParams() { |
---|
[84] | 268 | return framsClass.getParamEntries(); |
---|
[77] | 269 | } |
---|
| 270 | |
---|
[98] | 271 | @Override |
---|
| 272 | public int getCompositeParamCount() { |
---|
| 273 | return framsClass.getCompositeParamCount(); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | @Override |
---|
| 277 | public CompositeParam getCompositeParam(int number) { |
---|
| 278 | return framsClass.getCompositeParam(number); |
---|
| 279 | } |
---|
| 280 | |
---|
[84] | 281 | /* |
---|
[77] | 282 | protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) { |
---|
| 283 | int flags = 0; |
---|
| 284 | T val = type.cast(value); |
---|
| 285 | T min = param.getMin(type); |
---|
| 286 | T max = param.getMax(type); |
---|
| 287 | if (min != null && val.compareTo(min) < 0) { |
---|
| 288 | val = min; |
---|
| 289 | flags |= Flags.PSET_HITMIN; |
---|
| 290 | } |
---|
| 291 | if (max != null && val.compareTo(max) > 0) { |
---|
| 292 | val = max; |
---|
| 293 | flags |= Flags.PSET_HITMAX; |
---|
| 294 | } |
---|
| 295 | internalSet(param, val); |
---|
| 296 | return flags; |
---|
| 297 | }*/ |
---|
| 298 | |
---|
[99] | 299 | @Override |
---|
| 300 | public String toString() { |
---|
| 301 | StringBuilder b = new StringBuilder(); |
---|
| 302 | b.append(framsClass); |
---|
| 303 | if (getSelected() != null) { |
---|
| 304 | b.append("(").append(getSelected()).append(")"); |
---|
| 305 | } |
---|
| 306 | return b.toString(); |
---|
| 307 | } |
---|
[77] | 308 | |
---|
[99] | 309 | @Override |
---|
| 310 | public ParamBuilder buildParam(ParamBuilder builder) { |
---|
| 311 | return builder.name(getId()).type(ObjectParam.class).containedTypeName(getId()); |
---|
| 312 | } |
---|
| 313 | |
---|
[77] | 314 | } |
---|