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