KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > ArcDef


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  * (C) Copyright 2003, by Sasa Markovic.
9  *
10  * Developers: Sasa Markovic (saxon@jrobin.org)
11  * Arne Vandamme (cobralord@jrobin.org)
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
26 package org.jrobin.core;
27
28 /**
29  * Class to represent single archive definition within the RRD.
30  * Archive definition consists of the following four elements:
31  *
32  * <ul>
33  * <li>consolidation function
34  * <li>X-files factor
35  * <li>number of steps
36  * <li>number of rows.
37  * </ul>
38  * <p>For the complete explanation of all archive definition parameters, see RRDTool's
39  * <a HREF="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a>
40  * </p>
41  *
42  * @author <a HREF="mailto:saxon@jrobin.org">Sasa Markovic</a>
43  */

44
45 public class ArcDef {
46     /** array of valid consolidation function names */
47     public static final String JavaDoc CONSOL_FUNS[] = { "AVERAGE", "MAX", "MIN", "LAST" };
48
49     private String JavaDoc consolFun;
50     private double xff;
51     private int steps, rows;
52
53     /**
54      * <p>Creates new archive definition object. This object should be passed as argument to
55      * {@link org.jrobin.core.RrdDef#addArchive(org.jrobin.core.ArcDef) addArchive()} method of
56      * {@link org.jrobin.core.RrdDb RrdDb} object.</p>
57      *
58      * <p>For the complete explanation of all archive definition parameters, see RRDTool's
59      * <a HREF="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a></p>
60      *
61      * @param consolFun Consolidation function. Allowed values are "AVERAGE", "MIN",
62      * "MAX" and "LAST".
63      * @param xff X-files factor, between 0 and 1.
64      * @param steps Number of archive steps.
65      * @param rows Number of archive rows.
66      * @throws RrdException Thrown if any parameter has illegal value.
67      */

68     public ArcDef(String JavaDoc consolFun, double xff, int steps, int rows) throws RrdException {
69         this.consolFun = consolFun;
70         this.xff = xff;
71         this.steps = steps;
72         this.rows = rows;
73         validate();
74     }
75
76     /**
77      * Returns consolidation function.
78      * @return Consolidation function.
79      */

80     public String JavaDoc getConsolFun() {
81         return consolFun;
82     }
83
84     /**
85      * Returns the X-files factor.
86      * @return X-files factor value.
87      */

88     public double getXff() {
89         return xff;
90     }
91
92     /**
93      * Returns the number of primary RRD steps which complete a single archive step.
94      * @return Number of steps.
95      */

96     public int getSteps() {
97         return steps;
98     }
99
100     /**
101      * Returns the number of rows (aggregated values) stored in the archive.
102      * @return Number of rows.
103      */

104     public int getRows() {
105         return rows;
106     }
107
108     private void validate() throws RrdException {
109         if(!isValidConsolFun(consolFun)) {
110             throw new RrdException("Invalid consolidation function specified: " + consolFun);
111         }
112         if(Double.isNaN(xff) || xff < 0.0 || xff >= 1.0) {
113             throw new RrdException("Invalid xff, must be >= 0 and < 1: " + xff);
114         }
115         if(steps <= 0 || rows <= 0) {
116             throw new RrdException("Invalid steps/rows number: " + steps + "/" + rows);
117         }
118     }
119
120     /**
121      * Returns string representing archive definition (RRDTool format).
122      * @return String containing all archive definition parameters.
123      */

124     public String JavaDoc dump() {
125         return "RRA:" + consolFun + ":" + xff + ":" + steps + ":" + rows;
126     }
127
128     /**
129      * Checks if two archive definitions are equal.
130      * Archive definitions are considered equal if they have the same number of steps
131      * and the same consolidation function. It is not possible to create RRD with two
132      * equal archive definitions.
133      * @param obj Archive definition to compare with.
134      * @return <code>true</code> if archive definitions are equal,
135      * <code>false</code> otherwise.
136      */

137     public boolean equals(Object JavaDoc obj) {
138         if(obj instanceof ArcDef) {
139             ArcDef arcObj = (ArcDef) obj;
140             return consolFun.equals(arcObj.consolFun) && steps == arcObj.steps;
141         }
142         return false;
143     }
144
145     /**
146      * Checks if function argument represents valid consolidation function name.
147      * @param consolFun Consolidation function to be checked
148      * @return <code>true</code> if <code>consolFun</code> is valid consolidation function,
149      * <code>false</code> otherwise.
150      */

151     public static boolean isValidConsolFun(String JavaDoc consolFun) {
152         for(int i = 0; i < CONSOL_FUNS.length; i++) {
153             if(CONSOL_FUNS[i].equals(consolFun)) {
154                 return true;
155             }
156         }
157         return false;
158     }
159
160     void setRows(int rows) {
161         this.rows = rows;
162     }
163
164     boolean exactlyEqual(ArcDef def) {
165         return consolFun.equals(def.consolFun) && xff == def.xff &&
166                 steps == def.steps && rows == def.rows;
167     }
168 }
169
Popular Tags