001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     *
027     * -----------------------
028     * SimpleHistogramBin.java
029     * -----------------------
030     * (C) Copyright 2005 by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: SimpleHistogramBin.java,v 1.4.2.1 2005/10/25 21:34:46 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 10-Jan-2005 : Version 1 (DG);
040     *
041     */
042    
043    package org.jfree.data.statistics;
044    
045    import java.io.Serializable;
046    
047    import org.jfree.util.PublicCloneable;
048    
049    /**
050     * A bin for the {@link SimpleHistogramDataset}.
051     */
052    public class SimpleHistogramBin implements Comparable, 
053                                               Cloneable, PublicCloneable, 
054                                               Serializable {
055    
056        /** For serialization. */
057        private static final long serialVersionUID = 3480862537505941742L;
058        
059        /** The lower bound for the bin. */
060        private double lowerBound;
061        
062        /** The upper bound for the bin. */
063        private double upperBound;
064        
065        /** 
066         * A flag that controls whether the lower bound is included in the bin 
067         * range. 
068         */
069        private boolean includeLowerBound;
070        
071        /** 
072         * A flag that controls whether the upper bound is included in the bin 
073         * range. 
074         */
075        private boolean includeUpperBound;
076        
077        /** The item count. */
078        private int itemCount;
079        
080        /**
081         * Creates a new bin.
082         * 
083         * @param lowerBound  the lower bound (inclusive).
084         * @param upperBound  the upper bound (inclusive);
085         */
086        public SimpleHistogramBin(double lowerBound, double upperBound) {
087            this(lowerBound, upperBound, true, true);
088        }
089    
090        /**
091         * Creates a new bin.
092         * 
093         * @param lowerBound  the lower bound.
094         * @param upperBound  the upper bound.
095         * @param includeLowerBound  include the lower bound?
096         * @param includeUpperBound  include the upper bound?
097         */
098        public SimpleHistogramBin(double lowerBound, double upperBound,
099                                  boolean includeLowerBound, 
100                                  boolean includeUpperBound) {
101            if (lowerBound >= upperBound) {
102                throw new IllegalArgumentException("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 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 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 clone() throws CloneNotSupportedException {
266            return super.clone();   
267        }
268        
269    }