KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > DefaultMeterDataset


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------------
23  * DefaultMeterDataset.java
24  * ------------------------
25  * (C) Copyright 2002, 2003, by Hari and Contributors.
26  *
27  * Original Author: Hari;
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: DefaultMeterDataset.java,v 1.3 2003/07/10 07:16:45 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 02-Apr-2002 : Version 1, based on code contributed by Hari (DG);
35  * 16-Apr-2002 : Updated to the latest version from Hari (DG);
36  * 26-Mar-2003 : Implemented Serializable (DG);
37  *
38  */

39
40 package org.jfree.data;
41
42 import java.io.Serializable JavaDoc;
43
44 /**
45  * A default implementation of the {@link MeterDataset} interface.
46  *
47  * @author Hari
48  */

49 public class DefaultMeterDataset extends AbstractDataset implements MeterDataset, Serializable JavaDoc {
50
51     /** The default adjustment. */
52     private static final double DEFAULT_ADJ = 1.0;
53
54     /** The current value. */
55     private Number JavaDoc value;
56
57     /** The lower bound of the overall range. */
58     private Number JavaDoc min;
59
60     /** The upper bound of the overall range. */
61     private Number JavaDoc max;
62
63     /** The lower bound of the 'normal' range. */
64     private Number JavaDoc minNormal;
65
66     /** The upper bound of the 'normal' range. */
67     private Number JavaDoc maxNormal;
68
69     /** The lower bound of the 'warning' range. */
70     private Number JavaDoc minWarning;
71
72     /** The upper bound of the 'warning' range. */
73     private Number JavaDoc maxWarning;
74
75     /** The lower bound of the 'critical' range. */
76     private Number JavaDoc minCritical;
77
78     /** The upper bound of the 'critical' range. */
79     private Number JavaDoc maxCritical;
80
81     /** The border type. */
82     private int borderType;
83
84     /** The units. */
85     private String JavaDoc units;
86
87     /**
88      * Default constructor.
89      */

90     public DefaultMeterDataset() {
91         this(new Double JavaDoc(0), new Double JavaDoc(0), null, null);
92     }
93
94     /**
95      * Creates a new dataset.
96      *
97      * @param min the minimum value.
98      * @param max the maximum value.
99      * @param value the current value.
100      * @param units the unit description.
101      */

102     public DefaultMeterDataset(Number JavaDoc min, Number JavaDoc max, Number JavaDoc value, String JavaDoc units) {
103         this(min, max, value, units, null, null, null, null, null, null, FULL_DATA);
104     }
105
106     /**
107      * Creates a new dataset.
108      *
109      * @param min the lower bound for the overall range.
110      * @param max the upper bound for the overall range.
111      * @param value the current value.
112      * @param units the unit description.
113      * @param minCritical the minimum critical value.
114      * @param maxCritical the maximum critical value.
115      * @param minWarning the minimum warning value.
116      * @param maxWarning the maximum warning value.
117      * @param minNormal the minimum normal value.
118      * @param maxNormal the maximum normal value.
119      * @param borderType the border type.
120      */

121     public DefaultMeterDataset(Number JavaDoc min, Number JavaDoc max, Number JavaDoc value,
122                                String JavaDoc units,
123                                Number JavaDoc minCritical, Number JavaDoc maxCritical,
124                                Number JavaDoc minWarning, Number JavaDoc maxWarning,
125                                Number JavaDoc minNormal, Number JavaDoc maxNormal,
126                                int borderType) {
127
128         setRange(min, max);
129         setValue(value);
130         setUnits(units);
131         setCriticalRange(minCritical, maxCritical);
132         setWarningRange(minWarning, maxWarning);
133         setNormalRange(minNormal, maxNormal);
134         setBorderType(borderType);
135
136     }
137
138     /**
139      * Returns true if the value is valid, and false otherwise.
140      *
141      * @return boolean.
142      */

143     public boolean isValueValid() {
144         return (value != null);
145     }
146
147     /**
148      * Returns the value.
149      *
150      * @return the value.
151      */

152     public Number JavaDoc getValue() {
153         return value;
154     }
155
156     /**
157      * Sets the value.
158      *
159      * @param value the new value.
160      */

161     public void setValue(double value) {
162         setValue(new Double JavaDoc(value));
163     }
164
165     /**
166      * Sets the value for the dataset.
167      *
168      * @param value the new value.
169      */

170     public void setValue(Number JavaDoc value) {
171
172         if (value != null && min != null && max != null) {
173             if (value.doubleValue() < min.doubleValue()
174                     || value.doubleValue() > max.doubleValue()) {
175
176                 throw new IllegalArgumentException JavaDoc("Value is out of range for min/max");
177
178             }
179         }
180         this.value = value;
181         if (value != null && min != null && max != null) {
182                 if (min.doubleValue() == max.doubleValue()) {
183                         min = new Double JavaDoc(value.doubleValue() - DEFAULT_ADJ);
184                     max = new Double JavaDoc(value.doubleValue() + DEFAULT_ADJ);
185                 }
186         }
187         fireDatasetChanged();
188
189     }
190
191     /**
192      * Returns the minimum value.
193      *
194      * @return the minimum value.
195      */

196     public Number JavaDoc getMinimumValue() {
197         return min;
198     }
199
200     /**
201      * Returns the maximum value.
202      *
203      * @return the maximum value.
204      */

205     public Number JavaDoc getMaximumValue() {
206         return max;
207     }
208
209     /**
210      * Returns the minimum normal value.
211      *
212      * @return the minimum normal value.
213      */

214     public Number JavaDoc getMinimumNormalValue() {
215         return minNormal;
216     }
217
218     /**
219      * Returns the maximum normal value.
220      *
221      * @return the maximum normal value.
222      */

223     public Number JavaDoc getMaximumNormalValue() {
224         return maxNormal;
225     }
226
227     /**
228      * Returns the minimum warning value.
229      *
230      * @return the minimum warning value.
231      */

232     public Number JavaDoc getMinimumWarningValue() {
233         return minWarning;
234     }
235
236     /**
237      * Returns the maximum warning value.
238      *
239      * @return the maximum warning value.
240      */

241     public Number JavaDoc getMaximumWarningValue() {
242         return maxWarning;
243     }
244
245     /**
246      * Returns the minimum critical value.
247      *
248      * @return the minimum critical value.
249      */

250     public Number JavaDoc getMinimumCriticalValue() {
251         return minCritical;
252     }
253
254     /**
255      * Returns the maximum critical value.
256      *
257      * @return the maximum critical value.
258      */

259     public Number JavaDoc getMaximumCriticalValue() {
260         return maxCritical;
261     }
262
263     /**
264      * Sets the range for the dataset. Registered listeners are notified of the change.
265      *
266      * @param min the new minimum.
267      * @param max the new maximum.
268      */

269     public void setRange(Number JavaDoc min, Number JavaDoc max) {
270
271         if (min == null || max == null) {
272             throw new IllegalArgumentException JavaDoc("Min/Max should not be null");
273         }
274
275         // swap min and max if necessary...
276
if (min.doubleValue() > max.doubleValue()) {
277             Number JavaDoc temp = min;
278             min = max;
279             max = temp;
280         }
281
282         if (this.value != null) {
283             if (min.doubleValue() == max.doubleValue()) {
284                 min = new Double JavaDoc(value.doubleValue() - DEFAULT_ADJ);
285                 max = new Double JavaDoc(value.doubleValue() + DEFAULT_ADJ);
286             }
287         }
288         this.min = min;
289         this.max = max;
290         fireDatasetChanged();
291
292     }
293
294     /**
295      * Sets the normal range for the dataset. Registered listeners are
296      * notified of the change.
297      *
298      * @param minNormal the new minimum.
299      * @param maxNormal the new maximum.
300      */

301     public void setNormalRange(Number JavaDoc minNormal, Number JavaDoc maxNormal) {
302
303         this.minNormal = minNormal;
304         this.maxNormal = maxNormal;
305
306         if (this.minNormal != null && this.minNormal.doubleValue() < this.min.doubleValue()) {
307             this.min = this.minNormal;
308         }
309         if (this.maxNormal != null && this.maxNormal.doubleValue() > this.max.doubleValue()) {
310             this.max = this.maxNormal;
311         }
312         fireDatasetChanged();
313     }
314
315     /**
316      * Sets the warning range for the dataset. Registered listeners are
317      * notified of the change.
318      *
319      * @param minWarning the new minimum.
320      * @param maxWarning the new maximum.
321      */

322     public void setWarningRange(Number JavaDoc minWarning, Number JavaDoc maxWarning) {
323
324         this.minWarning = minWarning;
325         this.maxWarning = maxWarning;
326
327         if (this.minWarning != null && this.minWarning.doubleValue() < this.min.doubleValue()) {
328             this.min = this.minWarning;
329         }
330         if (this.maxWarning != null && this.maxWarning.doubleValue() > this.max.doubleValue()) {
331             this.max = this.maxWarning;
332         }
333         fireDatasetChanged();
334
335     }
336
337     /**
338      * Sets the critical range for the dataset. Registered listeners are
339      * notified of the change.
340      *
341      * @param minCritical the new minimum.
342      * @param maxCritical the new maximum.
343      */

344     public void setCriticalRange(Number JavaDoc minCritical, Number JavaDoc maxCritical) {
345
346         this.minCritical = minCritical;
347         this.maxCritical = maxCritical;
348
349         if (this.minCritical != null && this.minCritical.doubleValue() < this.min.doubleValue()) {
350             this.min = this.minCritical;
351         }
352         if (this.maxCritical != null && this.maxCritical.doubleValue() > this.max.doubleValue()) {
353             this.max = this.maxCritical;
354         }
355         fireDatasetChanged();
356
357     }
358
359     /**
360      * Returns the measurement units for the data.
361      *
362      * @return The measurement units.
363      */

364     public String JavaDoc getUnits() {
365         return units;
366     }
367
368     /**
369      * Sets the measurement unit description.
370      *
371      * @param units the new description.
372      */

373     public void setUnits(String JavaDoc units) {
374         this.units = units;
375         fireDatasetChanged();
376     }
377
378     /**
379      * Returns the border type.
380      *
381      * @return the border type.
382      */

383     public int getBorderType() {
384         return borderType;
385     }
386
387     /**
388      * Sets the border type.
389      *
390      * @param borderType the new border type.
391      */

392     public void setBorderType(int borderType) {
393         this.borderType = borderType;
394         fireDatasetChanged();
395     }
396
397 }
398
Popular Tags