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.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>PatternConsumerMulticaster</code> casts different data consumers to
32   * PatternConsumer. Delegate must be set, before any other method can be used.
33   *
34   * @author <a href="mailto:anze.zupanc@cosylab.com">Anze Zupanc</a>
35   * @version $Id: PatternConsumerMulticaster.java,v 1.7 2008-04-22 12:31:02 jbobnar Exp $
36   *
37   * @since Jul 27, 2004.
38   */
39  public class PatternConsumerMulticaster implements PatternConsumer,
40  	LongConsumer, StringConsumer, DoubleConsumer, NumberConsumer,
41  	ObjectConsumer
42  {
43  	/** Types of data consumers, that are supported by this multicaster. */
44  	public static final Class[] SUPPORTED_CONSUMER_TYPES = {
45  			PatternConsumer.class, LongConsumer.class, StringConsumer.class,
46  			DoubleConsumer.class, NumberConsumer.class, ObjectConsumer.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 PatternConsumer createDataConsumer(Class type,
60  	    PatternConsumer delegate)
61  	{
62  		if (type == PatternConsumer.class) {
63  			return delegate;
64  		}
65  
66  		if (type == NumberConsumer.class || type == StringConsumer.class
67  		    || type == ObjectConsumer.class || type == DoubleConsumer.class
68  		    || type == LongConsumer.class) {
69  			return new PatternConsumerMulticaster(delegate);
70  		}
71  
72  		return null;
73  	}
74  
75  	PatternConsumer delegate;
76  
77  	/**
78  	 * Creates a new PatternConsumerMulticaster object.
79  	 */
80  	public PatternConsumerMulticaster()
81  	{
82  		super();
83  	}
84  
85  	/**
86  	 * Creates a new PatternConsumerMulticaster object.
87  	 *
88  	 * @param delegate the consumer to which all call sare delegated
89  	 */
90  	public PatternConsumerMulticaster(PatternConsumer delegate)
91  	{
92  		this.delegate = delegate;
93  	}
94  
95  	/* (non-Javadoc)
96  	 * @see com.cosylab.gui.displayers.DataConsumer#getDefaultDataConsumer()
97  	 */
98  	public DataConsumer getDefaultDataConsumer()
99  	{
100 		return delegate.getDefaultDataConsumer();
101 	}
102 
103 	/**
104 	 * Returns the consumer to which all call sare delegated.
105 	 *
106 	 * @return the consumer to which all call sare delegated
107 	 */
108 	public PatternConsumer getDelegate()
109 	{
110 		return delegate;
111 	}
112 
113 	/* (non-Javadoc)
114 	 * @see com.cosylab.gui.displayers.DataConsumer#getName()
115 	 */
116 	public String getName()
117 	{
118 		return delegate.getName();
119 	}
120 
121 	/* (non-Javadoc)
122 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedCharacteristics()
123 	 */
124 	public String[] getSupportedCharacteristics()
125 	{
126 		return delegate.getSupportedCharacteristics();
127 	}
128 
129 	/* (non-Javadoc)
130 	 * @see com.cosylab.gui.displayers.DataConsumer#getSupportedConsumerTypes()
131 	 */
132 	@SuppressWarnings("unchecked")
133 	public Class[] getSupportedConsumerTypes()
134 	{
135 		return SUPPORTED_CONSUMER_TYPES;
136 	}
137 
138 	/* (non-Javadoc)
139 	 * @see DataConsumer#setCharacteristics(Map)
140 	 */
141 	public void setCharacteristics(Map characteristics)
142 	{
143 		String f = (String)characteristics.get(CommonDisplayer.C_FORMAT);
144 
145 		if (f != null) {
146 			HashMap map = new HashMap(characteristics);
147 			map.put(CommonDisplayer.C_FORMAT, DisplayerUtilities.toLongFormat(f));
148 			delegate.setCharacteristics(map);
149 		} else {
150 			delegate.setCharacteristics(characteristics);
151 		}
152 	}
153 
154 	/**
155 	 * Sets the consumer to which all call sare delegated.
156 	 *
157 	 * @param delegate the consumer to which all calls are delegated
158 	 */
159 	public void setDelegate(PatternConsumer delegate)
160 	{
161 		this.delegate = delegate;
162 	}
163 
164 	/* (non-Javadoc)
165 	 * @see com.cosylab.gui.displayers.DataConsumer#updateDataState(com.cosylab.gui.displayers.DataState)
166 	 */
167 	public void updateDataState(DataState state)
168 	{
169 		delegate.updateDataState(state);
170 	}
171 
172 	/* (non-Javadoc)
173 	 * @see com.cosylab.gui.displayers.PatternConsumer#updateValue(long, java.util.BitSet)
174 	 */
175 	public void updateValue(long timestamp, BitSet value)
176 		throws CommonException
177 	{
178 		delegate.updateValue(timestamp, value);
179 	}
180 
181 	/* (non-Javadoc)
182 	 * @see com.cosylab.gui.displayers.DoubleConsumer#updateValue(long, double)
183 	 */
184 	public void updateValue(long timestamp, double value)
185 		throws CommonException
186 	{
187 		delegate.updateValue(timestamp, BitSetUtilities.forLong((long)value));
188 	}
189 
190 	/* (non-Javadoc)
191 	 * @see com.cosylab.gui.displayers.LongConsumer#updateValue(long, long)
192 	 */
193 	public void updateValue(long timestamp, long value)
194 		throws CommonException
195 	{
196 		delegate.updateValue(timestamp, BitSetUtilities.forLong(value));
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see com.cosylab.gui.displayers.NumberConsumer#updateValue(long, java.lang.Number)
201 	 */
202 	public void updateValue(long timestamp, Number value)
203 		throws CommonException
204 	{
205 		delegate.updateValue(timestamp,
206 		    BitSetUtilities.forLong(value.longValue()));
207 	}
208 
209 	/* (non-Javadoc)
210 	 * @see com.cosylab.gui.displayers.ObjectConsumer#updateValue(long, java.lang.Object)
211 	 */
212 	public void updateValue(long timestamp, Object value)
213 		throws CommonException
214 	{
215 		delegate.updateValue(timestamp,
216 		    BitSetUtilities.forLong(((Number)value).longValue()));
217 	}
218 
219 	/* (non-Javadoc)
220 	 * @see com.cosylab.gui.displayers.StringConsumer#updateValue(long, java.lang.String)
221 	 */
222 	public void updateValue(long timestamp, String value)
223 		throws CommonException
224 	{
225 		delegate.updateValue(timestamp,
226 		    BitSetUtilities.forLong(Long.parseLong(value)));
227 	}
228 
229 	/*
230 	 * (non-Javadoc)
231 	 * @see com.cosylab.gui.displayers.DataConsumer#getDataConsumer(java.lang.Class)
232 	 */
233 	public <D extends DataConsumer> D getDataConsumer(Class<D> type) {
234 		return delegate.getDataConsumer(type);
235 	}
236 }
237 
238 /* __oOo__ */