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>DoubleSeqConsumerMulticaster</code> casts different data consumers to
29   * DoubleSeqConsumer. ObjectSeqConsumer 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: DoubleSeqConsumerMulticaster.java,v 1.5 2008-04-22 12:31:02 jbobnar Exp $
34   *
35   * @since Dec 4, 2003.
36   */
37  public class DoubleSeqConsumerMulticaster implements DoubleSeqConsumer,
38  	LongSeqConsumer, StringSeqConsumer, ObjectSeqConsumer
39  {
40  	/** Types of data consumers, that are supported by this multicaster. */
41  	public static final Class[] SUPPORTED_CONSUMER_TYPES = {
42  		DoubleSeqConsumer.class, LongSeqConsumer.class, ObjectSeqConsumer.class,
43  		StringSeqConsumer.class
44  	};
45  
46  	/**
47  	 * Types of data consumers, that should be prefered by DoubleSeqConsumer
48  	 * parent.
49  	 */
50  	public static final Class[] PREFERED_CONSUMER_TYPES = {
51  		DoubleSeqConsumer.class, LongSeqConsumer.class
52  	};
53  	DoubleSeqConsumer delegate;
54  
55  	/**
56  	 * If multicaster supports type, new multicaster is created, otherwise
57  	 * <code>null</code>is returned.
58  	 *
59  	 * @param type the requested consumer type
60  	 * @param delegate the consumer to which all call sare delegated
61  	 *
62  	 * @return new multicaster if type is supported, otherwise
63  	 *         <code>null</code>
64  	 */
65  	public static final DoubleSeqConsumer createDataConsumer(Class type,
66  	    DoubleSeqConsumer delegate)
67  	{
68  		if (type == DoubleSeqConsumer.class) {
69  			return delegate;
70  		}
71  
72  		if (type == LongSeqConsumer.class || type == StringSeqConsumer.class
73  		    || type == ObjectSeqConsumer.class) {
74  			return new DoubleSeqConsumerMulticaster(delegate);
75  		}
76  
77  		return null;
78  	}
79  
80  	/**
81  	 * Creates a new DoubleConsumerMulticaster object.
82  	 *
83  	 * @param delegate the consumer to which all call sare delegated
84  	 */
85  	public DoubleSeqConsumerMulticaster(DoubleSeqConsumer delegate)
86  	{
87  		this.delegate = delegate;
88  	}
89  
90  	/**
91  	 * Creates a new DoubleConsumerMulticaster object.
92  	 */
93  	public DoubleSeqConsumerMulticaster()
94  	{
95  	}
96  
97  	/**
98  	 * Returns the consumer to which all call sare delegated.
99  	 *
100 	 * @return the consumer to which all call sare delegated
101 	 */
102 	public DoubleSeqConsumer getDelegate()
103 	{
104 		return delegate;
105 	}
106 
107 	/**
108 	 * Sets the consumer to which all call sare delegated.
109 	 *
110 	 * @param delegate the consumer to which all calls are delegated
111 	 */
112 	public void setDelegate(DoubleSeqConsumer delegate)
113 	{
114 		this.delegate = delegate;
115 	}
116 
117 	/* (non-Javadoc)
118 	 * @see com.cosylab.gui.displayers.DoubleSeqConsumer#updateValue(long, double)
119 	 */
120 	public void updateValue(long timestamp, double[] value)
121 		throws CommonException
122 	{
123 		delegate.updateValue(timestamp, value);
124 	}
125 
126 	/* (non-Javadoc)
127 	 * @see com.cosylab.gui.displayers.LongSeqConsumer#updateValue(long, long)
128 	 */
129 	public void updateValue(long timestamp, long[] value)
130 		throws CommonException
131 	{
132 		double[] d = new double[value.length];
133 
134 		for (int i = 0; i < d.length; i++) {
135 			d[i] = value[i];
136 		}
137 
138 		delegate.updateValue(timestamp, d);
139 	}
140 
141 	/* (non-Javadoc)
142 	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
143 	 */
144 	public DataConsumer getDefaultDataConsumer()
145 	{
146 		return delegate.getDefaultDataConsumer();
147 	}
148 
149 	/* (non-Javadoc)
150 	 * @see com.cosylab.gui.displayers.DataConsumer#getName()
151 	 */
152 	public String getName()
153 	{
154 		return delegate.getName();
155 	}
156 
157 	/* (non-Javadoc)
158 	 * @see DataConsumer#setCharacteristics(Map)
159 	 */
160 	public void setCharacteristics(Map characteristics)
161 	{
162 		delegate.setCharacteristics(characteristics);
163 	}
164 
165 	/* (non-Javadoc)
166 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
167 	 */
168 	public void updateDataState(DataState state)
169 	{
170 		delegate.updateDataState(state);
171 	}
172 
173 	/* (non-Javadoc)
174 	 * @see com.cosylab.gui.displayers.StringSeqConsumer#updateValue(long, java.lang.String)
175 	 */
176 	public void updateValue(long timestamp, String[] value)
177 		throws CommonException
178 	{
179 		double[] d = new double[value.length];
180 
181 		for (int i = 0; i < d.length; i++) {
182 			d[i] = Double.parseDouble(value[i]);
183 		}
184 
185 		delegate.updateValue(timestamp, d);
186 	}
187 
188 	/* (non-Javadoc)
189 	 * @see com.cosylab.gui.displayers.ObjectSeqConsumer#updateValue(long, java.lang.Object)
190 	 */
191 	public void updateValue(long timestamp, Object[] value)
192 		throws CommonException
193 	{
194 		double[] d = new double[value.length];
195 
196 		for (int i = 0; i < d.length; i++) {
197 			d[i] = ((Number)value[i]).doubleValue();
198 		}
199 
200 		delegate.updateValue(timestamp, d);
201 	}
202 
203 	/* (non-Javadoc)
204 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
205 	 */
206 	public String[] getSupportedCharacteristics()
207 	{
208 		return delegate.getSupportedCharacteristics();
209 	}
210 
211 	/* (non-Javadoc)
212 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
213 	 */
214 	@SuppressWarnings("unchecked")
215 	public Class[] getSupportedConsumerTypes()
216 	{
217 		return PREFERED_CONSUMER_TYPES;
218 	}
219 
220 	/*
221 	 * (non-Javadoc)
222 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
223 	 */
224 	public <D extends DataConsumer> D getDataConsumer(Class<D> type) {
225 		return delegate.getDataConsumer(type);
226 	}
227 }
228 
229 /* __oOo__ */