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>DoubleConsumerMulticaster</code> casts different data consumers to
29   * DoubleConsumer. 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: DoubleConsumerMulticaster.java,v 1.8 2008-04-22 12:31:02 jbobnar Exp $
34   *
35   * @since Dec 4, 2003.
36   */
37  public class DoubleConsumerMulticaster implements DoubleConsumer,
38  	NumberConsumer, LongConsumer, StringConsumer, ObjectConsumer
39  {
40  	/** Types of data consumers, that are supported by this multicaster. */
41  	public static final Class[] SUPPORTED_CONSUMER_TYPES = {
42  		DoubleConsumer.class, LongConsumer.class, NumberConsumer.class,
43  		ObjectConsumer.class, StringConsumer.class
44  	};
45  
46  	/**
47  	 * Types of data consumers, that should be prefered by DoubleConsumer
48  	 * parent.
49  	 */
50  	public static final Class[] PREFERED_CONSUMER_TYPES = {
51  		DoubleConsumer.class, LongConsumer.class, NumberConsumer.class
52  	};
53  	DoubleConsumer 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 DoubleConsumer createDataConsumer(Class type,
66  	    DoubleConsumer delegate)
67  	{
68  		if (type == DoubleConsumer.class) {
69  			return delegate;
70  		}
71  
72  		if (type == LongConsumer.class || type == NumberConsumer.class
73  		    || type == StringConsumer.class || type == ObjectConsumer.class) {
74  			return new DoubleConsumerMulticaster(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 DoubleConsumerMulticaster(DoubleConsumer delegate)
86  	{
87  		this.delegate = delegate;
88  	}
89  
90  	/**
91  	 * Creates a new DoubleConsumerMulticaster object.
92  	 */
93  	public DoubleConsumerMulticaster()
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 DoubleConsumer 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(DoubleConsumer delegate)
113 	{
114 		this.delegate = delegate;
115 	}
116 
117 	/* (non-Javadoc)
118 	 * @see com.cosylab.gui.displayers.DoubleConsumer#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.NumberConsumer#updateValue(long, java.lang.Number)
128 	 */
129 	public void updateValue(long timestamp, Number value)
130 		throws CommonException
131 	{
132 		delegate.updateValue(timestamp, value.doubleValue());
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, (double)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 		delegate.updateValue(timestamp, Double.parseDouble(value));
183 	}
184 
185 	/* (non-Javadoc)
186 	 * @see com.cosylab.gui.displayers.ObjectConsumer#updateValue(long, java.lang.Object)
187 	 */
188 	public void updateValue(long timestamp, Object value)
189 		throws CommonException
190 	{
191 		delegate.updateValue(timestamp, ((Number)value).doubleValue());
192 	}
193 
194 	/* (non-Javadoc)
195 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
196 	 */
197 	public String[] getSupportedCharacteristics()
198 	{
199 		return delegate.getSupportedCharacteristics();
200 	}
201 	
202 	/*
203 	 * (non-Javadoc)
204 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
205 	 */
206 	public <D extends DataConsumer> D getDataConsumer(Class<D> type) {
207 		return delegate.getDataConsumer(type);
208 	}
209 
210 	/*
211 	 * (non-Javadoc)
212 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
213 	 */
214 	@SuppressWarnings("unchecked")
215 	public Class<DataConsumer>[] getSupportedConsumerTypes() {
216 		return PREFERED_CONSUMER_TYPES;
217 	}
218 }
219 
220 /* __oOo__ */