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. * DefaultCategoryDataset.java
  26. * ---------------------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: DefaultCategoryDataset.java,v 1.3 2005/01/14 17:33:23 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 21-Jan-2003 : Added standard header, and renamed DefaultCategoryDataset (DG);
  37. * 13-Mar-2003 : Inserted DefaultKeyedValues2DDataset into class hierarchy (DG);
  38. * 06-Oct-2003 : Added incrementValue() method (DG);
  39. * 05-Apr-2004 : Added clear() method (DG);
  40. * 18-Aug-2004 : Moved from org.jfree.data --> org.jfree.data.category (DG);
  41. *
  42. */
  43. package org.jfree.data.category;
  44. import java.io.Serializable;
  45. import java.util.List;
  46. import org.jfree.data.DefaultKeyedValues2D;
  47. import org.jfree.data.general.AbstractDataset;
  48. import org.jfree.data.general.DatasetChangeEvent;
  49. /**
  50. * A default implementation of the {@link CategoryDataset} interface.
  51. */
  52. public class DefaultCategoryDataset extends AbstractDataset
  53. implements CategoryDataset, Serializable {
  54. /** A storage structure for the data. */
  55. private DefaultKeyedValues2D data;
  56. /**
  57. * Creates a new (empty) dataset.
  58. */
  59. public DefaultCategoryDataset() {
  60. this.data = new DefaultKeyedValues2D();
  61. }
  62. /**
  63. * Returns the number of rows in the table.
  64. *
  65. * @return The row count.
  66. */
  67. public int getRowCount() {
  68. return this.data.getRowCount();
  69. }
  70. /**
  71. * Returns the number of columns in the table.
  72. *
  73. * @return The column count.
  74. */
  75. public int getColumnCount() {
  76. return this.data.getColumnCount();
  77. }
  78. /**
  79. * Returns a value from the table.
  80. *
  81. * @param row the row index (zero-based).
  82. * @param column the column index (zero-based).
  83. *
  84. * @return The value (possibly <code>null</code>).
  85. */
  86. public Number getValue(int row, int column) {
  87. return this.data.getValue(row, column);
  88. }
  89. /**
  90. * Returns a row key.
  91. *
  92. * @param row the row index (zero-based).
  93. *
  94. * @return The row key.
  95. */
  96. public Comparable getRowKey(int row) {
  97. return this.data.getRowKey(row);
  98. }
  99. /**
  100. * Returns the row index for a given key.
  101. *
  102. * @param key the row key.
  103. *
  104. * @return The row index.
  105. */
  106. public int getRowIndex(Comparable key) {
  107. return this.data.getRowIndex(key);
  108. }
  109. /**
  110. * Returns the row keys.
  111. *
  112. * @return The keys.
  113. */
  114. public List getRowKeys() {
  115. return this.data.getRowKeys();
  116. }
  117. /**
  118. * Returns a column key.
  119. *
  120. * @param column the column index (zero-based).
  121. *
  122. * @return The column key.
  123. */
  124. public Comparable getColumnKey(int column) {
  125. return this.data.getColumnKey(column);
  126. }
  127. /**
  128. * Returns the column index for a given key.
  129. *
  130. * @param key the column key.
  131. *
  132. * @return The column index.
  133. */
  134. public int getColumnIndex(Comparable key) {
  135. return this.data.getColumnIndex(key);
  136. }
  137. /**
  138. * Returns the column keys.
  139. *
  140. * @return The keys.
  141. */
  142. public List getColumnKeys() {
  143. return this.data.getColumnKeys();
  144. }
  145. /**
  146. * Returns the value for a pair of keys.
  147. * <P>
  148. * This method should return <code>null</code> if either of the keys is not found.
  149. *
  150. * @param rowKey the row key.
  151. * @param columnKey the column key.
  152. *
  153. * @return The value.
  154. */
  155. public Number getValue(Comparable rowKey, Comparable columnKey) {
  156. return this.data.getValue(rowKey, columnKey);
  157. }
  158. /**
  159. * Adds a value to the table. Performs the same function as setValue(...).
  160. *
  161. * @param value the value.
  162. * @param rowKey the row key.
  163. * @param columnKey the column key.
  164. */
  165. public void addValue(Number value, Comparable rowKey, Comparable columnKey) {
  166. this.data.addValue(value, rowKey, columnKey);
  167. fireDatasetChanged();
  168. }
  169. /**
  170. * Adds a value to the table.
  171. *
  172. * @param value the value.
  173. * @param rowKey the row key.
  174. * @param columnKey the column key.
  175. */
  176. public void addValue(double value, Comparable rowKey, Comparable columnKey) {
  177. addValue(new Double(value), rowKey, columnKey);
  178. }
  179. /**
  180. * Adds or updates a value in the table.
  181. *
  182. * @param value the value (<code>null</code> permitted).
  183. * @param rowKey the row key (<code>null</code> not permitted).
  184. * @param columnKey the column key (<code>null</code> not permitted).
  185. */
  186. public void setValue(Number value, Comparable rowKey, Comparable columnKey) {
  187. this.data.setValue(value, rowKey, columnKey);
  188. fireDatasetChanged();
  189. }
  190. /**
  191. * Adds or updates a value in the table.
  192. *
  193. * @param value the value.
  194. * @param rowKey the row key (<code>null</code> not permitted).
  195. * @param columnKey the column key (<code>null</code> not permitted).
  196. */
  197. public void setValue(double value, Comparable rowKey, Comparable columnKey) {
  198. setValue(new Double(value), rowKey, columnKey);
  199. }
  200. /**
  201. * Adds the specified value to an existing value in the dataset (if the existing value is
  202. * <code>null</code>, it is treated as if it were 0.0).
  203. *
  204. * @param value the value.
  205. * @param rowKey the row key.
  206. * @param columnKey the column key.
  207. */
  208. public void incrementValue(double value,
  209. Comparable rowKey,
  210. Comparable columnKey) {
  211. double existing = 0.0;
  212. Number n = getValue(rowKey, columnKey);
  213. if (n != null) {
  214. existing = n.doubleValue();
  215. }
  216. setValue(existing + value, rowKey, columnKey);
  217. }
  218. /**
  219. * Removes a value from the dataset.
  220. *
  221. * @param rowKey the row key.
  222. * @param columnKey the column key.
  223. */
  224. public void removeValue(Comparable rowKey, Comparable columnKey) {
  225. this.data.removeValue(rowKey, columnKey);
  226. fireDatasetChanged();
  227. }
  228. /**
  229. * Removes a row from the dataset.
  230. *
  231. * @param rowIndex the row index.
  232. */
  233. public void removeRow(int rowIndex) {
  234. this.data.removeRow(rowIndex);
  235. fireDatasetChanged();
  236. }
  237. /**
  238. * Removes a row from the dataset.
  239. *
  240. * @param rowKey the row key.
  241. */
  242. public void removeRow(Comparable rowKey) {
  243. this.data.removeRow(rowKey);
  244. fireDatasetChanged();
  245. }
  246. /**
  247. * Removes a column from the dataset.
  248. *
  249. * @param columnIndex the column index.
  250. */
  251. public void removeColumn(int columnIndex) {
  252. this.data.removeColumn(columnIndex);
  253. fireDatasetChanged();
  254. }
  255. /**
  256. * Removes a column from the dataset.
  257. *
  258. * @param columnKey the column key.
  259. */
  260. public void removeColumn(Comparable columnKey) {
  261. this.data.removeColumn(columnKey);
  262. fireDatasetChanged();
  263. }
  264. /**
  265. * Clears all data from the dataset and sends a {@link DatasetChangeEvent} to all registered
  266. * listeners.
  267. */
  268. public void clear() {
  269. this.data.clear();
  270. fireDatasetChanged();
  271. }
  272. /**
  273. * Tests if this object is equal to another.
  274. *
  275. * @param o the other object.
  276. *
  277. * @return A boolean.
  278. */
  279. public boolean equals(Object o) {
  280. if (o == null) {
  281. return false;
  282. }
  283. if (o == this) {
  284. return true;
  285. }
  286. if (!(o instanceof CategoryDataset)) {
  287. return false;
  288. }
  289. CategoryDataset cd = (CategoryDataset) o;
  290. if (!getRowKeys().equals(cd.getRowKeys())) {
  291. return false;
  292. }
  293. if (!getColumnKeys().equals(cd.getColumnKeys())) {
  294. return false;
  295. }
  296. int rowCount = getRowCount();
  297. int colCount = getColumnCount();
  298. for (int r = 0; r < rowCount; r++) {
  299. for (int c = 0; c < colCount; c++) {
  300. Number v1 = getValue(r, c);
  301. Number v2 = cd.getValue(r, c);
  302. if (v1 == null) {
  303. if (v2 != null) {
  304. return false;
  305. }
  306. }
  307. else if (!v1.equals(v2)) {
  308. return false;
  309. }
  310. }
  311. }
  312. return true;
  313. }
  314. /**
  315. * Returns a hash code for the dataset.
  316. *
  317. * @return A hash code.
  318. */
  319. public int hashCode() {
  320. return this.data.hashCode();
  321. }
  322. }