[77] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
| 3 | import java.io.IOException; |
---|
| 4 | import java.util.Collection; |
---|
| 5 | |
---|
[84] | 6 | import static com.framsticks.util.lang.Containers.filterInstanceof; |
---|
| 7 | |
---|
[77] | 8 | import org.apache.log4j.Logger; |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * The Class SimpleAbstractAccess implements all the methods of AccessInterface |
---|
| 12 | * which actions can be implemented with usage of {@link AccessInterface} methods |
---|
| 13 | * or concern schema, which is stored in {@link #framsClass} |
---|
| 14 | * |
---|
| 15 | * Based on c++ class SimpleAbstractParam located in: cpp/gdk/param.* |
---|
| 16 | * |
---|
| 17 | * @author Jarek Szymczak <name.surname@gmail.com>, Mateusz Jarus (please |
---|
| 18 | * replace name and surname with my personal data) |
---|
| 19 | * |
---|
| 20 | * @author Piotr Sniegowski |
---|
| 21 | */ |
---|
| 22 | public abstract class SimpleAbstractAccess implements AccessInterface { |
---|
| 23 | |
---|
[84] | 24 | private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName()); |
---|
[77] | 25 | |
---|
[84] | 26 | @Override |
---|
[77] | 27 | public final FramsClass getFramsClass() { |
---|
| 28 | return framsClass; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | public void setFramsClass(FramsClass framsClass) { |
---|
| 32 | this.framsClass = framsClass; |
---|
| 33 | } |
---|
[84] | 34 | /** |
---|
[77] | 35 | * Simple String key, value class. |
---|
| 36 | */ |
---|
[78] | 37 | public static class Entry { |
---|
[77] | 38 | |
---|
[78] | 39 | public final String key; |
---|
| 40 | public final String value; |
---|
[77] | 41 | |
---|
[78] | 42 | public Entry(String key, String value) { |
---|
[77] | 43 | this.key = key; |
---|
| 44 | this.value = value; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | @Override |
---|
| 48 | public String toString() { |
---|
| 49 | return key + " = " + value; |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | protected FramsClass framsClass; |
---|
| 54 | |
---|
| 55 | @Override |
---|
| 56 | public String getId() { |
---|
| 57 | return framsClass.getId(); |
---|
| 58 | } |
---|
| 59 | |
---|
[84] | 60 | @Override |
---|
[77] | 61 | public int getParamCount() { |
---|
| 62 | return framsClass.getParamCount(); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | @Override |
---|
| 66 | public Param getParam(int i) { |
---|
[84] | 67 | return framsClass.getParamEntry(i, Param.class); |
---|
[77] | 68 | } |
---|
| 69 | |
---|
| 70 | @Override |
---|
| 71 | public Param getParam(String id) { |
---|
[84] | 72 | return framsClass.getParamEntry(id, Param.class); |
---|
[77] | 73 | } |
---|
| 74 | |
---|
| 75 | @Override |
---|
| 76 | public Param getGroupMember(int gi, int n) { |
---|
| 77 | return framsClass.getGroupMember(gi, n); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | @Override |
---|
| 81 | public <T> T get(int i, Class<T> type) { |
---|
[84] | 82 | Param p = getParam(i); |
---|
| 83 | assert p instanceof ValueParam; |
---|
| 84 | return get((ValueParam) p, type); |
---|
[77] | 85 | } |
---|
| 86 | |
---|
| 87 | @Override |
---|
| 88 | public <T> T get(String id, Class<T> type) { |
---|
[84] | 89 | Param p = getParam(id); |
---|
| 90 | assert p instanceof ValueParam; |
---|
| 91 | return get((ValueParam) p, type); |
---|
[77] | 92 | } |
---|
| 93 | |
---|
| 94 | @Override |
---|
| 95 | public <T> int set(String id, T value) { |
---|
[84] | 96 | Param p = getParam(id); |
---|
| 97 | assert p instanceof ValueParam; |
---|
| 98 | return set((ValueParam) p, value); |
---|
[77] | 99 | } |
---|
| 100 | |
---|
| 101 | @Override |
---|
| 102 | public <T> int set(int i, T value) { |
---|
[84] | 103 | Param p = getParam(i); |
---|
| 104 | assert p instanceof ValueParam; |
---|
| 105 | return set((ValueParam) p, value); |
---|
[77] | 106 | } |
---|
| 107 | |
---|
| 108 | @Override |
---|
[84] | 109 | public <T> int set(ValueParam param, T value) { |
---|
[77] | 110 | int flags = 0; |
---|
[84] | 111 | |
---|
[77] | 112 | //String id = param.getEffectiveId(); |
---|
| 113 | try { |
---|
[84] | 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 | } |
---|
[77] | 126 | |
---|
| 127 | @Override |
---|
| 128 | public void setDefault(boolean numericOnly) { |
---|
[84] | 129 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
[77] | 130 | setDefault(i, numericOnly); |
---|
[84] | 131 | } |
---|
[77] | 132 | } |
---|
| 133 | |
---|
| 134 | @Override |
---|
| 135 | public void setDefault(int i, boolean numericOnly) { |
---|
[84] | 136 | ValueParam entry = framsClass.getParamEntry(i, ValueParam.class); |
---|
[77] | 137 | if ((entry != null) && (!numericOnly || entry.isNumeric())) { |
---|
| 138 | set(i, entry.getDef(entry.getStorageType())); |
---|
| 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | @Override |
---|
| 143 | public void setMin() { |
---|
| 144 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
| 145 | setMin(i); |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | @Override |
---|
| 150 | public void setMin(int i) { |
---|
[84] | 151 | PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class); |
---|
| 152 | if (entry == null) { |
---|
| 153 | return; |
---|
| 154 | } |
---|
[77] | 155 | Object min = entry.getMin(entry.getStorageType()); |
---|
| 156 | if (min != null) { |
---|
| 157 | set(i, min); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | @Override |
---|
| 162 | public void setMax() { |
---|
| 163 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
| 164 | setMax(i); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | @Override |
---|
| 169 | public void setMax(int i) { |
---|
[84] | 170 | PrimitiveParam entry = framsClass.getParamEntry(i, PrimitiveParam.class); |
---|
| 171 | if (entry == null) { |
---|
| 172 | return; |
---|
| 173 | } |
---|
[77] | 174 | Object max = entry.getMax(entry.getStorageType()); |
---|
| 175 | if (max != null) { |
---|
| 176 | set(i, max); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | @Override |
---|
| 181 | public void copyFrom(AccessInterface src) { |
---|
| 182 | clearValues(); |
---|
| 183 | //TODO: iterate over self, and pull from src |
---|
| 184 | /* |
---|
| 185 | for (int i = 0; i < src.getFramsClass().size(); i++) { |
---|
| 186 | this.set(i, src.get(i, Object.class)); |
---|
| 187 | } |
---|
| 188 | */ |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | @Override |
---|
| 195 | public void save(SinkInterface sink) { |
---|
[84] | 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 | } |
---|
[77] | 209 | |
---|
[84] | 210 | private Entry readEntry(SourceInterface source) |
---|
| 211 | throws IOException { |
---|
[77] | 212 | |
---|
[84] | 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; |
---|
| 222 | } |
---|
| 223 | key = line.substring(0, colonIndex); |
---|
| 224 | String inlineValue = line.substring(colonIndex + 1); |
---|
[77] | 225 | |
---|
[84] | 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; |
---|
[77] | 254 | } |
---|
| 255 | |
---|
| 256 | @Override |
---|
[84] | 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..."); |
---|
| 273 | } else { |
---|
| 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"); |
---|
[77] | 278 | } |
---|
| 279 | } |
---|
| 280 | } |
---|
[84] | 281 | } |
---|
[77] | 282 | |
---|
[84] | 283 | protected abstract <T> void internalSet(ValueParam param, T value); |
---|
[77] | 284 | |
---|
[84] | 285 | @Override |
---|
| 286 | public Collection<Param> getParams() { |
---|
| 287 | return framsClass.getParamEntries(); |
---|
[77] | 288 | } |
---|
| 289 | |
---|
[84] | 290 | /* |
---|
[77] | 291 | protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) { |
---|
| 292 | int flags = 0; |
---|
| 293 | T val = type.cast(value); |
---|
| 294 | T min = param.getMin(type); |
---|
| 295 | T max = param.getMax(type); |
---|
| 296 | if (min != null && val.compareTo(min) < 0) { |
---|
| 297 | val = min; |
---|
| 298 | flags |= Flags.PSET_HITMIN; |
---|
| 299 | } |
---|
| 300 | if (max != null && val.compareTo(max) > 0) { |
---|
| 301 | val = max; |
---|
| 302 | flags |= Flags.PSET_HITMAX; |
---|
| 303 | } |
---|
| 304 | internalSet(param, val); |
---|
| 305 | return flags; |
---|
| 306 | }*/ |
---|
| 307 | |
---|
| 308 | |
---|
| 309 | } |
---|