View Javadoc

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.Map;
23  
24  import com.cosylab.util.CommonException;
25  
26  
27  /**
28   * <code>StringConsumerMulticaster</code> casts different data consumers to
29   * StringConsumer. ObjectConsumer cast must receive <code>Number</code>
30   * instances. Delegate must be set, before any other method can be used.
31   *
32   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
33   * @version $Id: StringConsumerMulticaster.java,v 1.6 2008-04-22 12:31:02 jbobnar Exp $
34   *
35   * @since Dec 4, 2003.
36   */
37  public class StringConsumerMulticaster implements DoubleConsumer,
38  	NumberConsumer, LongConsumer, StringConsumer, ObjectConsumer, 
39  	DoubleSeqConsumer, LongSeqConsumer, StringSeqConsumer, ObjectSeqConsumer
40  {
41  	StringConsumer delegate;
42  
43  	/** Types of data consumers, that are supported by this multicaster. */
44  	public static final Class[] SUPPORTED_CONSUMER_TYPES = {
45  		LongConsumer.class, NumberConsumer.class, DoubleConsumer.class,
46  		ObjectConsumer.class, StringConsumer.class
47  	};
48  
49  	/**
50  	 * If multicaster supports type, new multicaster is created, otherwise
51  	 * <code>null</code>is returned.
52  	 *
53  	 * @param type the requested consumer type
54  	 * @param delegate the consumer to which all call sare delegated
55  	 *
56  	 * @return new multicaster if type is supported, otherwise
57  	 *         <code>null</code>
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  	 * Creates a new StringConsumerMulticaster object.
78  	 *
79  	 * @param delegate the consumer to which all call sare delegated
80  	 */
81  	public StringConsumerMulticaster(StringConsumer delegate)
82  	{
83  		this.delegate = delegate;
84  	}
85  
86  	/**
87  	 * Creates a new StringConsumerMulticaster object.
88  	 */
89  	public StringConsumerMulticaster()
90  	{
91  	}
92  
93  	/**
94  	 * Returns the consumer to which all call sare delegated.
95  	 *
96  	 * @return the consumer to which all call sare delegated
97  	 */
98  	public StringConsumer getDelegate()
99  	{
100 		return delegate;
101 	}
102 
103 	/**
104 	 * Sets the consumer to which all call sare delegated.
105 	 *
106 	 * @param delegate the consumer to which all calls are delegated
107 	 */
108 	public void setDelegate(StringConsumer delegate)
109 	{
110 		this.delegate = delegate;
111 	}
112 
113 	/* (non-Javadoc)
114 	 * @see com.cosylab.gui.displayers.StringConsumer#updateValue(long, double)
115 	 */
116 	public void updateValue(long timestamp, double value)
117 		throws CommonException
118 	{
119 		delegate.updateValue(timestamp, Double.toString(value));
120 	}
121 
122 	/* (non-Javadoc)
123 	 * @see com.cosylab.gui.displayers.NumberConsumer#updateValue(long, java.lang.Number)
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 	/* (non-Javadoc)
136 	 * @see com.cosylab.gui.displayers.LongConsumer#updateValue(long, long)
137 	 */
138 	public void updateValue(long timestamp, long value)
139 		throws CommonException
140 	{
141 		delegate.updateValue(timestamp, Long.toString(value));
142 	}
143 
144 	/* (non-Javadoc)
145 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
146 	 */
147 	public DataConsumer getDefaultDataConsumer()
148 	{
149 		return delegate.getDefaultDataConsumer();
150 	}
151 
152 	/* (non-Javadoc)
153 	 * @see com.cosylab.gui.displayers.DataConsumer#getName()
154 	 */
155 	public String getName()
156 	{
157 		return delegate.getName();
158 	}
159 
160 	/* (non-Javadoc)
161 	 * @see DataConsumer#setCharacteristics(Map)
162 	 */
163 	public void setCharacteristics(Map characteristics)
164 	{
165 		delegate.setCharacteristics(characteristics);
166 	}
167 
168 	/* (non-Javadoc)
169 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
170 	 */
171 	public void updateDataState(DataState state)
172 	{
173 		delegate.updateDataState(state);
174 	}
175 
176 	/* (non-Javadoc)
177 	 * @see com.cosylab.gui.displayers.StringConsumer#updateValue(long, java.lang.String)
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 	/* (non-Javadoc)
190 	 * @see com.cosylab.gui.displayers.ObjectConsumer#updateValue(long, java.lang.Object)
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 	/* (non-Javadoc)
203 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
204 	 */
205 	public String[] getSupportedCharacteristics()
206 	{
207 		return delegate.getSupportedCharacteristics();
208 	}
209 
210 	/* (non-Javadoc)
211 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
212 	 */
213 	@SuppressWarnings("unchecked")
214 	public Class[] getSupportedConsumerTypes()
215 	{
216 		return SUPPORTED_CONSUMER_TYPES;
217 	}
218 	
219 	/* (non-Javadoc)
220 	 * @see com.cosylab.gui.displayers.ObjectSeqConsumer#updateValue(long, java.lang.Object[])
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 	/* (non-Javadoc)
230 	 * @see com.cosylab.gui.displayers.DoubleSeqConsumer#updateValue(long, double[])
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 	/* (non-Javadoc)
250 	 * @see com.cosylab.gui.displayers.LongSeqConsumer#updateValue(long, long[])
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 	/* (non-Javadoc)
270 	 * @see com.cosylab.gui.displayers.StringSeqConsumer#updateValue(long, java.lang.String[])
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 	 * (non-Javadoc)
282 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
283 	 */
284 	public <D extends DataConsumer> D getDataConsumer(Class<D> type) {
285 		return delegate.getDataConsumer(type);
286 	}
287 }
288 
289 /* __oOo__ */