001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ----------------- 028 * MatrixSeries.java 029 * ----------------- 030 * (C) Copyright 2003-2008, by Barak Naveh and Contributors. 031 * 032 * Original Author: Barak Naveh; 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * Zhitao Wang; 035 * 036 * Changes 037 * ------- 038 * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG); 039 * 10-Feb-2004 : Fixed Checkstyle complaints (DG); 040 * 21-May-2004 : Fixed bug 940188 - problem in getItemColumn() and 041 * getItemRow() (DG); 042 * ------------- JFREECHART 1.0.x --------------------------------------------- 043 * 27-Nov-2006 : Fixed bug in equals() method (DG); 044 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG); 045 * 046 */ 047 048 package org.jfree.data.xy; 049 050 import java.io.Serializable; 051 052 import org.jfree.data.general.Series; 053 054 /** 055 * Represents a dense matrix M[i,j] where each Mij item of the matrix has a 056 * value (default is 0). 057 */ 058 public class MatrixSeries extends Series implements Serializable { 059 060 /** For serialization. */ 061 private static final long serialVersionUID = 7934188527308315704L; 062 063 /** Series matrix values */ 064 protected double[][] data; 065 066 /** 067 * Constructs a new matrix series. 068 * <p> 069 * By default, all matrix items are initialzed to 0. 070 * </p> 071 * 072 * @param name series name (<code>null</code> not permitted). 073 * @param rows the number of rows. 074 * @param columns the number of columns. 075 */ 076 public MatrixSeries(String name, int rows, int columns) { 077 super(name); 078 this.data = new double[rows][columns]; 079 zeroAll(); 080 } 081 082 /** 083 * Returns the number of columns in this matrix series. 084 * 085 * @return The number of columns in this matrix series. 086 */ 087 public int getColumnsCount() { 088 return this.data[0].length; 089 } 090 091 092 /** 093 * Return the matrix item at the specified index. Note that this method 094 * creates a new <code>Double</code> instance every time it is called. 095 * 096 * @param itemIndex item index. 097 * 098 * @return The matrix item at the specified index. 099 * 100 * @see #get(int, int) 101 */ 102 public Number getItem(int itemIndex) { 103 int i = getItemRow(itemIndex); 104 int j = getItemColumn(itemIndex); 105 106 Number n = new Double(get(i, j)); 107 108 return n; 109 } 110 111 112 /** 113 * Returns the column of the specified item. 114 * 115 * @param itemIndex the index of the item. 116 * 117 * @return The column of the specified item. 118 */ 119 public int getItemColumn(int itemIndex) { 120 //assert itemIndex >= 0 && itemIndex < getItemCount(); 121 return itemIndex % getColumnsCount(); 122 } 123 124 125 /** 126 * Returns the number of items in the series. 127 * 128 * @return The item count. 129 */ 130 public int getItemCount() { 131 return getRowCount() * getColumnsCount(); 132 } 133 134 135 /** 136 * Returns the row of the specified item. 137 * 138 * @param itemIndex the index of the item. 139 * 140 * @return The row of the specified item. 141 */ 142 public int getItemRow(int itemIndex) { 143 //assert itemIndex >= 0 && itemIndex < getItemCount(); 144 return itemIndex / getColumnsCount(); 145 } 146 147 148 /** 149 * Returns the number of rows in this matrix series. 150 * 151 * @return The number of rows in this matrix series. 152 */ 153 public int getRowCount() { 154 return this.data.length; 155 } 156 157 158 /** 159 * Returns the value of the specified item in this matrix series. 160 * 161 * @param i the row of the item. 162 * @param j the column of the item. 163 * 164 * @return The value of the specified item in this matrix series. 165 * 166 * @see #getItem(int) 167 * @see #update(int, int, double) 168 */ 169 public double get(int i, int j) { 170 return this.data[i][j]; 171 } 172 173 174 /** 175 * Updates the value of the specified item in this matrix series. 176 * 177 * @param i the row of the item. 178 * @param j the column of the item. 179 * @param mij the new value for the item. 180 * 181 * @see #get(int, int) 182 */ 183 public void update(int i, int j, double mij) { 184 this.data[i][j] = mij; 185 fireSeriesChanged(); 186 } 187 188 189 /** 190 * Sets all matrix values to zero and sends a 191 * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 192 * listeners. 193 */ 194 public void zeroAll() { 195 int rows = getRowCount(); 196 int columns = getColumnsCount(); 197 198 for (int row = 0; row < rows; row++) { 199 for (int column = 0; column < columns; column++) { 200 this.data[row][column] = 0.0; 201 } 202 } 203 fireSeriesChanged(); 204 } 205 206 /** 207 * Tests this object instance for equality with an arbitrary object. 208 * 209 * @param obj the object (<code>null</code> permitted). 210 * 211 * @return A boolean. 212 */ 213 public boolean equals(Object obj) { 214 if (obj == this) { 215 return true; 216 } 217 if (!(obj instanceof MatrixSeries)) { 218 return false; 219 } 220 MatrixSeries that = (MatrixSeries) obj; 221 if (!(getRowCount() == that.getRowCount())) { 222 return false; 223 } 224 if (!(getColumnsCount() == that.getColumnsCount())) { 225 return false; 226 } 227 for (int r = 0; r < getRowCount(); r++) { 228 for (int c = 0; c < getColumnsCount(); c++) { 229 if (get(r, c) != that.get(r, c)) { 230 return false; 231 } 232 } 233 } 234 return super.equals(obj); 235 } 236 237 }