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. * MatrixSeriesCollection.java
  26. * ---------------------------
  27. * (C) Copyright 2003-2005, by Barak Naveh and Contributors.
  28. *
  29. * Original Author: Barak Naveh;;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: MatrixSeriesCollection.java,v 1.3 2005/01/11 16:42:25 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
  37. * 05-May-2004 : Now extends AbstractXYZDataset (DG);
  38. * 15-Jul-2004 : Switched getZ() and getZValue() methods (DG);
  39. *
  40. */
  41. package org.jfree.data.xy;
  42. import java.io.Serializable;
  43. import java.util.List;
  44. import org.jfree.util.ObjectUtilities;
  45. /**
  46. * Represents a collection of {@link MatrixSeries} that can be used as a dataset.
  47. *
  48. * @author Barak Naveh
  49. *
  50. * @see org.jfree.data.xy.MatrixSeries
  51. */
  52. public class MatrixSeriesCollection extends AbstractXYZDataset
  53. implements XYZDataset, Serializable {
  54. /** The series that are included in the collection. */
  55. private List seriesList;
  56. /**
  57. * Constructs an empty dataset.
  58. */
  59. public MatrixSeriesCollection() {
  60. this(null);
  61. }
  62. /**
  63. * Constructs a dataset and populates it with a single matrix series.
  64. *
  65. * @param series the time series.
  66. */
  67. public MatrixSeriesCollection(MatrixSeries series) {
  68. this.seriesList = new java.util.ArrayList();
  69. if (series != null) {
  70. this.seriesList.add(series);
  71. series.addChangeListener(this);
  72. }
  73. }
  74. /**
  75. * Returns the number of items in the specified series.
  76. *
  77. * @param seriesIndex zero-based series index.
  78. *
  79. * @return The number of items in the specified series.
  80. */
  81. public int getItemCount(int seriesIndex) {
  82. return getSeries(seriesIndex).getItemCount();
  83. }
  84. /**
  85. * Returns the series having the specified index.
  86. *
  87. * @param seriesIndex zero-based series index.
  88. *
  89. * @return The series.
  90. *
  91. * @throws IllegalArgumentException
  92. */
  93. public MatrixSeries getSeries(int seriesIndex) {
  94. if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) {
  95. throw new IllegalArgumentException("Index outside valid range.");
  96. }
  97. MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex);
  98. return series;
  99. }
  100. /**
  101. * Returns the number of series in the collection.
  102. *
  103. * @return The number of series in the collection.
  104. */
  105. public int getSeriesCount() {
  106. return this.seriesList.size();
  107. }
  108. /**
  109. * Returns the name of a series.
  110. *
  111. * @param seriesIndex zero-based series index.
  112. *
  113. * @return The name of a series.
  114. */
  115. public String getSeriesName(int seriesIndex) {
  116. return getSeries(seriesIndex).getName();
  117. }
  118. /**
  119. * Returns the j index value of the specified Mij matrix item in the
  120. * specified matrix series.
  121. *
  122. * @param seriesIndex zero-based series index.
  123. * @param itemIndex zero-based item index.
  124. *
  125. * @return The j index value for the specified matrix item.
  126. *
  127. * @see org.jfree.data.xy.XYDataset#getXValue(int, int)
  128. */
  129. public Number getX(int seriesIndex, int itemIndex) {
  130. MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex);
  131. int x = series.getItemColumn(itemIndex);
  132. return new Integer(x); // I know it's bad to create object. better idea?
  133. }
  134. /**
  135. * Returns the i index value of the specified Mij matrix item in the
  136. * specified matrix series.
  137. *
  138. * @param seriesIndex zero-based series index.
  139. * @param itemIndex zero-based item index.
  140. *
  141. * @return The i index value for the specified matrix item.
  142. *
  143. * @see org.jfree.data.xy.XYDataset#getYValue(int, int)
  144. */
  145. public Number getY(int seriesIndex, int itemIndex) {
  146. MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex);
  147. int y = series.getItemRow(itemIndex);
  148. return new Integer(y); // I know it's bad to create object. better idea?
  149. }
  150. /**
  151. * Returns the Mij item value of the specified Mij matrix item in the
  152. * specified matrix series.
  153. *
  154. * @param seriesIndex the series (zero-based index).
  155. * @param itemIndex zero-based item index.
  156. *
  157. * @return the Mij item value for the specified matrix item.
  158. *
  159. * @see org.jfree.data.xy.XYZDataset#getZValue(int, int)
  160. */
  161. public Number getZ(int seriesIndex, int itemIndex) {
  162. MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex);
  163. Number z = series.getItem(itemIndex);
  164. return z;
  165. }
  166. /**
  167. * Adds a series to the collection.
  168. * <P>
  169. * Notifies all registered listeners that the dataset has changed.
  170. * </p>
  171. *
  172. * @param series the series.
  173. *
  174. * @throws IllegalArgumentException
  175. */
  176. public void addSeries(MatrixSeries series) {
  177. // check arguments...
  178. if (series == null) {
  179. throw new IllegalArgumentException("Cannot add null series.");
  180. }
  181. // add the series...
  182. this.seriesList.add(series);
  183. series.addChangeListener(this);
  184. fireDatasetChanged();
  185. }
  186. /**
  187. * Tests this collection for equality with an arbitrary object.
  188. *
  189. * @param obj the object.
  190. *
  191. * @return A boolean.
  192. */
  193. public boolean equals(Object obj) {
  194. if (obj == null) {
  195. return false;
  196. }
  197. if (obj == this) {
  198. return true;
  199. }
  200. if (obj instanceof MatrixSeriesCollection) {
  201. MatrixSeriesCollection c = (MatrixSeriesCollection) obj;
  202. return ObjectUtilities.equal(this.seriesList, c.seriesList);
  203. }
  204. return false;
  205. }
  206. /**
  207. * Returns a hash code.
  208. *
  209. * @return A hash code.
  210. */
  211. public int hashCode() {
  212. return (this.seriesList != null ? this.seriesList.hashCode() : 0);
  213. }
  214. /**
  215. * Removes all the series from the collection.
  216. * <P>
  217. * Notifies all registered listeners that the dataset has changed.
  218. * </p>
  219. */
  220. public void removeAllSeries() {
  221. // Unregister the collection as a change listener to each series inmthe collection.
  222. for (int i = 0; i < this.seriesList.size(); i++) {
  223. MatrixSeries series = (MatrixSeries) this.seriesList.get(i);
  224. series.removeChangeListener(this);
  225. }
  226. // Remove all the series from the collection and notify listeners.
  227. this.seriesList.clear();
  228. fireDatasetChanged();
  229. }
  230. /**
  231. * Removes a series from the collection.
  232. * <P>
  233. * Notifies all registered listeners that the dataset has changed.
  234. * </p>
  235. *
  236. * @param series the series.
  237. *
  238. * @throws IllegalArgumentException
  239. */
  240. public void removeSeries(MatrixSeries series) {
  241. // check arguments...
  242. if (series == null) {
  243. throw new IllegalArgumentException("Cannot remove null series.");
  244. }
  245. // remove the series...
  246. if (this.seriesList.contains(series)) {
  247. series.removeChangeListener(this);
  248. this.seriesList.remove(series);
  249. fireDatasetChanged();
  250. }
  251. }
  252. /**
  253. * Removes a series from the collection.
  254. * <P>
  255. * Notifies all registered listeners that the dataset has changed.
  256. * </p>
  257. *
  258. * @param seriesIndex the series (zero based index).
  259. *
  260. * @throws IllegalArgumentException
  261. */
  262. public void removeSeries(int seriesIndex) {
  263. // check arguments...
  264. if ((seriesIndex < 0) || (seriesIndex > getSeriesCount())) {
  265. throw new IllegalArgumentException("Index outside valid range.");
  266. }
  267. // fetch the series, remove the change listener, then remove the series.
  268. MatrixSeries series = (MatrixSeries) this.seriesList.get(seriesIndex);
  269. series.removeChangeListener(this);
  270. this.seriesList.remove(seriesIndex);
  271. fireDatasetChanged();
  272. }
  273. }