source: java/main/src/main/java/com/framsticks/params/SimpleAbstractAccess.java @ 87

Last change on this file since 87 was 87, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • FramsClass? and contained Param are now immutable classes (like String),

which allows to refer to them concurrently without synchronization
(which for example in turn simplifies GUI management)

  • also make Path immutable (which was earlier only assumed)
  • add global cache for FramsClasses? created solely and automatically

on base of Java classes.

representations basing on given FramsClass?

  • above changes greatly improved GUI responsivness during browsing
  • furtherly improve Param class hierarchy
  • allow to inject actions on state changes into MultiParamLoader?
  • add more tests

CHANGELOG:

Add StatusListener? to MultiParamLoader?.

Minor refactorization in MultiParamLoader?.

First step with auto append.

Add SchemaTest?.

Improve Registry.

Clean up in Registry.

Work out Registry.

Use annotations for Param.

Fix ListChange?.

Improve fluent interface of the FramsClassBuilder?.

Done caching of ReflectionAccess?.Backend

Fix hashCode of Pair.

A step on a way to cache ReflectionAccess?.Backend

Make SimpleAbstractAccess?.framsClass a final field.

Add static cache for FramsClasses? based on java.

Only classes created strictly and automatically
based on java classes are using this cache.

Make all Params immutable.

Many improvement to make Param immutable.

Make PrimitiveParam? generic type.

Several changes to make Param immutable.

Make FramsClass? immutable.

Another improvement to Path immutability.

Several improvements to Path.

Improve PathTest?.

Configurarable MutabilityDetector?.

File size: 7.0 KB
Line 
1package com.framsticks.params;
2
3import java.io.IOException;
4import java.util.Collection;
5
6import static com.framsticks.util.lang.Containers.filterInstanceof;
7
8import 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 */
22public 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.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                if (entry == null) {
150                        return;
151                }
152                Object min = entry.getMin(entry.getStorageType());
153                if (min != null) {
154                        set(i, min);
155                }
156        }
157
158        @Override
159        public void setMax() {
160                for (int i = 0; i < framsClass.getParamCount(); i++) {
161                        setMax(i);
162                }
163        }
164
165        @Override
166        public void setMax(int i) {
167                PrimitiveParam<?> entry = framsClass.getParamEntry(i, PrimitiveParam.class);
168                if (entry == null) {
169                        return;
170                }
171                Object max = entry.getMax(entry.getStorageType());
172                if (max != null) {
173                        set(i, max);
174                }
175        }
176
177        @Override
178        public void copyFrom(AccessInterface src) {
179                clearValues();
180                //TODO: iterate over self, and pull from src
181                /*
182                for (int i = 0; i < src.getFramsClass().size(); i++) {
183                        this.set(i, src.get(i, Object.class));
184                }
185                */
186        }
187
188
189
190
191        @Override
192        public void save(SinkInterface sink) {
193                assert framsClass != null;
194                sink.print(framsClass.getId()).print(":").breakLine();
195                for (PrimitiveParam<?> p : filterInstanceof(framsClass.getParamEntries(), PrimitiveParam.class)) {
196                        Object value = get(p, Object.class);
197                        if (value == null) {
198                                continue;
199                        }
200                        sink.print(p.getId()).print(":");
201                        p.save(sink, value);
202                        sink.breakLine();
203                }
204                sink.breakLine();
205        }
206
207        private Entry readEntry(SourceInterface source)
208                        throws IOException {
209
210                String line;
211                String key = null;
212                StringBuilder value = null;
213                while ((line = source.readLine()) != null)
214                {
215                        if (key == null) {
216                                int colonIndex = line.indexOf(':');
217                                if (colonIndex == -1) {
218                                        return null;
219                                }
220                                key = line.substring(0, colonIndex);
221                                String inlineValue = line.substring(colonIndex + 1);
222
223
224                                if (!inlineValue.startsWith("~")) {
225                                        return new Entry(key, inlineValue);
226                                }
227                                value = new StringBuilder();
228                                value.append(inlineValue.substring(1));
229                                continue;
230                        }
231                        if (value.length() != 0) {
232                                value.append(System.getProperty("line.separator"));
233                        }
234                        if (line.contains("~")) {
235                                value.append(line.substring(0, line.indexOf("~")));
236                                return new Entry(key, value.toString());
237                        }
238                        value.append(line);
239                        /*
240                        if (line.contains("~")) {
241                                String lastLine = line.substring(0, line.indexOf("~"));
242                                if (lastLine.length() > 0) {
243                                        appendToValue(value, lastLine);
244                                }
245                                return new Entry(key, value.toString());
246                        }
247                        appendToValue(value, line);
248                        */
249                }
250                return null;
251        }
252
253        @Override
254        public void load(SourceInterface source) throws Exception {
255                //TODO not clearing values, because get from manager gives only fields, not children
256                //this.clearValues();
257
258                Entry entry;
259                while ((entry = readEntry(source)) != null) {
260                        Param param = getParam(entry.key);
261                        if (param == null) {
262                                continue;
263                        }
264                        if (!(param instanceof ValueParam)) {
265                                log.warn("param " + param + " is not a ValueParam");
266                                continue;
267                        }
268                        if ((param.getFlags() & Flags.DONTLOAD) != 0) {
269                                log.debug("DontLoad flag was set - not loading...");
270                        } else {
271                                int retFlags = this.set((ValueParam) param, entry.value);
272                                if ((retFlags & (Flags.PSET_HITMIN | Flags.PSET_HITMAX)) != 0) {
273                                        String which = ((retFlags & Flags.PSET_HITMIN) != 0) ? "small" : "big";
274                                        log.warn("value of key '" + entry.key + "' was too " + which + ", adjusted");
275                                }
276                        }
277                }
278        }
279
280        protected abstract <T> void internalSet(ValueParam param, T value);
281
282        @Override
283        public Collection<Param> getParams() {
284                return framsClass.getParamEntries();
285        }
286
287        /*
288        protected <T extends Comparable<T>> int setAndCut(Param param, Object value, Class<T> type) {
289                int flags = 0;
290                T val = type.cast(value);
291                T min = param.getMin(type);
292                T max = param.getMax(type);
293                if (min != null && val.compareTo(min) < 0) {
294                        val = min;
295                        flags |= Flags.PSET_HITMIN;
296                }
297                if (max != null && val.compareTo(max) > 0) {
298                        val = max;
299                        flags |= Flags.PSET_HITMAX;
300                }
301                internalSet(param, val);
302                return flags;
303        }*/
304
305
306}
Note: See TracBrowser for help on using the repository browser.