KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jrobin.graph;
2
3 import java.util.HashMap JavaDoc;
4
5 import org.jrobin.core.RrdException;
6 import org.jrobin.core.XmlWriter;
7
8 /**
9  * <p>Represents a 'static' datasource for a graph. A static datasource is a single value (constant),
10  * but can only be the result of applying a consolidation function (AVG, MIN, MAX, LAST, FIRST or TOTAL)
11  * to one of the other, already defined, datasources.</p>
12  *
13  * @author Arne Vandamme (cobralord@jrobin.org)
14  */

15 class Sdef extends Cdef
16 {
17     // ================================================================
18
// -- Members
19
// ================================================================
20
private int defIndex = -1;
21
22     private String JavaDoc defName = "";
23     private String JavaDoc consolFunc = "AVERAGE";
24     private int aggregate = Source.AGG_AVERAGE;
25
26     private boolean calculated = false;
27     private double[] value = null;
28
29
30     // ================================================================
31
// -- Constructors
32
// ================================================================
33
/**
34      * Constructs a new Sdef object based on an existing datasource (Def, Cdef or Pdef)
35      * and the consolidation function to apply to that datasource.
36      *
37      * @param name Name of the datasource in the graph definition.
38      * @param defName Name of the datasource this Sdef is derived from.
39      * @param consolFunc Consolidation function to apply to the referring datasource (defName).
40      * @throws RrdException Thrown in case of invalid consolidation function specified.
41      */

42     Sdef( String JavaDoc name, String JavaDoc defName, String JavaDoc consolFunc ) throws RrdException
43     {
44         super( name );
45
46         this.defName = defName;
47         this.consolFunc = consolFunc;
48
49         // -- Parse the consolidation function to be used
50
if ( consolFunc.equalsIgnoreCase("AVERAGE") || consolFunc.equalsIgnoreCase("AVG") )
51             aggregate = Source.AGG_AVERAGE;
52         else if ( consolFunc.equalsIgnoreCase("MAX") || consolFunc.equalsIgnoreCase("MAXIMUM") )
53             aggregate = Source.AGG_MAXIMUM;
54         else if ( consolFunc.equalsIgnoreCase("MIN") || consolFunc.equalsIgnoreCase("MINIMUM") )
55             aggregate = Source.AGG_MINIMUM;
56         else if ( consolFunc.equalsIgnoreCase("LAST") )
57             aggregate = Source.AGG_LAST;
58         else if ( consolFunc.equalsIgnoreCase("FIRST") )
59             aggregate = Source.AGG_FIRST;
60         else if ( consolFunc.equalsIgnoreCase("TOTAL") )
61             aggregate = Source.AGG_TOTAL;
62         else
63             throw new RrdException( "Invalid consolidation function specified." );
64     }
65
66     // ================================================================
67
// -- Protected methods
68
// ================================================================
69
/**
70      * Prepares the Sdef for faster value calculation by setting the internal
71      * array and references. Override from Cdef parent class.
72      *
73      * @param sourceIndex Lookup table holding the name - index pairs for all datasources.
74      * @param numPoints Number of points used as graph resolution (size of the value table).
75      * @throws RrdException Thrown in case of the requested datasource is not available in the sourceIndex
76      */

77     void prepare( HashMap JavaDoc sourceIndex, int numPoints, int aggregatePoints ) throws RrdException
78     {
79         if ( sourceIndex.containsKey( defName ) )
80             defIndex = ( (Integer JavaDoc) sourceIndex.get( defName ) ).intValue();
81         else
82             throw new RrdException( "Datasource not found: " + defName );
83
84         values = new double[ numPoints ];
85         this.aggregatePoints = aggregatePoints;
86         calculated = false;
87     }
88
89     /**
90      * Returns the level this Sdef would have in the calculation tree. The level defines when
91      * the Sdef can be calculated. The level of the Sdef will always be one higher than that of
92      * its referring datasource, since it can only be calculated after that datasource has already
93      * been calculated.
94      *
95      * @param levels Array containing the previously calculated calculation levels.
96      * @return Level of this Sdef in the calculation tree.
97      */

98     int calculateLevel( int[] levels )
99     {
100         // An Sdef is always one lower in the tree than its source
101
return levels[defIndex] + 1;
102     }
103
104     /**
105      * Gets the value of the datapoint at a particular position in the datapoint array.
106      * In case of an Sdef, the value will be the same for every different position.
107      *
108      * @param pos Inherited from Cdef
109      * @return The consolidated value of the referring datasource as double
110      */

111     double get( int pos )
112     {
113         return values[0];
114     }
115
116     /**
117      * Calculates the internal value of this Sdef.
118      *
119      * @param sources Array of the calculated datasources.
120      */

121     void set( Source[] sources )
122     {
123         if ( calculated ) return;
124
125         double value = sources[ defIndex ].getAggregate( aggregate );
126         for ( int i = 0; i < values.length; i++ )
127             values[i] = value;
128
129         calculated = true;
130     }
131
132     /**
133      * Override from the corresponding method in the Source parent class,
134      * overridden for faster implementation.
135      *
136      * Five of the possible aggregation methods return the actual value
137      * of the Sdef:
138      * <code>AGG_MINIMUM, AGG_MAXIMUM, AGG_AVERAGE, AGG_FIRST and AGG_LAST</code>
139      *
140      * Only <code>AGG_TOTAL</code> will return value summed over the number of
141      * samples in the graph.
142      *
143      * @param aggType Type of the aggregate requested.
144      * @return The double value of the requested aggregate.
145      */

146     double getAggregate( int aggType )
147     {
148         switch ( aggType )
149         {
150             case AGG_MINIMUM:
151             case AGG_MAXIMUM:
152             case AGG_AVERAGE:
153             case AGG_FIRST:
154             case AGG_LAST:
155                 return values[0];
156
157             case AGG_TOTAL:
158                 return (values[0] * values.length) ;
159         }
160
161         return Double.NaN;
162     }
163
164
165     void exportXml(XmlWriter xml) {
166         xml.startTag( "def" );
167         xml.writeTag( "name", getName() );
168         xml.writeTag( "datasource", defName );
169         xml.writeTag( "cf", consolFunc );
170         xml.closeTag(); // def
171
}
172 }
173
Popular Tags