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