PageRenderTime 110ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/jfreechart-1.0.14/tests/org/jfree/data/general/junit/TestIntervalCategoryDataset.java

https://bitbucket.org/psychonetic/sudokusolver
Java | 456 lines | 176 code | 35 blank | 245 comment | 25 complexity | e511de9252bf0603177d5223e7c1105c MD5 | raw file
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2011, 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
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * --------------------------------
  28. * TestIntervalCategoryDataset.java
  29. * --------------------------------
  30. * (C) Copyright 2009, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 10-Sep-2009 : Version 1, based on DefaultCategoryDataset (DG);
  38. *
  39. */
  40. package org.jfree.data.general.junit;
  41. import java.io.Serializable;
  42. import java.util.List;
  43. import org.jfree.data.KeyedObjects2D;
  44. import org.jfree.data.UnknownKeyException;
  45. import org.jfree.data.category.IntervalCategoryDataset;
  46. import org.jfree.data.general.AbstractDataset;
  47. import org.jfree.data.general.DatasetChangeEvent;
  48. import org.jfree.util.PublicCloneable;
  49. /**
  50. * A test implementation of the {@link IntervalCategoryDataset} interface.
  51. */
  52. public class TestIntervalCategoryDataset extends AbstractDataset
  53. implements IntervalCategoryDataset, PublicCloneable, Serializable {
  54. /** For serialization. */
  55. private static final long serialVersionUID = -8168173757291644622L;
  56. /** A storage structure for the data. */
  57. private KeyedObjects2D data;
  58. /**
  59. * Creates a new (empty) dataset.
  60. */
  61. public TestIntervalCategoryDataset() {
  62. this.data = new KeyedObjects2D();
  63. }
  64. /**
  65. * Returns the number of rows in the table.
  66. *
  67. * @return The row count.
  68. *
  69. * @see #getColumnCount()
  70. */
  71. public int getRowCount() {
  72. return this.data.getRowCount();
  73. }
  74. /**
  75. * Returns the number of columns in the table.
  76. *
  77. * @return The column count.
  78. *
  79. * @see #getRowCount()
  80. */
  81. public int getColumnCount() {
  82. return this.data.getColumnCount();
  83. }
  84. /**
  85. * Returns a value from the table.
  86. *
  87. * @param row the row index (zero-based).
  88. * @param column the column index (zero-based).
  89. *
  90. * @return The value (possibly <code>null</code>).
  91. *
  92. * @see #addValue(Number, Comparable, Comparable)
  93. * @see #removeValue(Comparable, Comparable)
  94. */
  95. public Number getValue(int row, int column) {
  96. IntervalDataItem item = (IntervalDataItem) this.data.getObject(row,
  97. column);
  98. if (item == null) {
  99. return null;
  100. }
  101. return item.getValue();
  102. }
  103. /**
  104. * Returns the key for the specified row.
  105. *
  106. * @param row the row index (zero-based).
  107. *
  108. * @return The row key.
  109. *
  110. * @see #getRowIndex(Comparable)
  111. * @see #getRowKeys()
  112. * @see #getColumnKey(int)
  113. */
  114. public Comparable getRowKey(int row) {
  115. return this.data.getRowKey(row);
  116. }
  117. /**
  118. * Returns the row index for a given key.
  119. *
  120. * @param key the row key (<code>null</code> not permitted).
  121. *
  122. * @return The row index.
  123. *
  124. * @see #getRowKey(int)
  125. */
  126. public int getRowIndex(Comparable key) {
  127. // defer null argument check
  128. return this.data.getRowIndex(key);
  129. }
  130. /**
  131. * Returns the row keys.
  132. *
  133. * @return The keys.
  134. *
  135. * @see #getRowKey(int)
  136. */
  137. public List getRowKeys() {
  138. return this.data.getRowKeys();
  139. }
  140. /**
  141. * Returns a column key.
  142. *
  143. * @param column the column index (zero-based).
  144. *
  145. * @return The column key.
  146. *
  147. * @see #getColumnIndex(Comparable)
  148. */
  149. public Comparable getColumnKey(int column) {
  150. return this.data.getColumnKey(column);
  151. }
  152. /**
  153. * Returns the column index for a given key.
  154. *
  155. * @param key the column key (<code>null</code> not permitted).
  156. *
  157. * @return The column index.
  158. *
  159. * @see #getColumnKey(int)
  160. */
  161. public int getColumnIndex(Comparable key) {
  162. // defer null argument check
  163. return this.data.getColumnIndex(key);
  164. }
  165. /**
  166. * Returns the column keys.
  167. *
  168. * @return The keys.
  169. *
  170. * @see #getColumnKey(int)
  171. */
  172. public List getColumnKeys() {
  173. return this.data.getColumnKeys();
  174. }
  175. /**
  176. * Returns the value for a pair of keys.
  177. *
  178. * @param rowKey the row key (<code>null</code> not permitted).
  179. * @param columnKey the column key (<code>null</code> not permitted).
  180. *
  181. * @return The value (possibly <code>null</code>).
  182. *
  183. * @throws UnknownKeyException if either key is not defined in the dataset.
  184. *
  185. * @see #addValue(Number, Comparable, Comparable)
  186. */
  187. public Number getValue(Comparable rowKey, Comparable columnKey) {
  188. IntervalDataItem item = (IntervalDataItem) this.data.getObject(rowKey,
  189. columnKey);
  190. if (item == null) {
  191. return null;
  192. }
  193. return item.getValue();
  194. }
  195. /**
  196. * Adds a value to the table. Performs the same function as setValue().
  197. *
  198. * @param value the value.
  199. * @param rowKey the row key.
  200. * @param columnKey the column key.
  201. *
  202. * @see #getValue(Comparable, Comparable)
  203. * @see #removeValue(Comparable, Comparable)
  204. */
  205. public void addItem(Number value, Number lower, Number upper,
  206. Comparable rowKey, Comparable columnKey) {
  207. IntervalDataItem item = new IntervalDataItem(value, lower, upper);
  208. this.data.addObject(item, rowKey, columnKey);
  209. fireDatasetChanged();
  210. }
  211. /**
  212. * Adds a value to the table.
  213. *
  214. * @param value the value.
  215. * @param rowKey the row key.
  216. * @param columnKey the column key.
  217. *
  218. * @see #getValue(Comparable, Comparable)
  219. */
  220. public void addItem(double value, double lower, double upper,
  221. Comparable rowKey, Comparable columnKey) {
  222. addItem(new Double(value), new Double(lower), new Double(upper),
  223. rowKey, columnKey);
  224. }
  225. /**
  226. * Adds or updates a value in the table and sends a
  227. * {@link DatasetChangeEvent} to all registered listeners.
  228. *
  229. * @param value the value (<code>null</code> permitted).
  230. * @param rowKey the row key (<code>null</code> not permitted).
  231. * @param columnKey the column key (<code>null</code> not permitted).
  232. *
  233. * @see #getValue(Comparable, Comparable)
  234. */
  235. public void setItem(Number value, Number lower, Number upper,
  236. Comparable rowKey, Comparable columnKey) {
  237. IntervalDataItem item = new IntervalDataItem(value, lower, upper);
  238. this.data.addObject(item, rowKey, columnKey);
  239. fireDatasetChanged();
  240. }
  241. /**
  242. * Adds or updates a value in the table and sends a
  243. * {@link DatasetChangeEvent} to all registered listeners.
  244. *
  245. * @param value the value.
  246. * @param rowKey the row key (<code>null</code> not permitted).
  247. * @param columnKey the column key (<code>null</code> not permitted).
  248. *
  249. * @see #getValue(Comparable, Comparable)
  250. */
  251. public void setItem(double value, double lower, double upper,
  252. Comparable rowKey, Comparable columnKey) {
  253. setItem(new Double(value), new Double(lower), new Double(upper),
  254. rowKey, columnKey);
  255. }
  256. /**
  257. * Removes a value from the dataset and sends a {@link DatasetChangeEvent}
  258. * to all registered listeners.
  259. *
  260. * @param rowKey the row key.
  261. * @param columnKey the column key.
  262. *
  263. * @see #addValue(Number, Comparable, Comparable)
  264. */
  265. public void removeItem(Comparable rowKey, Comparable columnKey) {
  266. this.data.removeObject(rowKey, columnKey);
  267. fireDatasetChanged();
  268. }
  269. /**
  270. * Removes a row from the dataset and sends a {@link DatasetChangeEvent}
  271. * to all registered listeners.
  272. *
  273. * @param rowIndex the row index.
  274. *
  275. * @see #removeColumn(int)
  276. */
  277. public void removeRow(int rowIndex) {
  278. this.data.removeRow(rowIndex);
  279. fireDatasetChanged();
  280. }
  281. /**
  282. * Removes a row from the dataset and sends a {@link DatasetChangeEvent}
  283. * to all registered listeners.
  284. *
  285. * @param rowKey the row key.
  286. *
  287. * @see #removeColumn(Comparable)
  288. */
  289. public void removeRow(Comparable rowKey) {
  290. this.data.removeRow(rowKey);
  291. fireDatasetChanged();
  292. }
  293. /**
  294. * Removes a column from the dataset and sends a {@link DatasetChangeEvent}
  295. * to all registered listeners.
  296. *
  297. * @param columnIndex the column index.
  298. *
  299. * @see #removeRow(int)
  300. */
  301. public void removeColumn(int columnIndex) {
  302. this.data.removeColumn(columnIndex);
  303. fireDatasetChanged();
  304. }
  305. /**
  306. * Removes a column from the dataset and sends a {@link DatasetChangeEvent}
  307. * to all registered listeners.
  308. *
  309. * @param columnKey the column key (<code>null</code> not permitted).
  310. *
  311. * @see #removeRow(Comparable)
  312. *
  313. * @throws UnknownKeyException if <code>columnKey</code> is not defined
  314. * in the dataset.
  315. */
  316. public void removeColumn(Comparable columnKey) {
  317. this.data.removeColumn(columnKey);
  318. fireDatasetChanged();
  319. }
  320. /**
  321. * Clears all data from the dataset and sends a {@link DatasetChangeEvent}
  322. * to all registered listeners.
  323. */
  324. public void clear() {
  325. this.data.clear();
  326. fireDatasetChanged();
  327. }
  328. /**
  329. * Tests this dataset for equality with an arbitrary object.
  330. *
  331. * @param obj the object (<code>null</code> permitted).
  332. *
  333. * @return A boolean.
  334. */
  335. public boolean equals(Object obj) {
  336. if (obj == this) {
  337. return true;
  338. }
  339. if (!(obj instanceof TestIntervalCategoryDataset)) {
  340. return false;
  341. }
  342. TestIntervalCategoryDataset that = (TestIntervalCategoryDataset) obj;
  343. if (!getRowKeys().equals(that.getRowKeys())) {
  344. return false;
  345. }
  346. if (!getColumnKeys().equals(that.getColumnKeys())) {
  347. return false;
  348. }
  349. int rowCount = getRowCount();
  350. int colCount = getColumnCount();
  351. for (int r = 0; r < rowCount; r++) {
  352. for (int c = 0; c < colCount; c++) {
  353. Number v1 = getValue(r, c);
  354. Number v2 = that.getValue(r, c);
  355. if (v1 == null) {
  356. if (v2 != null) {
  357. return false;
  358. }
  359. }
  360. else if (!v1.equals(v2)) {
  361. return false;
  362. }
  363. }
  364. }
  365. return true;
  366. }
  367. /**
  368. * Returns a hash code for the dataset.
  369. *
  370. * @return A hash code.
  371. */
  372. public int hashCode() {
  373. return this.data.hashCode();
  374. }
  375. /**
  376. * Returns a clone of the dataset.
  377. *
  378. * @return A clone.
  379. *
  380. * @throws CloneNotSupportedException if there is a problem cloning the
  381. * dataset.
  382. */
  383. public Object clone() throws CloneNotSupportedException {
  384. TestIntervalCategoryDataset clone = (TestIntervalCategoryDataset)
  385. super.clone();
  386. clone.data = (KeyedObjects2D) this.data.clone();
  387. return clone;
  388. }
  389. public Number getStartValue(int series, int category) {
  390. IntervalDataItem item = (IntervalDataItem) this.data.getObject(series,
  391. category);
  392. if (item == null) {
  393. return null;
  394. }
  395. return item.getLowerBound();
  396. }
  397. public Number getStartValue(Comparable series, Comparable category) {
  398. IntervalDataItem item = (IntervalDataItem) this.data.getObject(series,
  399. category);
  400. if (item == null) {
  401. return null;
  402. }
  403. return item.getLowerBound();
  404. }
  405. public Number getEndValue(int series, int category) {
  406. IntervalDataItem item = (IntervalDataItem) this.data.getObject(series,
  407. category);
  408. if (item == null) {
  409. return null;
  410. }
  411. return item.getUpperBound();
  412. }
  413. public Number getEndValue(Comparable series, Comparable category) {
  414. IntervalDataItem item = (IntervalDataItem) this.data.getObject(series,
  415. category);
  416. if (item == null) {
  417. return null;
  418. }
  419. return item.getUpperBound();
  420. }
  421. }