KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jrobin > core > jrrd > DataSource


1 /*
2  * Copyright (C) 2001 Ciaran Treanor <ciaran@codeloop.com>
3  *
4  * Distributable under GPL license.
5  * See terms of license at gnu.org.
6  *
7  * $Id: DataSource.java,v 1.1 2004/07/22 09:34:10 saxon64 Exp $
8  */

9 package org.jrobin.core.jrrd;
10
11 import java.io.*;
12 import java.text.NumberFormat JavaDoc;
13
14 /**
15  * Instances of this class model a data source in an RRD file.
16  *
17  * @author <a HREF="mailto:ciaran@codeloop.com">Ciaran Treanor</a>
18  * @version $Revision: 1.1 $
19  */

20 public class DataSource {
21
22     long offset;
23     long size;
24     String JavaDoc name;
25     DataSourceType type;
26     int minimumHeartbeat;
27     double minimum;
28     double maximum;
29     PDPStatusBlock pdpStatusBlock;
30
31     DataSource(RRDFile file) throws IOException {
32
33         offset = file.getFilePointer();
34         name = file.readString(Constants.DS_NAM_SIZE);
35         type = DataSourceType.get(file.readString(Constants.DST_SIZE));
36
37         file.align(8);
38
39         minimumHeartbeat = file.readInt(true);
40
41         file.align(8);
42
43         minimum = file.readDouble();
44         maximum = file.readDouble();
45
46         // Skip rest of ds_def_t.par[]
47
file.align();
48         file.skipBytes(56);
49
50         size = file.getFilePointer() - offset;
51     }
52
53     void loadPDPStatusBlock(RRDFile file) throws IOException {
54         pdpStatusBlock = new PDPStatusBlock(file);
55     }
56
57     /**
58      * Returns the primary data point status block for this data source.
59      *
60      * @return the primary data point status block for this data source.
61      */

62     public PDPStatusBlock getPDPStatusBlock() {
63         return pdpStatusBlock;
64     }
65
66     /**
67      * Returns the minimum required heartbeat for this data source.
68      *
69      * @return the minimum required heartbeat for this data source.
70      */

71     public int getMinimumHeartbeat() {
72         return minimumHeartbeat;
73     }
74
75     /**
76      * Returns the minimum value input to this data source can have.
77      *
78      * @return the minimum value input to this data source can have.
79      */

80     public double getMinimum() {
81         return minimum;
82     }
83
84     /**
85      * Returns the type this data source is.
86      *
87      * @return the type this data source is.
88      * @see DataSourceType
89      */

90     public DataSourceType getType() {
91         return type;
92     }
93
94     /**
95      * Returns the maximum value input to this data source can have.
96      *
97      * @return the maximum value input to this data source can have.
98      */

99     public double getMaximum() {
100         return maximum;
101     }
102
103     /**
104      * Returns the name of this data source.
105      *
106      * @return the name of this data source.
107      */

108     public String JavaDoc getName() {
109         return name;
110     }
111
112     void printInfo(PrintStream s, NumberFormat JavaDoc numberFormat) {
113
114         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("ds[");
115
116         sb.append(name);
117         s.print(sb);
118         s.print("].type = \"");
119         s.print(type);
120         s.println("\"");
121         s.print(sb);
122         s.print("].minimal_heartbeat = ");
123         s.println(minimumHeartbeat);
124         s.print(sb);
125         s.print("].min = ");
126         s.println(Double.isNaN(minimum)
127                 ? "NaN"
128                 : numberFormat.format(minimum));
129         s.print(sb);
130         s.print("].max = ");
131         s.println(Double.isNaN(maximum)
132                 ? "NaN"
133                 : numberFormat.format(maximum));
134         s.print(sb);
135         s.print("].last_ds = ");
136         s.println(pdpStatusBlock.lastReading);
137         s.print(sb);
138         s.print("].value = ");
139
140         double value = pdpStatusBlock.value;
141
142         s.println(Double.isNaN(value)
143                 ? "NaN"
144                 : numberFormat.format(value));
145         s.print(sb);
146         s.print("].unknown_sec = ");
147         s.println(pdpStatusBlock.unknownSeconds);
148     }
149
150     void toXml(PrintStream s) {
151
152         s.println("\t<ds>");
153         s.print("\t\t<name> ");
154         s.print(name);
155         s.println(" </name>");
156         s.print("\t\t<type> ");
157         s.print(type);
158         s.println(" </type>");
159         s.print("\t\t<minimal_heartbeat> ");
160         s.print(minimumHeartbeat);
161         s.println(" </minimal_heartbeat>");
162         s.print("\t\t<min> ");
163         s.print(minimum);
164         s.println(" </min>");
165         s.print("\t\t<max> ");
166         s.print(maximum);
167         s.println(" </max>");
168         s.println();
169         s.println("\t\t<!-- PDP Status -->");
170         s.print("\t\t<last_ds> ");
171         s.print(pdpStatusBlock.lastReading);
172         s.println(" </last_ds>");
173         s.print("\t\t<value> ");
174         s.print(pdpStatusBlock.value);
175         s.println(" </value>");
176         s.print("\t\t<unknown_sec> ");
177         s.print(pdpStatusBlock.unknownSeconds);
178         s.println(" </unknown_sec>");
179         s.println("\t</ds>");
180         s.println();
181     }
182
183     /**
184      * Returns a summary the contents of this data source.
185      *
186      * @return a summary of the information contained in this data source.
187      */

188     public String JavaDoc toString() {
189
190         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("[DataSource: OFFSET=0x");
191
192         sb.append(Long.toHexString(offset));
193         sb.append(", SIZE=0x");
194         sb.append(Long.toHexString(size));
195         sb.append(", name=");
196         sb.append(name);
197         sb.append(", type=");
198         sb.append(type.toString());
199         sb.append(", minHeartbeat=");
200         sb.append(minimumHeartbeat);
201         sb.append(", min=");
202         sb.append(minimum);
203         sb.append(", max=");
204         sb.append(maximum);
205         sb.append("]");
206         sb.append("\n\t\t");
207         sb.append(pdpStatusBlock.toString());
208
209         return sb.toString();
210     }
211 }
212
Popular Tags