source: java/main/src/main/java/com/framsticks/params/AccessOperations.java @ 101

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

HIGHLIGHTS:

  • improve tree side notes
  • improve GUI layout
  • add foldable list of occured events to EventControl?
  • improve automatic type conversion in proxy listeners
  • implement several Access functionalities as algorithms independent of Access type
  • introduce draft base classes for distributed experiments
  • automatically register dependant Java classes to FramsClass? registry
  • add testing prime experiment and configuration
  • simplify and improve task dispatching

CHANGELOG:
Improve task dispatching in RemoteTree?.

GUI no longer hangs on connection problems.

Make all dispatchers joinables.

Refactorize Thread dispatcher.

Remove Task and PeriodicTask?.

Use Java utilities in those situations.

Reworking tasks dispatching.

Fix bug in EventControl? listener dispatching.

Minor improvements.

Add testing configuration for ExternalProcess? in GUI.

More improvement to prime.

Support for USERREADONLY in GUI.

Add that flag to various params in Java classes.

Remove redundant register clauses from several FramsClassAnnotations?.

Automatically gather and register dependant classes.

Add configuration for prime.

Improve Simulator class.

Add prime.xml configuration.

Introduce draft Experiment and Simulator classes.

Add prime experiment tests.

Enclose typical map with listeners into SimpleUniqueList?.

Needfile works in GUI.

Improve needfile handling in Browser.

More improvement with NeedFile?.

Implementing needfile.

Update test.

Rename ChangeEvent? to TestChangeEvent?.

Automatic argument type search in RemoteTree? listeners.

MultiParamLoader? uses AccessProvider?. By default old implementation
enclosed in AccessStash? or Registry.

Minor changes.

Rename SourceInterface? to Source.

Also improve toString of File and ListSource?.

Remove unused SimpleSource? class.

Add clearing in HistoryControl?.

Show entries in table at EventControl?.

Improve EventControl?.

Add listeners registration to EventControl?.

Add foldable table to HistoryControl?.

Add control row to Procedure and Event controls.

Improve layout of controls.

Another minor change to gui layout.

Minor improvement in the SliderControl?.

Minor changes.

Move ReflectionAccess?.Backend to separate file.

It was to cluttered.

Cleanup in ReflectionAccess?.

Move setMin, setMax, setDef to AccessOperations?.

Extract loading operation into AccessOperations?.

Append Framsticks to name of UnsupportedOperationException?.

The java.lang.UnsupportedOperationException? was shadowing this class.

Rename params.Util to params.ParamsUtil?.

Several improvements.

Minor changes.

Implement revert functionality.

Improve local changes management.

Minor improvement.

Remove methods rendered superfluous after SideNoteKey? improvement.

Improve SideNoteKey?.

It is now generic type, so explicit type specification at
call site is no more needed.

Introduce SideNoteKey? interface.

Only Objects implementing that key may be used as side note keys.

Minor improvements.

Use strings instead of ValueControls? in several gui mappings.

