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
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software Foundation,
  21. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * --------------------------------------
  27. * DefaultStatisticalCategoryDataset.java
  28. * --------------------------------------
  29. * (C) Copyright 2002-2005, by Pascal Collet and Contributors.
  30. *
  31. * Original Author: Pascal Collet;
  32. * Contributor(s): David Gilbert (for Object Refinery Limited);
  33. *
  34. * $Id: DefaultStatisticalCategoryDataset.java,v 1.8 2005/02/10 10:07:03 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. * 21-Aug-2002 : Version 1, contributed by Pascal Collet (DG);
  39. * 07-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  40. * 05-Feb-2003 : Revised implementation to use KeyedObjects2D (DG);
  41. * 28-Aug-2003 : Moved from org.jfree.data --> org.jfree.data.statistics (DG);
  42. * 06-Oct-2003 : Removed incorrect Javadoc text (DG);
  43. * 18-Nov-2004 : Updated for changes in RangeInfo interface (DG);
  44. * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0
  45. * release (DG);
  46. * 01-Feb-2005 : Changed minimumRangeValue and maximumRangeValue from Double
  47. * to double (DG);
  48. * 05-Feb-2005 : Implemented equals() method (DG);
  49. *
  50. */
  51. package org.jfree.data.statistics;
  52. import java.util.List;
  53. import org.jfree.data.KeyedObjects2D;
  54. import org.jfree.data.Range;
  55. import org.jfree.data.RangeInfo;
  56. import org.jfree.data.general.AbstractDataset;
  57. /**
  58. * A convenience class that provides a default implementation of the
  59. * {@link StatisticalCategoryDataset} interface.
  60. *
  61. * @author Pascal Collet
  62. */
  63. public class DefaultStatisticalCategoryDataset extends AbstractDataset
  64. implements StatisticalCategoryDataset, RangeInfo {
  65. /** Storage for the data. */
  66. private KeyedObjects2D data;
  67. /** The minimum range value. */
  68. private double minimumRangeValue;
  69. /** The maximum range value. */
  70. private double maximumRangeValue;
  71. /** The range of values. */
  72. private Range rangeBounds;
  73. /**
  74. * Creates a new dataset.
  75. */
  76. public DefaultStatisticalCategoryDataset() {
  77. this.data = new KeyedObjects2D();
  78. this.minimumRangeValue = 0.0;
  79. this.maximumRangeValue = 0.0;
  80. this.rangeBounds = new Range(0.0, 0.0);
  81. }
  82. /**
  83. * Returns the mean value for an item.
  84. *
  85. * @param row the row index (zero-based).
  86. * @param column the column index (zero-based).
  87. *
  88. * @return The mean value.
  89. */
  90. public Number getMeanValue(int row, int column) {
  91. Number result = null;
  92. MeanAndStandardDeviation masd
  93. = (MeanAndStandardDeviation) this.data.getObject(row, column);
  94. if (masd != null) {
  95. result = masd.getMean();
  96. }
  97. return result;
  98. }
  99. /**
  100. * Returns the value for an item (for this dataset, the mean value is
  101. * returned).
  102. *
  103. * @param row the row index.
  104. * @param column the column index.
  105. *
  106. * @return The value.
  107. */
  108. public Number getValue(int row, int column) {
  109. return getMeanValue(row, column);
  110. }
  111. /**
  112. * Returns the value for an item (for this dataset, the mean value is
  113. * returned).
  114. *
  115. * @param rowKey the row key.
  116. * @param columnKey the columnKey.
  117. *
  118. * @return The value.
  119. */
  120. public Number getValue(Comparable rowKey, Comparable columnKey) {
  121. return getMeanValue(rowKey, columnKey);
  122. }
  123. /**
  124. * Returns the mean value for an item.
  125. *
  126. * @param rowKey the row key.
  127. * @param columnKey the columnKey.
  128. *
  129. * @return The mean value.
  130. */
  131. public Number getMeanValue(Comparable rowKey, Comparable columnKey) {
  132. Number result = null;
  133. MeanAndStandardDeviation masd
  134. = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
  135. if (masd != null) {
  136. result = masd.getMean();
  137. }
  138. return result;
  139. }
  140. /**
  141. * Returns the standard deviation value for an item.
  142. *
  143. * @param row the row index (zero-based).
  144. * @param column the column index (zero-based).
  145. *
  146. * @return The standard deviation.
  147. */
  148. public Number getStdDevValue(int row, int column) {
  149. Number result = null;
  150. MeanAndStandardDeviation masd
  151. = (MeanAndStandardDeviation) this.data.getObject(row, column);
  152. if (masd != null) {
  153. result = masd.getStandardDeviation();
  154. }
  155. return result;
  156. }
  157. /**
  158. * Returns the standard deviation value for an item.
  159. *
  160. * @param rowKey the row key.
  161. * @param columnKey the columnKey.
  162. *
  163. * @return The standard deviation.
  164. */
  165. public Number getStdDevValue(Comparable rowKey, Comparable columnKey) {
  166. Number result = null;
  167. MeanAndStandardDeviation masd
  168. = (MeanAndStandardDeviation) this.data.getObject(rowKey, columnKey);
  169. if (masd != null) {
  170. result = masd.getStandardDeviation();
  171. }
  172. return result;
  173. }
  174. /**
  175. * Returns the column index for a given key.
  176. *
  177. * @param key the column key.
  178. *
  179. * @return The column index.
  180. */
  181. public int getColumnIndex(Comparable key) {
  182. return this.data.getColumnIndex(key);
  183. }
  184. /**
  185. * Returns a column key.
  186. *
  187. * @param column the column index (zero-based).
  188. *
  189. * @return The column key.
  190. */
  191. public Comparable getColumnKey(int column) {
  192. return this.data.getColumnKey(column);
  193. }
  194. /**
  195. * Returns the column keys.
  196. *
  197. * @return The keys.
  198. */
  199. public List getColumnKeys() {
  200. return this.data.getColumnKeys();
  201. }
  202. /**
  203. * Returns the row index for a given key.
  204. *
  205. * @param key the row key.
  206. *
  207. * @return The row index.
  208. */
  209. public int getRowIndex(Comparable key) {
  210. return this.data.getRowIndex(key);
  211. }
  212. /**
  213. * Returns a row key.
  214. *
  215. * @param row the row index (zero-based).
  216. *
  217. * @return The row key.
  218. */
  219. public Comparable getRowKey(int row) {
  220. return this.data.getRowKey(row);
  221. }
  222. /**
  223. * Returns the row keys.
  224. *
  225. * @return The keys.
  226. */
  227. public List getRowKeys() {
  228. return this.data.getRowKeys();
  229. }
  230. /**
  231. * Returns the number of rows in the table.
  232. *
  233. * @return The row count.
  234. */
  235. public int getRowCount() {
  236. return this.data.getRowCount();
  237. }
  238. /**
  239. * Returns the number of columns in the table.
  240. *
  241. * @return The column count.
  242. */
  243. public int getColumnCount() {
  244. return this.data.getColumnCount();
  245. }
  246. /**
  247. * Adds a mean and standard deviation to the table.
  248. *
  249. * @param mean the mean.
  250. * @param standardDeviation the standard deviation.
  251. * @param rowKey the row key.
  252. * @param columnKey the column key.
  253. */
  254. public void add(double mean, double standardDeviation,
  255. Comparable rowKey, Comparable columnKey) {
  256. add(new Double(mean), new Double(standardDeviation), rowKey, columnKey);
  257. }
  258. /**
  259. * Adds a mean and standard deviation to the table.
  260. *
  261. * @param mean the mean.
  262. * @param standardDeviation the standard deviation.
  263. * @param rowKey the row key.
  264. * @param columnKey the column key.
  265. */
  266. public void add(Number mean, Number standardDeviation,
  267. Comparable rowKey, Comparable columnKey) {
  268. MeanAndStandardDeviation item = new MeanAndStandardDeviation(
  269. mean, standardDeviation
  270. );
  271. this.data.addObject(item, rowKey, columnKey);
  272. double m = 0.0;
  273. double sd = 0.0;
  274. if (mean != null) {
  275. m = mean.doubleValue();
  276. }
  277. if (standardDeviation != null) {
  278. sd = standardDeviation.doubleValue();
  279. }
  280. if ((m + sd) > this.maximumRangeValue) {
  281. this.maximumRangeValue = m + sd;
  282. this.rangeBounds = new Range(
  283. this.minimumRangeValue, this.maximumRangeValue
  284. );
  285. }
  286. if ((m - sd) < this.minimumRangeValue) {
  287. this.minimumRangeValue = m - sd;
  288. this.rangeBounds = new Range(
  289. this.minimumRangeValue, this.maximumRangeValue
  290. );
  291. }
  292. fireDatasetChanged();
  293. }
  294. /**
  295. * Returns the minimum y-value in the dataset.
  296. *
  297. * @param includeInterval a flag that determines whether or not the
  298. * y-interval is taken into account (ignored for
  299. * this dataset).
  300. *
  301. * @return The minimum value.
  302. */
  303. public double getRangeLowerBound(boolean includeInterval) {
  304. return this.minimumRangeValue;
  305. }
  306. /**
  307. * Returns the maximum y-value in the dataset.
  308. *
  309. * @param includeInterval a flag that determines whether or not the
  310. * y-interval is taken into account (ignored for
  311. * this dataset).
  312. *
  313. * @return The maximum value.
  314. */
  315. public double getRangeUpperBound(boolean includeInterval) {
  316. return this.maximumRangeValue;
  317. }
  318. /**
  319. * Returns the range of the values in this dataset's range.
  320. *
  321. * @param includeInterval a flag that determines whether or not the
  322. * y-interval is taken into account.
  323. *
  324. * @return The range.
  325. */
  326. public Range getRangeBounds(boolean includeInterval) {
  327. return this.rangeBounds;
  328. }
  329. /**
  330. * Tests this instance for equality with an arbitrary object.
  331. *
  332. * @param obj the object (<code>null</code> permitted).
  333. *
  334. * @return A boolean.
  335. */
  336. public boolean equals(Object obj) {
  337. if (obj == this) {
  338. return true;
  339. }
  340. if (!(obj instanceof DefaultStatisticalCategoryDataset)) {
  341. return false;
  342. }
  343. DefaultStatisticalCategoryDataset that
  344. = (DefaultStatisticalCategoryDataset) obj;
  345. if (!this.data.equals(that.data)) {
  346. return false;
  347. }
  348. return true;
  349. }
  350. }