KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.jrobin.core.Util;
28
29 /**
30  * <p>Class used to represent a number of datapoints for a graph.</p>
31  *
32  * @author Arne Vandamme (cobralord@jrobin.org)
33  */

34 class Source
35 {
36     // ================================================================
37
// -- Members
38
// ================================================================
39
protected static final int AGG_MINIMUM = 0;
40     protected static final int AGG_MAXIMUM = 1;
41     protected static final int AGG_AVERAGE = 2;
42     protected static final int AGG_FIRST = 3;
43     protected static final int AGG_LAST = 4;
44     protected static final int AGG_TOTAL = 5;
45     
46     protected static final String JavaDoc[] aggregates = { "MINIMUM", "MAXIMUM", "AVERAGE", "FIRST", "LAST", "TOTAL" };
47     private String JavaDoc name;
48
49     protected int aggregatePoints;
50     protected double[] values;
51     
52     private double min = Double.NaN;
53     private double max = Double.NaN;
54     private double lastValue = Double.NaN;
55     private double totalValue = 0;
56     private double nextValue = Double.POSITIVE_INFINITY;
57
58     protected long step = 0;
59     private long lastPreciseTime = 0; // Last time requested
60
private long lastTime = 0;
61     private long totalTime = 0;
62
63     private int stPos = 0;
64     private int lastStPos = 0; // Last value position requested
65

66     // ================================================================
67
// -- Constructors
68
// ================================================================
69
/**
70      * Constructs a new Source object holding a number of datapoints for a graph.
71      * @param name Name of the datasource in the graph definition.
72      */

73     Source( String JavaDoc name )
74     {
75         this.name = name;
76     }
77     
78     
79     // ================================================================
80
// -- Protected methods
81
// ================================================================
82
/**
83      * Stub method, sets the value of a specific datapoint.
84      * Forces this point to be used in aggregate function calculating.
85      * @param pos Position (index in the value table) of the new datapoint.
86      * @param time Timestamp of the new datapoint in number of seconds.
87      * @param val Double value of the new datapoint.
88      */

89     void set( int pos, long time, double val )
90     {
91         // The first sample is before the time range we want, and as such
92
// should not be counted for data aggregation
93
if ( pos > 0 && pos < aggregatePoints )
94             aggregate( time, val );
95     }
96
97     void setFetchedStep( long step ) {
98         this.step = step;
99     }
100
101     long getStep() {
102         return step;
103     }
104
105     /**
106      * Get the double value of a datapoint.
107      *
108      * @param pos Index in the value table of the datapoint.
109      * @return The double value of the requested datapoint.
110      */

111     double get( int pos )
112     {
113         if ( pos < 0 )
114             return Double.NaN;
115         if ( pos > values.length )
116             return Double.NaN;
117
118         double val = values[pos];
119
120         if ( Double.isInfinite(val) )
121         {
122             // Return the next value if we fetched it before
123
if ( !Double.isInfinite(nextValue) && pos >= lastStPos )
124                 return nextValue;
125
126             lastStPos = pos;
127             
128             // Try to fetch the next value
129
for ( int i = pos + 1; i < values.length; i++ )
130             {
131                 if ( !Double.isInfinite(values[i]) )
132                 {
133                     nextValue = values[i];
134
135                     return nextValue;
136                 }
137             }
138
139             // No more next value
140
nextValue = Double.NaN;
141
142             return nextValue;
143         }
144         else
145             nextValue = Double.POSITIVE_INFINITY;
146
147         lastStPos = pos;
148
149         return values[pos];
150     }
151
152     double get( long preciseTime, long[] reducedTimestamps )
153     {
154         long t = Util.normalize( preciseTime, step );
155         t = ( t < preciseTime ? t + step : t );
156
157         if ( preciseTime < lastPreciseTime ) // Backward fetching is weird, start over, we prolly in a new iteration
158
stPos = 0;
159
160         lastPreciseTime = preciseTime;
161
162         while ( stPos < reducedTimestamps.length - 1 )
163         {
164             if ( reducedTimestamps[ stPos + 1 ] <= t )
165                 stPos++;
166             else
167                 return get( stPos );
168         }
169
170         if ( t <= reducedTimestamps[stPos] )
171             return get( stPos );
172
173         return Double.NaN;
174     }
175
176     /**
177      * Gets a specific aggregate of this datasource.
178      * Requested aggregate can be one of the following:
179      * <code>AGG_MINIMUM, AGG_MAXIMUM, AGG_AVERAGE, AGG_FIRST, AGG_TOTAL</code>
180      * and <code>AGG_LAST</code>.
181      * @param aggType Type of the aggregate requested.
182      * @return The double value of the requested aggregate.
183      */

184     double getAggregate( int aggType )
185     {
186         switch ( aggType )
187         {
188             case AGG_MINIMUM:
189                 return min;
190                 
191             case AGG_MAXIMUM:
192                 return max;
193                 
194             case AGG_AVERAGE:
195                 if ( totalTime > 0 )
196                     return totalValue / totalTime;
197                 break;
198                 
199             case AGG_FIRST:
200                 if ( values != null && values.length > 0)
201                     return values[0];
202                 break;
203                 
204             case AGG_LAST:
205                 if ( values != null && values.length > 0)
206                     return values[values.length - 1];
207                 break;
208                 
209             case AGG_TOTAL:
210                 return totalValue;
211         }
212         
213         return Double.NaN;
214     }
215     
216     String JavaDoc getName() {
217         return name;
218     }
219     
220     double[] getValues() {
221         return values;
222     }
223     
224     long getSampleCount() {
225         return ( values != null ? values.length : 0 );
226     }
227     
228     
229     // ================================================================
230
// -- Private methods
231
// ================================================================
232
/**
233      * Adds a datapoint to the aggregate function calculation.
234      * @param time Timestamp in seconds of the datapoint.
235      * @param value Double value of the datapoint.
236      */

237     private void aggregate( long time, double value )
238     {
239         if ( Double.isInfinite(value) )
240             return;
241
242         min = Util.min( min, value );
243         max = Util.max( max, value );
244         
245         if ( !Double.isNaN(lastValue) && !Double.isNaN(value) )
246         {
247             long timeDelta = time - lastTime;
248
249             totalValue += timeDelta * ( value + lastValue ) / 2.0;
250             totalTime += timeDelta;
251         }
252
253         lastTime = time;
254         lastValue = value;
255     }
256 }
257
Popular Tags