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