KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > graph > Pdef


1 /* ============================================================
2  * JRobin : Pure java implementation of RRDTool's functionality
3  * ============================================================
4  *
5  * Project Info: http://www.jrobin.org
6  * Project Lead: Sasa Markovic (saxon@jrobin.org)
7  *
8  * Developers: Sasa Markovic (saxon@jrobin.org)
9  * Arne Vandamme (cobralord@jrobin.org)
10  *
11  * (C) Copyright 2003, by Sasa Markovic.
12  *
13  * This library is free software; you can redistribute it and/or modify it under the terms
14  * of the GNU Lesser General Public License as published by the Free Software Foundation;
15  * either version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  * See the GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License along with this
22  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */

25 package org.jrobin.graph;
26
27 /**
28  * <p>Plottable Def, reprents a custom datasource that can be graphed by JRobin.
29  * All the Pdef needs, is a reference to a Plottable class, and it will get the datapoint values (based
30  * on timestamps) from that external class. Any class extending the public Plottable class will do,
31  * meaning that the class could get its values from ANY source... like a RDBMS for example.
32  * </p>
33  *
34  * @author Arne Vandamme <cobralord@cherrymon.org>
35  */

36 class Pdef extends Source
37 {
38     // ================================================================
39
// -- Members
40
// ================================================================
41
private Plottable plottable;
42     
43     private int index = 0;
44     private String JavaDoc sourceName = null;
45     private boolean indexed = false;
46     private boolean named = false;
47
48
49     // ================================================================
50
// -- Constructors
51
// ================================================================
52
/**
53      * Constructs a new Plottable Def: a custom external datasource
54      * (represented as a Plottable class) that can be graphed by JRobin.
55      *
56      * @param name Name of the datasource in the graph definition.
57      * @param plottable Reference to the class extending Plottable and providing the datapoints.
58      */

59     Pdef( String JavaDoc name, Plottable plottable )
60     {
61         super(name );
62         this.plottable = plottable;
63     }
64     
65     /**
66      * Constructs a new Plottable Def: a custom external datasource
67      * (represented as a Plottable class) that can be graphed by JRobin.
68      *
69      * @param name Name of the datasource in the graph definition.
70      * @param plottable Reference to the class extending Plottable and providing the datapoints.
71      * @param index Integer number used for referring to the series of datapoints to use in the Plottable class.
72      */

73     Pdef( String JavaDoc name, Plottable plottable, int index )
74     {
75         super(name );
76         this.plottable = plottable;
77         this.index = index;
78         indexed = true;
79     }
80     
81     /**
82      * Constructs a new Plottable Def: a custom external datasource
83      * (represented as a Plottable class) that can be graphed by JRobin.
84      *
85      * @param name Name of the datasource in the graph definition.
86      * @param plottable Reference to the class extending Plottable and providing the datapoints.
87      * @param sourceName String used for referring to the series of datapoints to use in the Plottable class.
88      */

89     Pdef( String JavaDoc name, Plottable plottable, String JavaDoc sourceName)
90     {
91         super(name );
92         this.plottable = plottable;
93         this.sourceName = sourceName;
94         named = true;
95     }
96
97
98     // ================================================================
99
// -- Protected methods
100
// ================================================================
101
/**
102      * Prepares the array that will hold the values.
103      *
104      * @param numPoints Number of datapoints that will be used.
105      */

106     void prepare( int numPoints, int aggregatePoints )
107     {
108         // Create values table of correct size
109
values = new double[numPoints];
110
111         // Set the number of points that should be used for aggregate calculation
112
this.aggregatePoints = aggregatePoints;
113     }
114     
115     /**
116      * Sets the value of a specific datapoint for this Pdef. The Pdef gets the datapoint by retrieving
117      * the value from the Plottable interface using an appropriate getValue() method.
118      *
119      * @param pos Position (index in the value table) of the new datapoint.
120      * @param timestamp Timestamp of the new datapoint in number of seconds.
121      */

122     void set( int pos, long timestamp )
123     {
124         double val = Double.NaN;
125
126         /**
127          * With the new calculation algorithm we expect the value for a period to be defined
128          * by the first timestamp AFTER that period. The implementation of Pdef is different,
129          * and works with 'point' value instead of period. Converted to period this would mean
130          * that the period is defined by the starting timestamp, or another timestamp IN that
131          * period. As a result, we shift the requested timestamp one step back in time.
132          */

133         if ( indexed )
134             val = plottable.getValue( timestamp - step, index );
135         else if ( named )
136             val = plottable.getValue( timestamp - step, sourceName );
137         else
138             val = plottable.getValue( timestamp - step );
139
140         super.set( pos, timestamp, val );
141         
142         values[pos] = val;
143     }
144 }
145
Popular Tags