001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2022, by David Gilbert 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 * HistogramBin.java
029 * -----------------
030 * (C) Copyright 2003-2008, by Jelai Wang and Contributors.
031 *
032 * Original Author:  Jelai Wang (jelaiw AT mindspring.com);
033 * Contributor(s):   David Gilbert;
034 *
035 * Changes
036 * -------
037 * 06-Jul-2003 : Version 1, contributed by Jelai Wang (DG);
038 * 07-Jul-2003 : Changed package and added Javadocs (DG);
039 * 01-Mar-2004 : Moved from org.jfree.data --> org.jfree.data.statistics (DG);
040 * ------------- JFREECHART 1.0.x ---------------------------------------------
041 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
042 * 29-Jan-2017 : Added missing hashCode (TH);
043 *
044 */
045
046package org.jfree.data.statistics;
047
048import java.io.Serializable;
049
050/**
051 * A bin for the {@link HistogramDataset} class.
052 */
053public class HistogramBin implements Cloneable, Serializable {
054
055    /** For serialization. */
056    private static final long serialVersionUID = 7614685080015589931L;
057
058    /** The number of items in the bin. */
059    private int count;
060
061    /** The start boundary. */
062    private double startBoundary;
063
064    /** The end boundary. */
065    private double endBoundary;
066
067    /**
068     * Creates a new bin.
069     *
070     * @param startBoundary  the start boundary.
071     * @param endBoundary  the end boundary.
072     */
073    public HistogramBin(double startBoundary, double endBoundary) {
074        if (startBoundary > endBoundary) {
075            throw new IllegalArgumentException(
076                    "HistogramBin():  startBoundary > endBoundary.");
077        }
078        this.count = 0;
079        this.startBoundary = startBoundary;
080        this.endBoundary = endBoundary;
081    }
082
083    /**
084     * Returns the number of items in the bin.
085     *
086     * @return The item count.
087     */
088    public int getCount() {
089        return this.count;
090    }
091
092    /**
093     * Increments the item count.
094     */
095    public void incrementCount() {
096        this.count++;
097    }
098
099    /**
100     * Returns the start boundary.
101     *
102     * @return The start boundary.
103     */
104    public double getStartBoundary() {
105        return this.startBoundary;
106    }
107
108    /**
109     * Returns the end boundary.
110     *
111     * @return The end boundary.
112     */
113    public double getEndBoundary() {
114        return this.endBoundary;
115    }
116
117    /**
118     * Returns the bin width.
119     *
120     * @return The bin width.
121     */
122    public double getBinWidth() {
123        return this.endBoundary - this.startBoundary;
124    }
125
126    /**
127     * Tests this object for equality with an arbitrary object.
128     *
129     * @param obj  the object to test against.
130     *
131     * @return A boolean.
132     */
133    @Override
134    public boolean equals(Object obj) {
135        if (obj == null) {
136            return false;
137        }
138        if (obj == this) {
139            return true;
140        }
141        if (obj instanceof HistogramBin) {
142            HistogramBin bin = (HistogramBin) obj;
143            boolean b0 = bin.startBoundary == this.startBoundary;
144            boolean b1 = bin.endBoundary == this.endBoundary;
145            boolean b2 = bin.count == this.count;
146            return b0 && b1 && b2;
147        }
148        return false;
149    }
150
151    @Override
152    public int hashCode()
153    {
154        int hash = 7;
155        hash = 37 * hash + this.count;
156        hash = 37 * hash + (int) ( Double.doubleToLongBits( this.startBoundary ) ^ ( Double.doubleToLongBits( this.startBoundary ) >>> 32 ) );
157        hash = 37 * hash + (int) ( Double.doubleToLongBits( this.endBoundary ) ^ ( Double.doubleToLongBits( this.endBoundary ) >>> 32 ) );
158        return hash;
159    }
160
161    /**
162     * Returns a clone of the bin.
163     *
164     * @return A clone.
165     *
166     * @throws CloneNotSupportedException not thrown by this class.
167     */
168    @Override
169    public Object clone() throws CloneNotSupportedException {
170        return super.clone();
171    }
172
173}