File size: 4.2 KB
Line 
1package com.framsticks.params;
2
3import org.apache.logging.log4j.Logger;
4import org.apache.logging.log4j.LogManager;
5
6import com.framsticks.util.FramsticksException;
7import com.framsticks.util.lang.Containers;
8import com.framsticks.util.lang.Holder;
9// import com.framsticks.util.lang.Containers;
10
11import static com.framsticks.params.SetStateFlags.*;
12
13public final class AccessOperations {
14
15        private final static Logger log = LogManager.getLogger(SimpleAbstractAccess.class.getName());
16
17        /**
18         *
19         */
20        private AccessOperations() {
21        }
22
23        /**
24         * Simple String key, value class.
25         */
26        public static class Entry {
27
28                public final String key;
29                public final String value;
30
31                public Entry(String key, String value) {
32                        this.key = key;
33                        this.value = value;
34                }
35
36                @Override
37                public String toString() {
38                        return key + " = " + value;
39                }
40        }
41
42        private static Entry readEntry(Source source) {
43
44                String line;
45                String key = null;
46                StringBuilder value = null;
47                while ((line = source.readLine()) != null) {
48                        if (key == null) {
49                                int colonIndex = line.indexOf(':');
50                                if (colonIndex == -1) {
51                                        return null;
52                                }
53                                key = line.substring(0, colonIndex);
54                                String inlineValue = line.substring(colonIndex + 1);
55
56
57                                if (!inlineValue.startsWith("~")) {
58                                        return new Entry(key, inlineValue);
59                                }
60                                value = new StringBuilder();
61                                value.append(inlineValue.substring(1));
62                                continue;
63                        }
64                        if (value.length() != 0) {
65                                value.append(System.getProperty("line.separator"));
66                        }
67                        if (line.endsWith("~") && !line.endsWith("\\~")) {
68                                value.append(line.substring(0, line.length() - 1));
69                                return new Entry(key, value.toString().replaceAll("\\\\~", "~"));
70                        }
71                        value.append(line);
72                }
73                return null;
74        }
75
76        public static void load(Access access, Source source) {
77                Entry entry;
78                while ((entry = readEntry(source)) != null) {
79                        Param param = access.getParam(entry.key);
80                        if (param == null) {
81                                throw new FramsticksException().msg("param not found in access").arg("name", entry.key).arg("access", access);
82                        }
83                        if (!(param instanceof ValueParam)) {
84                                throw new FramsticksException().msg("param is not a value param").arg("param", param).arg("access", access);
85                        }
86                        if ((param.getFlags() & ParamFlags.DONTLOAD) == 0) {
87                                int retFlags = access.set((ValueParam) param, entry.value);
88                                if ((retFlags & (PSET_HITMIN | PSET_HITMAX)) != 0) {
89                                        String which = ((retFlags & PSET_HITMIN) != 0) ? "small" : "big";
90                                        log.warn("value of key '{}' was too {}, adjusted", entry.key, which);
91                                }
92                        }
93                }
94
95        }
96
97        public interface Adjuster {
98                public Holder<Object> adjust(ValueParam param);
99                public Class<? extends ValueParam> getParamType();
100        }
101
102        public static class MinAdjuster implements Adjuster {
103
104                /**
105                 *
106                 */
107                public MinAdjuster() {
108                }
109
110                @Override
111                public Class<? extends ValueParam> getParamType() {
112                        return PrimitiveParam.class;
113                }
114
115                @Override
116                public Holder<Object> adjust(ValueParam param) {
117                        Object value = ((PrimitiveParam<?>) param).getMin(Object.class);
118                        if (value == null) {
119                                return null;
120                        }
121                        return Holder.make(value);
122                }
123        }
124
125        public static class MaxAdjuster implements Adjuster {
126
127                /**
128                 *
129                 */
130                public MaxAdjuster() {
131                }
132
133                @Override
134                public Class<? extends ValueParam> getParamType() {
135                        return PrimitiveParam.class;
136                }
137
138                @Override
139                public Holder<Object> adjust(ValueParam param) {
140                        Object value = ((PrimitiveParam<?>) param).getMax(Object.class);
141                        if (value == null) {
142                                return null;
143                        }
144                        return Holder.make(value);
145                }
146        }
147
148        public static class DefAdjuster implements Adjuster {
149
150                protected final boolean numericOnly;
151
152                /**
153                 * @param numericOnly
154                 */
155                public DefAdjuster(boolean numericOnly) {
156                        this.numericOnly = numericOnly;
157                }
158
159                public Class<? extends ValueParam> getParamType() {
160                        return ValueParam.class;
161                }
162
163                @Override
164                public Holder<Object> adjust(ValueParam param) {
165                        if (numericOnly && !(param.isNumeric())) {
166                                return null;
167                        }
168                        return Holder.make(param.getDef(Object.class));
169                }
170        }
171
172        public static void adjustAll(Access access, Adjuster adjuster) {
173                for (ValueParam param : Containers.filterInstanceof(access.getParams(), adjuster.getParamType())) {
174                        Holder<Object> value = adjuster.adjust(param);
175                        if (value != null) {
176                                access.set(param, value.get());
177                        }
178                }
179        }
180
181}
Note: See TracBrowser for help on using the repository browser.