1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.displayers;
21
22 import java.io.IOException;
23 import java.io.ObjectStreamException;
24 import java.io.Serializable;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30
31
32
33
34
35
36
37
38
39 public class DisplayerParameters implements Serializable {
40
41 private static final long serialVersionUID = 794831250607895558L;
42 protected String name;
43 transient protected String[] names;
44 transient protected Map<String, Object> values;
45
46
47
48
49 public DisplayerParameters(String name) {
50 this(name, 10);
51 }
52
53
54
55
56 public DisplayerParameters(String name, int size) {
57 this.name = name;
58 values = new HashMap<String, Object>(size);
59 }
60
61
62
63
64 protected DisplayerParameters(String name, List<String> names, List values) {
65 this.name = name;
66 this.values = new HashMap<String, Object>(names.size());
67 for (int i = 0; i < names.size(); i++) {
68 this.values.put(names.get(i), values.get(i));
69 }
70 }
71
72
73
74
75 protected DisplayerParameters(String name, String[] names, Object[] values) {
76 this.name = name;
77 this.values = new HashMap<String, Object>(names.length);
78 for (int i = 0; i < names.length; i++) {
79 this.values.put(names[i], values[i]);
80 }
81 }
82
83 protected DisplayerParameters(String name, int size, double min,
84 double max, String units, String format) {
85 this(name, size);
86 values.put(Displayer.C_MINIMUM, min);
87 values.put(Displayer.C_MAXIMUM, max);
88 values.put(Displayer.C_UNITS, units);
89 values.put(Displayer.C_FORMAT,format);
90 }
91
92 protected DisplayerParameters(String name, int size, DoubleDisplayer disp) {
93 this(name, size, disp.getMinimum(), disp.getMaximum(), disp.getUnits(),
94 disp.getFormat());
95 }
96
97
98
99
100
101
102
103
104 public DisplayerParameters(String name, DoubleDisplayer disp) {
105 this(name, 4, disp.getMinimum(), disp.getMaximum(), disp.getUnits(),
106 disp.getFormat());
107 }
108
109
110
111
112
113
114
115
116
117
118 public DisplayerParameters(String name, double min, double max,
119 String units, String format) {
120 this(name, 4, min, max, units, format);
121 }
122
123
124
125
126
127
128
129
130 public DisplayerParameters(String name, Map<String, Object> values) {
131 this(name);
132 this.values.putAll(values);
133 }
134
135
136
137
138
139
140
141
142 public Object addParameter(String name, Object value) {
143 names=null;
144 return values.put(name, value);
145 }
146
147
148
149
150
151
152
153
154
155 public Object putParameter(String name, Object value) {
156 return values.put(name, value);
157 }
158
159
160
161
162
163
164
165 public Object getParameter(String name) {
166 return values.get(name);
167 }
168
169
170
171
172
173
174 public String getName() {
175 return name;
176 }
177
178
179
180
181
182
183 @Override
184 public String toString() {
185 String[] n= getNames();
186
187 StringBuilder s = new StringBuilder(32 * n.length);
188 s.append(name);
189 s.append("::{ ");
190 if (0 < n.length) {
191 s.append(n[0]);
192 s.append("=");
193 s.append(values.get(n[0]));
194 }
195 for (int i = 1; i < n.length; i++) {
196 s.append(", ");
197 s.append(n[i]);
198 s.append("=");
199 s.append(values.get(n[i]));
200 }
201 s.append('}');
202 return s.toString();
203 }
204
205
206
207
208 public String getFormat() {
209 return (String)values.get(Displayer.C_FORMAT);
210 }
211
212
213
214
215 public double getMaximum() {
216 return getDouble(Displayer.C_MAXIMUM);
217 }
218
219
220
221
222 public double getMinimum() {
223 return getDouble(Displayer.C_MINIMUM);
224 }
225
226 protected double getDouble(String name) {
227 Object o = values.get(name);
228 if (o instanceof Number) {
229 return ((Number)o).doubleValue();
230 }
231 return Double.NaN;
232 }
233
234 protected int getInt(String name) {
235 Object o = values.get(name);
236 if (o instanceof Number) {
237 return ((Number)o).intValue();
238 }
239 return 0;
240 }
241
242 protected boolean getBool(String name) {
243 Object o= values.get(name);
244 return (o instanceof Boolean) && ((Boolean)o).booleanValue();
245 }
246
247
248
249
250 public String getUnits() {
251 return (String)values.get(Displayer.C_UNITS);
252 }
253
254
255
256
257
258
259 public String[] getNames() {
260 if (names!=null) {
261 return names;
262 }
263 names= values.keySet().toArray(new String[values.size()]);
264 Arrays.sort(names);
265 return names;
266 }
267
268
269
270
271
272 public Object[] getValues() {
273 String[] n= getNames();
274 Object[] o= new Object[values.size()];
275 for (int i = 0; i < o.length; i++) {
276 o[i]=values.get(n[i]);
277 }
278 return o;
279 }
280
281
282
283
284
285
286 public int count() {
287 return values.size();
288 }
289
290
291
292
293
294
295 @Override
296 public boolean equals(Object o) {
297 if (!(o instanceof DisplayerParameters))
298 return false;
299 DisplayerParameters p = (DisplayerParameters)o;
300 Object[] val = p.getValues();
301 String[] names = p.getNames();
302 return (Arrays.equals(val, getValues()) && Arrays.equals(names,
303 getNames()));
304 }
305
306
307
308
309
310 @Override
311 public int hashCode() {
312 return toString().hashCode();
313 }
314
315 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
316 out.defaultWriteObject();
317 String[] n= getNames();
318 Object[] o= getValues();
319 out.writeObject(n);
320 out.writeObject(o);
321 }
322 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
323 in.defaultReadObject();
324 String[] n= (String[])in.readObject();
325 Object[] o= (Object[])in.readObject();
326
327 values= new HashMap<String, Object>(n.length);
328 for (int i = 0; i < o.length; i++) {
329 values.put(n[i], o[i]);
330 }
331 }
332 @SuppressWarnings("unused")
333 private void readObjectNoData() throws ObjectStreamException {
334 values= new HashMap<String, Object>(0);
335 }
336 }
337
338