KickJava   Java API By Example, From Geeks To Geeks.

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


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 data source definition within the RRD.
30  * Datasource definition consists of the following five elements:
31  *
32  * <ul>
33  * <li>data source name
34  * <li>data soruce type
35  * <li>heartbeat
36  * <li>minimal value
37  * <li>maximal value
38  * </ul>
39  * <p>For the complete explanation of all source definition parameters, see RRDTool's
40  * <a HREF="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a>.</p>
41  *
42  * @author <a HREF="mailto:saxon@jrobin.org">Sasa Markovic</a>
43  */

44 public class DsDef {
45     /** array of valid source types */
46     public static final String JavaDoc[] DS_TYPES = { "GAUGE", "COUNTER", "DERIVE", "ABSOLUTE" };
47
48     private String JavaDoc dsName, dsType;
49     private long heartbeat;
50     private double minValue, maxValue;
51
52     /**
53      * <p>Creates new data source definition object. This object should be passed as argument
54      * to {@link org.jrobin.core.RrdDef#addDatasource(org.jrobin.core.DsDef) addDatasource()} method of
55      * {@link org.jrobin.core.RrdDb RrdDb} object.</p>
56      *
57      * <p>For the complete explanation of all source definition parameters, see RRDTool's
58      * <a HREF="../../../../man/rrdcreate.html" target="man">rrdcreate man page</a></p>
59      *
60      * @param dsName Data source name.
61      * @param dsType Data source type. Valid values are "COUNTER", "GAUGE", "DERIVE"
62      * and "ABSOLUTE"
63      * @param heartbeat Hearbeat
64      * @param minValue Minimal value. Use <code>Double.NaN</code> if unknown.
65      * @param maxValue Maximal value. Use <code>Double.NaN</code> if unknown.
66      * @throws RrdException Thrown if any parameter has illegal value.
67      */

68     public DsDef(String JavaDoc dsName, String JavaDoc dsType, long heartbeat,
69                  double minValue, double maxValue) throws RrdException {
70         this.dsName = dsName;
71         this.dsType = dsType;
72         this.heartbeat = heartbeat;
73         this.minValue = minValue;
74         this.maxValue = maxValue;
75         validate();
76     }
77
78     /**
79      * Returns data source name.
80      * @return Data source name.
81      */

82     public String JavaDoc getDsName() {
83         return dsName;
84     }
85
86     /**
87      * Returns source type.
88      * @return Source type ("COUNTER", "GAUGE", "DERIVE" or "ABSOLUTE").
89      */

90     public String JavaDoc getDsType() {
91         return dsType;
92     }
93
94     /**
95      * Returns source heartbeat.
96      * @return Source heartbeat.
97      */

98     public long getHeartbeat() {
99         return heartbeat;
100     }
101
102     /**
103      * Returns minimal calculated source value.
104      * @return Minimal value.
105      */

106     public double getMinValue() {
107         return minValue;
108     }
109
110     /**
111      * Returns maximal calculated source value.
112      * @return Maximal value.
113      */

114     public double getMaxValue() {
115         return maxValue;
116     }
117
118     private void validate() throws RrdException {
119         if(dsName == null || dsName.length() == 0) {
120             throw new RrdException("Invalid datasource name specified");
121         }
122         if(!isValidDsType(dsType)) {
123             throw new RrdException("Invalid datasource type specified: " + dsType);
124         }
125         if(heartbeat <= 0) {
126             throw new RrdException("Invalid heartbeat, must be positive: " + heartbeat);
127         }
128         if(!Double.isNaN(minValue) && !Double.isNaN(maxValue) && minValue >= maxValue) {
129             throw new RrdException("Invalid min/max values specified: " +
130                 minValue + "/" + maxValue);
131         }
132     }
133
134     /**
135      * Checks if function argument represents valid source type.
136      * @param dsType Source type to be checked.
137      * @return <code>true</code> if <code>dsType</code> is valid type,
138      * <code>false</code> otherwise.
139      */

140     public static boolean isValidDsType(String JavaDoc dsType) {
141         for(int i = 0; i < DS_TYPES.length; i++) {
142             if(DS_TYPES[i].equals(dsType)) {
143                 return true;
144             }
145         }
146         return false;
147     }
148
149     /**
150      * Returns string representing source definition (RRDTool format).
151      * @return String containing all data source definition parameters.
152      */

153     public String JavaDoc dump() {
154         return "DS:" + dsName + ":" + dsType + ":" + heartbeat +
155             ":" + Util.formatDouble(minValue, "U", false) +
156             ":" + Util.formatDouble(maxValue, "U", false);
157     }
158
159     /**
160      * Checks if two datasource definitions are equal.
161      * Source definitions are treated as equal if they have the same source name.
162      * It is not possible to create RRD with two equal archive definitions.
163      * @param obj Archive definition to compare with.
164      * @return <code>true</code> if archive definitions are equal,
165      * <code>false</code> otherwise.
166      */

167     public boolean equals(Object JavaDoc obj) {
168         if(obj instanceof DsDef) {
169             DsDef dsObj = (DsDef) obj;
170             return dsName.equals(dsObj.dsName);
171         }
172         return false;
173     }
174
175     boolean exactlyEqual(DsDef def) {
176         return dsName.equals(def.dsName) && dsType.equals(def.dsType) &&
177                 heartbeat == def.heartbeat && Util.equal(minValue, def.minValue) &&
178                 Util.equal(maxValue, def.maxValue);
179     }
180 }
181
Popular Tags