KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > statistics > SimpleHistogramBin


1 /* ===========================================================
2  * JFreeChart : a free chart library for the Java(tm) platform
3  * ===========================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jfreechart/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------------
28  * SimpleHistogramBin.java
29  * -----------------------
30  * (C) Copyright 2005 by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: SimpleHistogramBin.java,v 1.4.2.1 2005/10/25 21:34:46 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 10-Jan-2005 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.data.statistics;
44
45 import java.io.Serializable JavaDoc;
46
47 import org.jfree.util.PublicCloneable;
48
49 /**
50  * A bin for the {@link SimpleHistogramDataset}.
51  */

52 public class SimpleHistogramBin implements Comparable JavaDoc,
53                                            Cloneable JavaDoc, PublicCloneable,
54                                            Serializable JavaDoc {
55
56     /** For serialization. */
57     private static final long serialVersionUID = 3480862537505941742L;
58     
59     /** The lower bound for the bin. */
60     private double lowerBound;
61     
62     /** The upper bound for the bin. */
63     private double upperBound;
64     
65     /**
66      * A flag that controls whether the lower bound is included in the bin
67      * range.
68      */

69     private boolean includeLowerBound;
70     
71     /**
72      * A flag that controls whether the upper bound is included in the bin
73      * range.
74      */

75     private boolean includeUpperBound;
76     
77     /** The item count. */
78     private int itemCount;
79     
80     /**
81      * Creates a new bin.
82      *
83      * @param lowerBound the lower bound (inclusive).
84      * @param upperBound the upper bound (inclusive);
85      */

86     public SimpleHistogramBin(double lowerBound, double upperBound) {
87         this(lowerBound, upperBound, true, true);
88     }
89
90     /**
91      * Creates a new bin.
92      *
93      * @param lowerBound the lower bound.
94      * @param upperBound the upper bound.
95      * @param includeLowerBound include the lower bound?
96      * @param includeUpperBound include the upper bound?
97      */

98     public SimpleHistogramBin(double lowerBound, double upperBound,
99                               boolean includeLowerBound,
100                               boolean includeUpperBound) {
101         if (lowerBound >= upperBound) {
102             throw new IllegalArgumentException JavaDoc("Invalid bounds");
103         }
104         this.lowerBound = lowerBound;
105         this.upperBound = upperBound;
106         this.includeLowerBound = includeLowerBound;
107         this.includeUpperBound = includeUpperBound;
108         this.itemCount = 0;
109     }
110     
111     /**
112      * Returns the lower bound.
113      *
114      * @return The lower bound.
115      */

116     public double getLowerBound() {
117         return this.lowerBound;
118     }
119     
120     /**
121      * Return the upper bound.
122      *
123      * @return The upper bound.
124      */

125     public double getUpperBound() {
126         return this.upperBound;
127     }
128     
129     /**
130      * Returns the item count.
131      *
132      * @return The item count.
133      */

134     public int getItemCount() {
135         return this.itemCount;
136     }
137    
138     /**
139      * Sets the item count.
140      *
141      * @param count the item count.
142      */

143     public void setItemCount(int count) {
144         this.itemCount = count;
145     }
146
147     /**
148      * Returns <code>true</code> if the specified value belongs in the bin,
149      * and <code>false</code> otherwise.
150      *
151      * @param value the value.
152      *
153      * @return A boolean.
154      */

155     public boolean accepts(double value) {
156         if (Double.isNaN(value)) {
157             return false;
158         }
159         if (value < this.lowerBound) {
160             return false;
161         }
162         if (value > this.upperBound) {
163             return false;
164         }
165         if (value == this.lowerBound) {
166             return this.includeLowerBound;
167         }
168         if (value == this.upperBound) {
169             return this.includeUpperBound;
170         }
171         return true;
172     }
173     
174     /**
175      * Returns <code>true</code> if this bin overlaps with the specified bin,
176      * and <code>false</code> otherwise.
177      *
178      * @param bin the other bin (<code>null</code> not permitted).
179      *
180      * @return A boolean.
181      */

182     public boolean overlapsWith(SimpleHistogramBin bin) {
183         if (this.upperBound < bin.lowerBound) {
184             return false;
185         }
186         if (this.lowerBound > bin.upperBound) {
187             return false;
188         }
189         if (this.upperBound == bin.lowerBound) {
190             return this.includeUpperBound && bin.includeLowerBound;
191         }
192         if (this.lowerBound == bin.upperBound) {
193             return this.includeLowerBound && bin.includeUpperBound;
194         }
195         return true;
196     }
197     
198     /**
199      * Compares the bin to an arbitrary object and returns the relative
200      * ordering.
201      *
202      * @param obj the object.
203      *
204      * @return An integer indicating the relative ordering of the this bin and
205      * the given object.
206      */

207     public int compareTo(Object JavaDoc obj) {
208         if (!(obj instanceof SimpleHistogramBin)) {
209             return 0;
210         }
211         SimpleHistogramBin bin = (SimpleHistogramBin) obj;
212         if (this.lowerBound < bin.lowerBound) {
213             return -1;
214         }
215         if (this.lowerBound > bin.lowerBound) {
216             return 1;
217         }
218         // lower bounds are the same
219
if (this.upperBound < bin.upperBound) {
220             return -1;
221         }
222         if (this.upperBound > bin.upperBound) {
223             return 1;
224         }
225         return 0;
226     }
227     
228     /**
229      * Tests this bin for equality with an arbitrary object.
230      *
231      * @param obj the object (<code>null</code> permitted).
232      *
233      * @return A boolean.
234      */

235     public boolean equals(Object JavaDoc obj) {
236         if (!(obj instanceof SimpleHistogramBin)) {
237             return false;
238         }
239         SimpleHistogramBin that = (SimpleHistogramBin) obj;
240         if (this.lowerBound != that.lowerBound) {
241             return false;
242         }
243         if (this.upperBound != that.upperBound) {
244             return false;
245         }
246         if (this.includeLowerBound != that.includeLowerBound) {
247             return false;
248         }
249         if (this.includeUpperBound != that.includeUpperBound) {
250             return false;
251         }
252         if (this.itemCount != that.itemCount) {
253             return false;
254         }
255         return true;
256     }
257     
258     /**
259      * Returns a clone of the bin.
260      *
261      * @return A clone.
262      *
263      * @throws CloneNotSupportedException not thrown by this class.
264      */

265     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
266         return super.clone();
267     }
268     
269 }
270
Popular Tags