1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans.
5 *
6 * CosyBeans is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.displayers;
21
22 import java.util.BitSet;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import com.cosylab.util.BitSetUtilities;
27 import com.cosylab.util.CommonException;
28
29
30 /**
31 * <code>LongConsumerMulticaster</code> casts different data consumers to
32 * LongConsumer. ObjectConsumer cast must receive <code>Number</code>
33 * instances. Delegate must be set, before any other method can be used.
34 *
35 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
36 * @version $Id: LongConsumerMulticaster.java,v 1.7 2008-04-22 12:31:02 jbobnar Exp $
37 *
38 * @since Dec 4, 2003.
39 */
40 public class LongConsumerMulticaster implements DoubleConsumer, NumberConsumer,
41 LongConsumer, StringConsumer, ObjectConsumer, PatternConsumer
42 {
43 /** Types of data consumers, that are supported by this multicaster. */
44 public static final Class[] SUPPORTED_CONSUMER_TYPES = {
45 DoubleConsumer.class, LongConsumer.class, NumberConsumer.class,
46 ObjectConsumer.class, StringConsumer.class, PatternConsumer.class
47 };
48 LongConsumer delegate;
49
50 /**
51 * If multicaster supports type, new multicaster is created, otherwise
52 * <code>null</code>is returned.
53 *
54 * @param type the requested consumer type
55 * @param delegate the consumer to which all call sare delegated
56 *
57 * @return new multicaster if type is supported, otherwise
58 * <code>null</code>
59 */
60 public static final LongConsumer createDataConsumer(Class type,
61 LongConsumer delegate)
62 {
63 if (type == LongConsumer.class) {
64 return delegate;
65 }
66
67 if (type == NumberConsumer.class || type == StringConsumer.class
68 || type == ObjectConsumer.class || type == DoubleConsumer.class || type==PatternConsumer.class) {
69 return new LongConsumerMulticaster(delegate);
70 }
71
72 return null;
73 }
74
75 /**
76 * Creates a new LongConsumerMulticaster object.
77 *
78 * @param delegate the consumer to which all call sare delegated
79 */
80 public LongConsumerMulticaster(LongConsumer delegate)
81 {
82 this.delegate = delegate;
83 }
84
85 /**
86 * Creates a new LongConsumerMulticaster object.
87 */
88 public LongConsumerMulticaster()
89 {
90 }
91
92 /**
93 * Returns the consumer to which all call sare delegated.
94 *
95 * @return the consumer to which all call sare delegated
96 */
97 public LongConsumer getDelegate()
98 {
99 return delegate;
100 }
101
102 /**
103 * Sets the consumer to which all call sare delegated.
104 *
105 * @param delegate the consumer to which all calls are delegated
106 */
107 public void setDelegate(LongConsumer delegate)
108 {
109 this.delegate = delegate;
110 }
111
112 /* (non-Javadoc)
113 * @see com.cosylab.gui.displayers.LongConsumer#updateValue(long, double)
114 */
115 public void updateValue(long timestamp, double value)
116 throws CommonException
117 {
118 delegate.updateValue(timestamp, (long)value);
119 }
120
121 /* (non-Javadoc)
122 * @see com.cosylab.gui.displayers.NumberConsumer#updateValue(long, java.lang.Number)
123 */
124 public void updateValue(long timestamp, Number value)
125 throws CommonException
126 {
127 delegate.updateValue(timestamp, value.longValue());
128 }
129
130 /* (non-Javadoc)
131 * @see com.cosylab.gui.displayers.LongConsumer#updateValue(long, long)
132 */
133 public void updateValue(long timestamp, long value)
134 throws CommonException
135 {
136 delegate.updateValue(timestamp, value);
137 }
138
139 /* (non-Javadoc)
140 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
141 */
142 public DataConsumer getDataConsumer(Class type)
143 {
144 return delegate.getDataConsumer(type);
145 }
146
147 /* (non-Javadoc)
148 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
149 */
150 public DataConsumer getDefaultDataConsumer()
151 {
152 return delegate.getDefaultDataConsumer();
153 }
154
155 /* (non-Javadoc)
156 * @see com.cosylab.gui.displayers.DataConsumer#getName()
157 */
158 public String getName()
159 {
160 return delegate.getName();
161 }
162
163 /* (non-Javadoc)
164 * @see DataConsumer#setCharacteristics(Map)
165 */
166 public void setCharacteristics(Map characteristics)
167 {
168 String f = (String)characteristics.get(CommonDisplayer.C_FORMAT);
169
170 if (f != null) {
171 HashMap map = new HashMap(characteristics);
172 map.put(CommonDisplayer.C_FORMAT, DisplayerUtilities.toLongFormat(f));
173 delegate.setCharacteristics(map);
174 } else {
175 delegate.setCharacteristics(characteristics);
176 }
177 }
178
179 /* (non-Javadoc)
180 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
181 */
182 public void updateDataState(DataState state)
183 {
184 delegate.updateDataState(state);
185 }
186
187 /* (non-Javadoc)
188 * @see com.cosylab.gui.displayers.StringConsumer#updateValue(long, java.lang.String)
189 */
190 public void updateValue(long timestamp, String value)
191 throws CommonException
192 {
193 delegate.updateValue(timestamp, Long.parseLong(value));
194 }
195
196 /* (non-Javadoc)
197 * @see com.cosylab.gui.displayers.ObjectConsumer#updateValue(long, java.lang.Object)
198 */
199 public void updateValue(long timestamp, Object value)
200 throws CommonException
201 {
202 delegate.updateValue(timestamp, ((Number)value).longValue());
203 }
204
205 /* (non-Javadoc)
206 * @see com.cosylab.gui.displayers.PatternConsumer#updateValue(long, java.util.BitSet)
207 */
208 public void updateValue(long timestamp, BitSet value)
209 throws CommonException
210 {
211 delegate.updateValue(timestamp, BitSetUtilities.toLong(value));
212 }
213
214 /* (non-Javadoc)
215 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
216 */
217 public String[] getSupportedCharacteristics()
218 {
219 return delegate.getSupportedCharacteristics();
220 }
221
222 /* (non-Javadoc)
223 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
224 */
225 public Class[] getSupportedConsumerTypes()
226 {
227 return SUPPORTED_CONSUMER_TYPES;
228 }
229 }
230
231 /* __oOo__ */