001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2007, 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-2007, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * Changes
036     * -------
037     * 10-Jan-2005 : Version 1 (DG);
038     *
039     */
040    
041    package org.jfree.data.statistics;
042    
043    import java.io.Serializable;
044    
045    import org.jfree.util.PublicCloneable;
046    
047    /**
048     * A bin for the {@link SimpleHistogramDataset}.
049     */
050    public class SimpleHistogramBin implements Comparable, 
051                                               Cloneable, PublicCloneable, 
052                                               Serializable {
053    
054        /** For serialization. */
055        private static final long serialVersionUID = 3480862537505941742L;
056        
057        /** The lower bound for the bin. */
058        private double lowerBound;
059        
060        /** The upper bound for the bin. */
061        private double upperBound;
062        
063        /** 
064         * A flag that controls whether the lower bound is included in the bin 
065         * range. 
066         */
067        private boolean includeLowerBound;
068        
069        /** 
070         * A flag that controls whether the upper bound is included in the bin 
071         * range. 
072         */
073        private boolean includeUpperBound;
074        
075        /** The item count. */
076        private int itemCount;
077        
078        /**
079         * Creates a new bin.
080         * 
081         * @param lowerBound  the lower bound (inclusive).
082         * @param upperBound  the upper bound (inclusive);
083         */
084        public SimpleHistogramBin(double lowerBound, double upperBound) {
085            this(lowerBound, upperBound, true, true);
086        }
087    
088        /**
089         * Creates a new bin.
090         * 
091         * @param lowerBound  the lower bound.
092         * @param upperBound  the upper bound.
093         * @param includeLowerBound  include the lower bound?
094         * @param includeUpperBound  include the upper bound?
095         */
096        public SimpleHistogramBin(double lowerBound, double upperBound,
097                                  boolean includeLowerBound, 
098                                  boolean includeUpperBound) {
099            if (lowerBound >= upperBound) {
100                throw new IllegalArgumentException("Invalid bounds");
101            }
102            this.lowerBound = lowerBound;
103            this.upperBound = upperBound;
104            this.includeLowerBound = includeLowerBound;
105            this.includeUpperBound = includeUpperBound;
106            this.itemCount = 0;
107        }
108        
109        /**
110         * Returns the lower bound.
111         * 
112         * @return The lower bound.
113         */
114        public double getLowerBound() {
115            return this.lowerBound;
116        }
117        
118        /**
119         * Return the upper bound.
120         * 
121         * @return The upper bound.
122         */
123        public double getUpperBound() {
124            return this.upperBound;
125        }
126        
127        /**
128         * Returns the item count.
129         * 
130         * @return The item count.
131         */
132        public int getItemCount() {
133            return this.itemCount;
134        }
135       
136        /**
137         * Sets the item count.
138         * 
139         * @param count  the item count.
140         */
141        public void setItemCount(int count) {
142            this.itemCount = count;
143        }
144    
145        /**
146         * Returns <code>true</code> if the specified value belongs in the bin, 
147         * and <code>false</code> otherwise.
148         * 
149         * @param value  the value.
150         * 
151         * @return A boolean.
152         */
153        public boolean accepts(double value) {
154            if (Double.isNaN(value)) {
155                return false;
156            }
157            if (value < this.lowerBound) {
158                return false;
159            }
160            if (value > this.upperBound) {
161                return false;
162            }
163            if (value == this.lowerBound) {
164                return this.includeLowerBound;
165            }
166            if (value == this.upperBound) {
167                return this.includeUpperBound;
168            }
169            return true;
170        }
171        
172        /**
173         * Returns <code>true</code> if this bin overlaps with the specified bin,
174         * and <code>false</code> otherwise.
175         * 
176         * @param bin  the other bin (<code>null</code> not permitted).
177         * 
178         * @return A boolean.
179         */
180        public boolean overlapsWith(SimpleHistogramBin bin) {
181            if (this.upperBound < bin.lowerBound) {
182                return false;
183            }
184            if (this.lowerBound > bin.upperBound) {
185                return false;
186            }
187            if (this.upperBound == bin.lowerBound) {
188                return this.includeUpperBound && bin.includeLowerBound;
189            }
190            if (this.lowerBound == bin.upperBound) {
191                return this.includeLowerBound && bin.includeUpperBound;
192            }
193            return true;
194        }
195        
196        /**
197         * Compares the bin to an arbitrary object and returns the relative 
198         * ordering.
199         * 
200         * @param obj  the object.
201         * 
202         * @return An integer indicating the relative ordering of the this bin and 
203         *         the given object.
204         */
205        public int compareTo(Object obj) {
206            if (!(obj instanceof SimpleHistogramBin)) {
207                return 0;
208            }
209            SimpleHistogramBin bin = (SimpleHistogramBin) obj;
210            if (this.lowerBound < bin.lowerBound) {
211                return -1;
212            }
213            if (this.lowerBound > bin.lowerBound) {
214                return 1;
215            }
216            // lower bounds are the same
217            if (this.upperBound < bin.upperBound) {
218                return -1;
219            }
220            if (this.upperBound > bin.upperBound) {
221                return 1;
222            }
223            return 0;
224        }
225        
226        /**
227         * Tests this bin for equality with an arbitrary object.
228         * 
229         * @param obj  the object (<code>null</code> permitted).
230         * 
231         * @return A boolean.
232         */
233        public boolean equals(Object obj) {
234            if (!(obj instanceof SimpleHistogramBin)) {
235                return false;
236            }
237            SimpleHistogramBin that = (SimpleHistogramBin) obj;
238            if (this.lowerBound != that.lowerBound) {
239                return false;
240            }
241            if (this.upperBound != that.upperBound) {
242                return false;
243            }
244            if (this.includeLowerBound != that.includeLowerBound) {
245                return false;
246            }
247            if (this.includeUpperBound != that.includeUpperBound) {
248                return false;
249            }
250            if (this.itemCount != that.itemCount) {
251                return false;
252            }
253            return true;
254        }
255        
256        /**
257         * Returns a clone of the bin.
258         * 
259         * @return A clone.
260         * 
261         * @throws CloneNotSupportedException not thrown by this class.
262         */
263        public Object clone() throws CloneNotSupportedException {
264            return super.clone();   
265        }
266        
267    }