1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -----------------------
  25. * SimpleHistogramBin.java
  26. * -----------------------
  27. * (C) Copyright 2005 by Object Refinery Limited and Contributors.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: SimpleHistogramBin.java,v 1.2 2005/02/10 10:06:44 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 10-Jan-2005 : Version 1 (DG);
  37. *
  38. */
  39. package org.jfree.data.statistics;
  40. import java.io.Serializable;
  41. import org.jfree.util.PublicCloneable;
  42. /**
  43. * A bin for the {@link SimpleHistogramDataset}.
  44. */
  45. public class SimpleHistogramBin implements Comparable, Cloneable, PublicCloneable, Serializable {
  46. /** The lower bound for the bin. */
  47. private double lowerBound;
  48. /** The upper bound for the bin. */
  49. private double upperBound;
  50. /** A flag that controls whether the lower bound is included in the bin range. */
  51. private boolean includeLowerBound;
  52. /** A flag that controls whether the upper bound is included in the bin range. */
  53. private boolean includeUpperBound;
  54. /** The item count. */
  55. private int itemCount;
  56. /**
  57. * Creates a new bin.
  58. *
  59. * @param lowerBound the lower bound (inclusive).
  60. * @param upperBound the upper bound (inclusive);
  61. */
  62. public SimpleHistogramBin(double lowerBound, double upperBound) {
  63. this(lowerBound, upperBound, true, true);
  64. }
  65. /**
  66. * Creates a new bin.
  67. *
  68. * @param lowerBound the lower bound.
  69. * @param upperBound the upper bound.
  70. * @param includeLowerBound include the lower bound?
  71. * @param includeUpperBound include the upper bound?
  72. */
  73. public SimpleHistogramBin(double lowerBound, double upperBound,
  74. boolean includeLowerBound, boolean includeUpperBound) {
  75. if (lowerBound >= upperBound) {
  76. throw new IllegalArgumentException("Invalid bounds");
  77. }
  78. this.lowerBound = lowerBound;
  79. this.upperBound = upperBound;
  80. this.includeLowerBound = includeLowerBound;
  81. this.includeUpperBound = includeUpperBound;
  82. this.itemCount = 0;
  83. }
  84. /**
  85. * Returns the lower bound.
  86. *
  87. * @return The lower bound.
  88. */
  89. public double getLowerBound() {
  90. return this.lowerBound;
  91. }
  92. /**
  93. * Return the upper bound.
  94. *
  95. * @return The upper bound.
  96. */
  97. public double getUpperBound() {
  98. return this.upperBound;
  99. }
  100. /**
  101. * Returns the item count.
  102. *
  103. * @return The item count.
  104. */
  105. public int getItemCount() {
  106. return this.itemCount;
  107. }
  108. /**
  109. * Sets the item count.
  110. *
  111. * @param count the item count.
  112. */
  113. public void setItemCount(int count) {
  114. this.itemCount = count;
  115. }
  116. /**
  117. * Returns <code>true</code> if the specified value belongs in the bin,
  118. * and <code>false</code> otherwise.
  119. *
  120. * @param value the value.
  121. *
  122. * @return A boolean.
  123. */
  124. public boolean accepts(double value) {
  125. if (Double.isNaN(value)) {
  126. return false;
  127. }
  128. if (value < this.lowerBound) {
  129. return false;
  130. }
  131. if (value > this.upperBound) {
  132. return false;
  133. }
  134. if (value == this.lowerBound) {
  135. return this.includeLowerBound;
  136. }
  137. if (value == this.upperBound) {
  138. return this.includeUpperBound;
  139. }
  140. return true;
  141. }
  142. /**
  143. * Returns <code>true</code> if this bin overlaps with the specified bin,
  144. * and <code>false</code> otherwise.
  145. *
  146. * @param bin the other bin (<code>null</code> not permitted).
  147. *
  148. * @return A boolean.
  149. */
  150. public boolean overlapsWith(SimpleHistogramBin bin) {
  151. if (this.upperBound < bin.lowerBound) {
  152. return false;
  153. }
  154. if (this.lowerBound > bin.upperBound) {
  155. return false;
  156. }
  157. if (this.upperBound == bin.lowerBound) {
  158. return this.includeUpperBound && bin.includeLowerBound;
  159. }
  160. if (this.lowerBound == bin.upperBound) {
  161. return this.includeLowerBound && bin.includeUpperBound;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Compares the bin to an arbitrary object and returns the relative ordering.
  167. *
  168. * @param obj the object.
  169. *
  170. * @return An integer indicating the relative ordering of the this bin and the given object.
  171. */
  172. public int compareTo(Object obj) {
  173. if (!(obj instanceof SimpleHistogramBin)) {
  174. return 0;
  175. }
  176. SimpleHistogramBin bin = (SimpleHistogramBin) obj;
  177. if (this.lowerBound < bin.lowerBound) {
  178. return -1;
  179. }
  180. if (this.lowerBound > bin.lowerBound) {
  181. return 1;
  182. }
  183. // lower bounds are the same
  184. if (this.upperBound < bin.upperBound) {
  185. return -1;
  186. }
  187. if (this.upperBound > bin.upperBound) {
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * Tests this bin for equality with an arbitrary object.
  194. *
  195. * @param obj the object (<code>null</code> permitted).
  196. *
  197. * @return A boolean.
  198. */
  199. public boolean equals(Object obj) {
  200. if (!(obj instanceof SimpleHistogramBin)) {
  201. return false;
  202. }
  203. SimpleHistogramBin that = (SimpleHistogramBin) obj;
  204. if (this.lowerBound != that.lowerBound) {
  205. return false;
  206. }
  207. if (this.upperBound != that.upperBound) {
  208. return false;
  209. }
  210. if (this.includeLowerBound != that.includeLowerBound) {
  211. return false;
  212. }
  213. if (this.includeUpperBound != that.includeUpperBound) {
  214. return false;
  215. }
  216. if (this.itemCount != that.itemCount) {
  217. return false;
  218. }
  219. return true;
  220. }
  221. /**
  222. * Returns a clone of the bin.
  223. *
  224. * @return A clone.
  225. *
  226. * @throws CloneNotSupportedException not thrown by this class.
  227. */
  228. public Object clone() throws CloneNotSupportedException {
  229. return super.clone();
  230. }
  231. }