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.util.Map;
23
24 import com.cosylab.util.CommonException;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class StringConsumerMulticaster implements DoubleConsumer,
38 NumberConsumer, LongConsumer, StringConsumer, ObjectConsumer,
39 DoubleSeqConsumer, LongSeqConsumer, StringSeqConsumer, ObjectSeqConsumer
40 {
41 StringConsumer delegate;
42
43
44 public static final Class[] SUPPORTED_CONSUMER_TYPES = {
45 LongConsumer.class, NumberConsumer.class, DoubleConsumer.class,
46 ObjectConsumer.class, StringConsumer.class
47 };
48
49
50
51
52
53
54
55
56
57
58
59 public static final StringConsumer createDataConsumer(Class type,
60 StringConsumer delegate)
61 {
62 if (type == StringConsumer.class) {
63 return delegate;
64 }
65
66 if (type == LongConsumer.class || type == NumberConsumer.class
67 || type == DoubleConsumer.class || type == ObjectConsumer.class
68 || type == DoubleSeqConsumer.class || type == LongSeqConsumer.class
69 || type == StringSeqConsumer.class || type == ObjectSeqConsumer.class) {
70 return new StringConsumerMulticaster(delegate);
71 }
72
73 return null;
74 }
75
76
77
78
79
80
81 public StringConsumerMulticaster(StringConsumer delegate)
82 {
83 this.delegate = delegate;
84 }
85
86
87
88
89 public StringConsumerMulticaster()
90 {
91 }
92
93
94
95
96
97
98 public StringConsumer getDelegate()
99 {
100 return delegate;
101 }
102
103
104
105
106
107
108 public void setDelegate(StringConsumer delegate)
109 {
110 this.delegate = delegate;
111 }
112
113
114
115
116 public void updateValue(long timestamp, double value)
117 throws CommonException
118 {
119 delegate.updateValue(timestamp, Double.toString(value));
120 }
121
122
123
124
125 public void updateValue(long timestamp, Number value)
126 throws CommonException
127 {
128 if (value==null) {
129 delegate.updateValue(timestamp, "N/A");
130 return;
131 }
132 delegate.updateValue(timestamp, value.toString());
133 }
134
135
136
137
138 public void updateValue(long timestamp, long value)
139 throws CommonException
140 {
141 delegate.updateValue(timestamp, Long.toString(value));
142 }
143
144
145
146
147 public DataConsumer getDefaultDataConsumer()
148 {
149 return delegate.getDefaultDataConsumer();
150 }
151
152
153
154
155 public String getName()
156 {
157 return delegate.getName();
158 }
159
160
161
162
163 public void setCharacteristics(Map characteristics)
164 {
165 delegate.setCharacteristics(characteristics);
166 }
167
168
169
170
171 public void updateDataState(DataState state)
172 {
173 delegate.updateDataState(state);
174 }
175
176
177
178
179 public void updateValue(long timestamp, String value)
180 throws CommonException
181 {
182 if (value==null) {
183 delegate.updateValue(timestamp, "N/A");
184 return;
185 }
186 delegate.updateValue(timestamp, value);
187 }
188
189
190
191
192 public void updateValue(long timestamp, Object value)
193 throws CommonException
194 {
195 if (value==null) {
196 delegate.updateValue(timestamp, "N/A");
197 return;
198 }
199 delegate.updateValue(timestamp, value.toString());
200 }
201
202
203
204
205 public String[] getSupportedCharacteristics()
206 {
207 return delegate.getSupportedCharacteristics();
208 }
209
210
211
212
213 @SuppressWarnings("unchecked")
214 public Class[] getSupportedConsumerTypes()
215 {
216 return SUPPORTED_CONSUMER_TYPES;
217 }
218
219
220
221
222 public void updateValue(long timestamp, Object[] value) throws CommonException {
223 if (value==null) {
224 delegate.updateValue(timestamp, "N/A");
225 return;
226 }
227 delegate.updateValue(timestamp, DisplayerUtilities.toString(value));
228 }
229
230
231
232 public void updateValue(long timestamp, double[] value) throws CommonException {
233 if (value==null) {
234 delegate.updateValue(timestamp, "N/A");
235 return;
236 }
237 StringBuilder s= new StringBuilder(16*value.length);
238 s.append('[');
239 if (value.length>0) {
240 s.append(value[0]);
241 }
242 for (int i = 1; i < value.length; i++) {
243 s.append(',');
244 s.append(value[i]);
245 }
246 s.append(']');
247 delegate.updateValue(timestamp, s.toString());
248 }
249
250
251
252 public void updateValue(long timestamp, long[] value) throws CommonException {
253 if (value==null) {
254 delegate.updateValue(timestamp, "N/A");
255 return;
256 }
257 StringBuilder s= new StringBuilder(16*value.length);
258 s.append('[');
259 if (value.length>0) {
260 s.append(value[0]);
261 }
262 for (int i = 1; i < value.length; i++) {
263 s.append(',');
264 s.append(value[i]);
265 }
266 s.append(']');
267 delegate.updateValue(timestamp, s.toString());
268 }
269
270
271
272 public void updateValue(long timestamp, String[] value) throws CommonException {
273 if (value==null) {
274 delegate.updateValue(timestamp, "N/A");
275 return;
276 }
277 delegate.updateValue(timestamp, DisplayerUtilities.toString(value));
278 }
279
280
281
282
283
284 public <D extends DataConsumer> D getDataConsumer(Class<D> type) {
285 return delegate.getDataConsumer(type);
286 }
287 }
288
289