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. * DefaultKeyedValues2D.java
  26. * -------------------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Andreas Schroeder;
  31. *
  32. * $Id: DefaultKeyedValues2D.java,v 1.5 2005/01/12 14:54:58 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 28-Oct-2002 : Version 1 (DG);
  37. * 21-Jan-2003 : Updated Javadocs (DG);
  38. * 13-Mar-2003 : Implemented Serializable (DG);
  39. * 18-Aug-2003 : Implemented Cloneable (DG);
  40. * 31-Mar-2004 : Made the rows optionally sortable by a flag (AS);
  41. * 01-Apr-2004 : Implemented remove method (AS);
  42. * 05-Apr-2004 : Added clear() method (DG);
  43. * 15-Sep-2004 : Fixed clone() method (DG);
  44. * 12-Jan-2005 : Fixed bug in getValue() method (DG);
  45. *
  46. */
  47. package org.jfree.data;
  48. import java.io.Serializable;
  49. import java.util.Collections;
  50. import java.util.Iterator;
  51. import java.util.List;
  52. import org.jfree.util.ObjectUtilities;
  53. /**
  54. * A data structure that stores zero, one or many values, where each value
  55. * is associated with two keys (a 'row' key and a 'column' key). The keys
  56. * should be (a) instances of {@link Comparable} and (b) immutable.
  57. */
  58. public class DefaultKeyedValues2D implements KeyedValues2D, Cloneable, Serializable {
  59. /** The row keys. */
  60. private List rowKeys;
  61. /** The column keys. */
  62. private List columnKeys;
  63. /** The row data. */
  64. private List rows;
  65. /** If the row keys should be sorted by their comparable order. */
  66. private boolean sortRowKeys;
  67. /**
  68. * Creates a new instance (initially empty).
  69. */
  70. public DefaultKeyedValues2D() {
  71. this(false);
  72. }
  73. /**
  74. * Creates a new instance (initially empty).
  75. *
  76. * @param sortRowKeys if the row keys should be sorted.
  77. */
  78. public DefaultKeyedValues2D(boolean sortRowKeys) {
  79. this.rowKeys = new java.util.ArrayList();
  80. this.columnKeys = new java.util.ArrayList();
  81. this.rows = new java.util.ArrayList();
  82. this.sortRowKeys = sortRowKeys;
  83. }
  84. /**
  85. * Returns the row count.
  86. *
  87. * @return The row count.
  88. */
  89. public int getRowCount() {
  90. return this.rowKeys.size();
  91. }
  92. /**
  93. * Returns the column count.
  94. *
  95. * @return The column count.
  96. */
  97. public int getColumnCount() {
  98. return this.columnKeys.size();
  99. }
  100. /**
  101. * Returns the value for a given row and column.
  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. Number result = null;
  110. DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row);
  111. if (rowData != null) {
  112. Comparable columnKey = (Comparable) this.columnKeys.get(column);
  113. // the row may not have an entry for this key, in which case the return value is null
  114. int index = rowData.getIndex(columnKey);
  115. if (index >= 0) {
  116. result = rowData.getValue(index);
  117. }
  118. }
  119. return result;
  120. }
  121. /**
  122. * Returns the key for a given row.
  123. *
  124. * @param row the row index (zero based).
  125. *
  126. * @return The row index.
  127. */
  128. public Comparable getRowKey(int row) {
  129. return (Comparable) this.rowKeys.get(row);
  130. }
  131. /**
  132. * Returns the row index for a given key.
  133. *
  134. * @param key the key (<code>null</code> not permitted).
  135. *
  136. * @return The row index.
  137. */
  138. public int getRowIndex(Comparable key) {
  139. if (key == null) {
  140. throw new IllegalArgumentException("Null 'key' argument.");
  141. }
  142. if (this.sortRowKeys) {
  143. return Collections.binarySearch(this.rowKeys, key);
  144. }
  145. else {
  146. return this.rowKeys.indexOf(key);
  147. }
  148. }
  149. /**
  150. * Returns the row keys.
  151. *
  152. * @return The row keys.
  153. */
  154. public List getRowKeys() {
  155. return Collections.unmodifiableList(this.rowKeys);
  156. }
  157. /**
  158. * Returns the key for a given column.
  159. *
  160. * @param column the column.
  161. *
  162. * @return The key.
  163. */
  164. public Comparable getColumnKey(int column) {
  165. return (Comparable) this.columnKeys.get(column);
  166. }
  167. /**
  168. * Returns the column index for a given key.
  169. *
  170. * @param key the key (<code>null</code> not permitted).
  171. *
  172. * @return the column index.
  173. */
  174. public int getColumnIndex(Comparable key) {
  175. if (key == null) {
  176. throw new IllegalArgumentException("Null 'key' argument.");
  177. }
  178. return this.columnKeys.indexOf(key);
  179. }
  180. /**
  181. * Returns the column keys.
  182. *
  183. * @return The column keys.
  184. */
  185. public List getColumnKeys() {
  186. return Collections.unmodifiableList(this.columnKeys);
  187. }
  188. /**
  189. * Returns the value for the given row and column keys.
  190. *
  191. * @param rowKey the row key (<code>null</code> not permitted).
  192. * @param columnKey the column key (<code>null</code> not permitted).
  193. *
  194. * @return The value.
  195. */
  196. public Number getValue(Comparable rowKey, Comparable columnKey) {
  197. if (rowKey == null) {
  198. throw new IllegalArgumentException("Null 'rowKey' argument.");
  199. }
  200. if (columnKey == null) {
  201. throw new IllegalArgumentException("Null 'columnKey' argument.");
  202. }
  203. Number result = null;
  204. int row = getRowIndex(rowKey);
  205. if (row >= 0) {
  206. DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row);
  207. result = rowData.getValue(columnKey);
  208. }
  209. return result;
  210. }
  211. /**
  212. * Adds a value to the table. Performs the same function as
  213. * #setValue(Number, Comparable, Comparable).
  214. *
  215. * @param value the value (<code>null</code> permitted).
  216. * @param rowKey the row key (<code>null</code> not permitted).
  217. * @param columnKey the column key (<code>null</code> not permitted).
  218. */
  219. public void addValue(Number value, Comparable rowKey, Comparable columnKey) {
  220. // defer argument checking
  221. setValue(value, rowKey, columnKey);
  222. }
  223. /**
  224. * Adds or updates a value.
  225. *
  226. * @param value the value (<code>null</code> permitted).
  227. * @param rowKey the row key (<code>null</code> not permitted).
  228. * @param columnKey the column key (<code>null</code> not permitted).
  229. */
  230. public void setValue(Number value, Comparable rowKey, Comparable columnKey) {
  231. DefaultKeyedValues row;
  232. int rowIndex = getRowIndex(rowKey);
  233. if (rowIndex >= 0) {
  234. row = (DefaultKeyedValues) this.rows.get(rowIndex);
  235. }
  236. else {
  237. row = new DefaultKeyedValues();
  238. if (this.sortRowKeys) {
  239. rowIndex = -rowIndex - 1;
  240. this.rowKeys.add(rowIndex, rowKey);
  241. this.rows.add(rowIndex, row);
  242. }
  243. else {
  244. this.rowKeys.add(rowKey);
  245. this.rows.add(row);
  246. }
  247. }
  248. row.setValue(columnKey, value);
  249. int columnIndex = this.columnKeys.indexOf(columnKey);
  250. if (columnIndex < 0) {
  251. this.columnKeys.add(columnKey);
  252. }
  253. }
  254. /**
  255. * Removes a value.
  256. *
  257. * @param rowKey the row key (<code>null</code> not permitted).
  258. * @param columnKey the column key (<code>null</code> not permitted).
  259. */
  260. public void removeValue(Comparable rowKey, Comparable columnKey) {
  261. setValue(null, rowKey, columnKey);
  262. // 1. check whether the row is now empty.
  263. boolean allNull = true;
  264. int rowIndex = getRowIndex(rowKey);
  265. DefaultKeyedValues row = (DefaultKeyedValues) this.rows.get(rowIndex);
  266. for (int item = 0, itemCount = row.getItemCount(); item < itemCount; item++) {
  267. if (row.getValue(item) != null) {
  268. allNull = false;
  269. break;
  270. }
  271. }
  272. if (allNull) {
  273. this.rowKeys.remove(rowIndex);
  274. this.rows.remove(rowIndex);
  275. }
  276. // 2. check whether the column is now empty.
  277. allNull = true;
  278. int columnIndex = getColumnIndex(columnKey);
  279. for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) {
  280. row = (DefaultKeyedValues) this.rows.get(item);
  281. if (row.getValue(columnIndex) != null) {
  282. allNull = false;
  283. break;
  284. }
  285. }
  286. if (allNull) {
  287. for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) {
  288. row = (DefaultKeyedValues) this.rows.get(item);
  289. row.removeValue(columnIndex);
  290. }
  291. this.columnKeys.remove(columnIndex);
  292. }
  293. }
  294. /**
  295. * Removes a row.
  296. *
  297. * @param rowIndex the row index.
  298. */
  299. public void removeRow(int rowIndex) {
  300. this.rowKeys.remove(rowIndex);
  301. this.rows.remove(rowIndex);
  302. }
  303. /**
  304. * Removes a row.
  305. *
  306. * @param rowKey the row key.
  307. */
  308. public void removeRow(Comparable rowKey) {
  309. removeRow(getRowIndex(rowKey));
  310. }
  311. /**
  312. * Removes a column.
  313. *
  314. * @param columnIndex the column index.
  315. */
  316. public void removeColumn(int columnIndex) {
  317. Comparable columnKey = getColumnKey(columnIndex);
  318. removeColumn(columnKey);
  319. }
  320. /**
  321. * Removes a column.
  322. *
  323. * @param columnKey the column key (<code>null</code> not permitted).
  324. */
  325. public void removeColumn(Comparable columnKey) {
  326. Iterator iterator = this.rows.iterator();
  327. while (iterator.hasNext()) {
  328. DefaultKeyedValues rowData = (DefaultKeyedValues) iterator.next();
  329. rowData.removeValue(columnKey);
  330. }
  331. this.columnKeys.remove(columnKey);
  332. }
  333. /**
  334. * Clears all the data and associated keys.
  335. */
  336. public void clear() {
  337. this.rowKeys.clear();
  338. this.columnKeys.clear();
  339. this.rows.clear();
  340. }
  341. /**
  342. * Tests if this object is equal to another.
  343. *
  344. * @param o the other object (<code>null</code> permitted).
  345. *
  346. * @return A boolean.
  347. */
  348. public boolean equals(Object o) {
  349. if (o == null) {
  350. return false;
  351. }
  352. if (o == this) {
  353. return true;
  354. }
  355. if (!(o instanceof KeyedValues2D)) {
  356. return false;
  357. }
  358. KeyedValues2D kv2D = (KeyedValues2D) o;
  359. if (!getRowKeys().equals(kv2D.getRowKeys())) {
  360. return false;
  361. }
  362. if (!getColumnKeys().equals(kv2D.getColumnKeys())) {
  363. return false;
  364. }
  365. int rowCount = getRowCount();
  366. if (rowCount != kv2D.getRowCount()) {
  367. return false;
  368. }
  369. int colCount = getColumnCount();
  370. if (colCount != kv2D.getColumnCount()) {
  371. return false;
  372. }
  373. for (int r = 0; r < rowCount; r++) {
  374. for (int c = 0; c < colCount; c++) {
  375. Number v1 = getValue(r, c);
  376. Number v2 = kv2D.getValue(r, c);
  377. if (v1 == null) {
  378. if (v2 != null) {
  379. return false;
  380. }
  381. }
  382. else {
  383. if (!v1.equals(v2)) {
  384. return false;
  385. }
  386. }
  387. }
  388. }
  389. return true;
  390. }
  391. /**
  392. * Returns a hash code.
  393. *
  394. * @return A hash code.
  395. */
  396. public int hashCode() {
  397. int result;
  398. result = this.rowKeys.hashCode();
  399. result = 29 * result + this.columnKeys.hashCode();
  400. result = 29 * result + this.rows.hashCode();
  401. return result;
  402. }
  403. /**
  404. * Returns a clone.
  405. *
  406. * @return A clone.
  407. *
  408. * @throws CloneNotSupportedException this class will not throw this exception, but subclasses
  409. * (if any) might.
  410. */
  411. public Object clone() throws CloneNotSupportedException {
  412. DefaultKeyedValues2D clone = (DefaultKeyedValues2D) super.clone();
  413. // for the keys, a shallow copy should be fine because keys
  414. // should be immutable...
  415. clone.columnKeys = new java.util.ArrayList(this.columnKeys);
  416. clone.rowKeys = new java.util.ArrayList(this.rowKeys);
  417. // but the row data requires a deep copy
  418. clone.rows = (List) ObjectUtilities.deepClone(this.rows);
  419. return clone;
  420. }
  421. }