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.adapters;
21
22 import com.cosylab.gui.displayers.DataConsumer;
23 import com.cosylab.gui.displayers.DoubleSeqConsumer;
24 import com.cosylab.util.CommonException;
25
26
27 /**
28 * <code>SimpleConverterSupport</code> is implementation of
29 * <code>Converter</code> interface intended for simple implementation, which
30 * use transfromation function on double data flow.
31 *
32 * <p>
33 * <b>Note!</b> This implementation works only with data sources and consumers,
34 * whcih support <code>double[]</code> as value type.
35 * </p>
36 *
37 * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
38 * @version $Id: SimpleArrayConverterSupport.java,v 1.4 2008-04-22 12:31:02 jbobnar Exp $
39 *
40 * @see com.cosylab.gui.adapters.Converter
41 * @since Feb 14, 2004.
42 */
43 public abstract class SimpleArrayConverterSupport extends AbstractConverter
44 implements Converter, DoubleSeqConsumer
45 {
46
47 /**
48 * Creates new converter.
49 */
50 public SimpleArrayConverterSupport()
51 {
52 // we dalagate to super suppport class only the consumer type/types we want to suppport
53 super(new Class[]{ DoubleSeqConsumer.class });
54
55 // the name of this converter when it acts as a consumer
56 name = "SimpleConverter";
57 }
58
59 /**
60 * This is called by peer data source. Call is delegated to contained
61 * consumers.
62 *
63 * @see com.cosylab.gui.displayers.DoubleConsumer#updateValue(long, double)
64 */
65 public void updateValue(long timestamp, double[] value)
66 throws CommonException
67 {
68
69 DataConsumer[] delegates = getConsumers();
70
71 // transform value
72 final double[] newValue = transform(value);
73
74 for (int i = 0; i < delegates.length; i++) {
75 try {
76 ((DoubleSeqConsumer)delegates[i]).updateValue(timestamp, newValue);
77 } catch (Exception e) {
78 e.printStackTrace();
79 }
80 }
81 }
82
83 /**
84 * User must implement this method and from prvided parameter calculate
85 * with own transfromatin function new value and return it.
86 *
87 * @param value the value to be transfirmed
88 *
89 * @return the transformed value
90 */
91 abstract protected double[] transform(double[] value);
92
93 }
94
95 /* __oOo__ */