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