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.params.types.ObjectParam; |
---|
9 | import com.framsticks.util.FramsticksException; |
---|
10 | import static com.framsticks.params.SetStateFlags.*; |
---|
11 | |
---|
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 | |
---|
26 | private final static Logger log = Logger.getLogger(SimpleAbstractAccess.class.getName()); |
---|
27 | |
---|
28 | protected final FramsClass framsClass; |
---|
29 | |
---|
30 | /** |
---|
31 | * @param framsClass |
---|
32 | */ |
---|
33 | public SimpleAbstractAccess(FramsClass framsClass) { |
---|
34 | this.framsClass = framsClass; |
---|
35 | } |
---|
36 | |
---|
37 | @Override |
---|
38 | public final FramsClass getFramsClass() { |
---|
39 | return framsClass; |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * Simple String key, value class. |
---|
44 | */ |
---|
45 | public static class Entry { |
---|
46 | |
---|
47 | public final String key; |
---|
48 | public final String value; |
---|
49 | |
---|
50 | public Entry(String key, String value) { |
---|
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 | |
---|
67 | @Override |
---|
68 | public int getParamCount() { |
---|
69 | return framsClass.getParamCount(); |
---|
70 | } |
---|
71 | |
---|
72 | @Override |
---|
73 | public Param getParam(int i) { |
---|
74 | return framsClass.getParam(i); |
---|
75 | } |
---|
76 | |
---|
77 | @Override |
---|
78 | public Param getParam(String id) { |
---|
79 | return framsClass.getParam(id); |
---|
80 | } |
---|
81 | |
---|
82 | // @Override |
---|
83 | // public Param getGroupMember(int gi, int n) { |
---|
84 | // return framsClass.getGroupMember(gi, n); |
---|
85 | // } |
---|
86 | |
---|
87 | @Override |
---|
88 | public <T> T get(int i, Class<T> type) { |
---|
89 | return get(framsClass.getParamEntry(i, ValueParam.class), type); |
---|
90 | } |
---|
91 | |
---|
92 | @Override |
---|
93 | public <T> T get(String id, Class<T> type) { |
---|
94 | return get(framsClass.getParamEntry(id, ValueParam.class), type); |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | public <T> int set(int i, T value) { |
---|
99 | return set(framsClass.getParamEntry(i, ValueParam.class), value); |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | public <T> int set(String id, T value) { |
---|
104 | return set(framsClass.getParamEntry(id, ValueParam.class), value); |
---|
105 | } |
---|
106 | |
---|
107 | @Override |
---|
108 | public <T> int set(ValueParam param, T value) { |
---|
109 | |
---|
110 | //String id = param.getEffectiveId(); |
---|
111 | try { |
---|
112 | Object oldValue = get(param, param.getStorageType()); |
---|
113 | ReassignResult<?> result = param.reassign(value, oldValue); |
---|
114 | Object casted = result.getValue(); |
---|
115 | if (casted != null && !casted.equals(oldValue)) { |
---|
116 | internalSet(param, casted); |
---|
117 | } |
---|
118 | return result.getFlags(); |
---|
119 | } catch (CastFailure e) { |
---|
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); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | @Override |
---|
130 | public void setDefault(boolean numericOnly) { |
---|
131 | for (int i = 0; i < framsClass.getParamCount(); i++) { |
---|
132 | setDefault(i, numericOnly); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | @Override |
---|
137 | public void setDefault(int i, boolean numericOnly) { |
---|
138 | ValueParam entry = framsClass.getParamEntry(i, ValueParam.class); |
---|
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) { |
---|
153 | PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class); |
---|
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) { |
---|
169 | PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class); |
---|
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) { |
---|
178 | assert framsClass != null; |
---|
179 | sink.print(framsClass.getId()).print(":").breakLine(); |
---|
180 | for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) { |
---|
181 | Object value = get(p, Object.class); |
---|
182 | if ((value == null) || value.equals(p.getDef(Object.class))) { |
---|
183 | continue; |
---|
184 | } |
---|
185 | sink.print(p.getId()).print(":"); |
---|
186 | p.save(sink, value); |
---|
187 | sink.breakLine(); |
---|
188 | } |
---|
189 | sink.breakLine(); |
---|
190 | } |
---|
191 | |
---|
192 | private Entry readEntry(SourceInterface source) { |
---|
193 | |
---|
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); |
---|
206 | |
---|
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; |
---|
235 | } |
---|
236 | |
---|
237 | @Override |
---|
238 | public void load(SourceInterface source) { |
---|
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 | } |
---|
252 | if ((param.getFlags() & ParamFlags.DONTLOAD) != 0) { |
---|
253 | log.debug("DontLoad flag was set - not loading..."); |
---|
254 | } else { |
---|
255 | int retFlags = this.set((ValueParam) param, entry.value); |
---|
256 | if ((retFlags & (PSET_HITMIN | PSET_HITMAX)) != 0) { |
---|
257 | String which = ((retFlags & PSET_HITMIN) != 0) ? "small" : "big"; |
---|
258 | log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted"); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | protected abstract <T> void internalSet(ValueParam param, T value); |
---|
265 | |
---|
266 | @Override |
---|
267 | public Iterable<Param> getParams() { |
---|
268 | return framsClass.getParamEntries(); |
---|
269 | } |
---|
270 | |
---|
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 | |
---|
281 | /* |
---|
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 | |
---|
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 | } |
---|
308 | |
---|
309 | @Override |
---|
310 | public ParamBuilder buildParam(ParamBuilder builder) { |
---|
311 | return builder.name(getId()).type(ObjectParam.class).containedTypeName(getId()); |
---|
312 | } |
---|
313 | |
---|
314 | } |
---|