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