Frames | No Frames |
1: /* =========================================================== 2: * JFreeChart : a free chart library for the Java(tm) platform 3: * =========================================================== 4: * 5: * (C) Copyright 2000-2007, 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: * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 25: * in the United States and other countries.] 26: * 27: * ----------------- 28: * MatrixSeries.java 29: * ----------------- 30: * (C) Copyright 2003-2007, by Barak Naveh and Contributors. 31: * 32: * Original Author: Barak Naveh;; 33: * Contributor(s): David Gilbert (for Object Refinery Limited); 34: * Zhitao Wang; 35: * 36: * $Id: MatrixSeries.java,v 1.5.2.3 2007/02/02 15:14:53 mungady Exp $ 37: * 38: * Changes 39: * ------- 40: * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG); 41: * 10-Feb-2004 : Fixed Checkstyle complaints (DG); 42: * 21-May-2004 : Fixed bug 940188 - problem in getItemColumn() and 43: * getItemRow() (DG); 44: * ------------- JFREECHART 1.0.x --------------------------------------------- 45: * 27-Nov-2006 : Fixed bug in equals() method (DG); 46: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG); 47: * 48: */ 49: 50: package org.jfree.data.xy; 51: 52: import java.io.Serializable; 53: 54: import org.jfree.data.general.Series; 55: 56: /** 57: * Represents a dense matrix M[i,j] where each Mij item of the matrix has a 58: * value (default is 0). 59: */ 60: public class MatrixSeries extends Series implements Serializable { 61: 62: /** For serialization. */ 63: private static final long serialVersionUID = 7934188527308315704L; 64: 65: /** Series matrix values */ 66: protected double[][] data; 67: 68: /** 69: * Constructs a new matrix series. 70: * <p> 71: * By default, all matrix items are initialzed to 0. 72: * </p> 73: * 74: * @param name series name (<code>null</code> not permitted). 75: * @param rows the number of rows. 76: * @param columns the number of columns. 77: */ 78: public MatrixSeries(String name, int rows, int columns) { 79: super(name); 80: this.data = new double[rows][columns]; 81: zeroAll(); 82: } 83: 84: /** 85: * Returns the number of columns in this matrix series. 86: * 87: * @return The number of columns in this matrix series. 88: */ 89: public int getColumnsCount() { 90: return this.data[0].length; 91: } 92: 93: 94: /** 95: * Return the matrix item at the specified index. Note that this method 96: * creates a new <code>Double</code> instance every time it is called. 97: * 98: * @param itemIndex item index. 99: * 100: * @return The matrix item at the specified index. 101: * 102: * @see #get(int, int) 103: */ 104: public Number getItem(int itemIndex) { 105: int i = getItemRow(itemIndex); 106: int j = getItemColumn(itemIndex); 107: 108: Number n = new Double(get(i, j)); 109: 110: return n; 111: } 112: 113: 114: /** 115: * Returns the column of the specified item. 116: * 117: * @param itemIndex the index of the item. 118: * 119: * @return The column of the specified item. 120: */ 121: public int getItemColumn(int itemIndex) { 122: //assert itemIndex >= 0 && itemIndex < getItemCount(); 123: return itemIndex % getColumnsCount(); 124: } 125: 126: 127: /** 128: * Returns the number of items in the series. 129: * 130: * @return The item count. 131: */ 132: public int getItemCount() { 133: return getRowCount() * getColumnsCount(); 134: } 135: 136: 137: /** 138: * Returns the row of the specified item. 139: * 140: * @param itemIndex the index of the item. 141: * 142: * @return The row of the specified item. 143: */ 144: public int getItemRow(int itemIndex) { 145: //assert itemIndex >= 0 && itemIndex < getItemCount(); 146: return itemIndex / getColumnsCount(); 147: } 148: 149: 150: /** 151: * Returns the number of rows in this matrix series. 152: * 153: * @return The number of rows in this matrix series. 154: */ 155: public int getRowCount() { 156: return this.data.length; 157: } 158: 159: 160: /** 161: * Returns the value of the specified item in this matrix series. 162: * 163: * @param i the row of the item. 164: * @param j the column of the item. 165: * 166: * @return The value of the specified item in this matrix series. 167: * 168: * @see #getItem(int) 169: * @see #update(int, int, double) 170: */ 171: public double get(int i, int j) { 172: return this.data[i][j]; 173: } 174: 175: 176: /** 177: * Updates the value of the specified item in this matrix series. 178: * 179: * @param i the row of the item. 180: * @param j the column of the item. 181: * @param mij the new value for the item. 182: * 183: * @see #get(int, int) 184: */ 185: public void update(int i, int j, double mij) { 186: this.data[i][j] = mij; 187: fireSeriesChanged(); 188: } 189: 190: 191: /** 192: * Sets all matrix values to zero and sends a 193: * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 194: * listeners. 195: */ 196: public void zeroAll() { 197: int rows = getRowCount(); 198: int columns = getColumnsCount(); 199: 200: for (int row = 0; row < rows; row++) { 201: for (int column = 0; column < columns; column++) { 202: this.data[row][column] = 0.0; 203: } 204: } 205: fireSeriesChanged(); 206: } 207: 208: /** 209: * Tests this object instance for equality with an arbitrary object. 210: * 211: * @param obj the object (<code>null</code> permitted). 212: * 213: * @return A boolean. 214: */ 215: public boolean equals(Object obj) { 216: if (obj == this) { 217: return true; 218: } 219: if (!(obj instanceof MatrixSeries)) { 220: return false; 221: } 222: MatrixSeries that = (MatrixSeries) obj; 223: if (!(getRowCount() == that.getRowCount())) { 224: return false; 225: } 226: if (!(getColumnsCount() == that.getColumnsCount())) { 227: return false; 228: } 229: for (int r = 0; r < getRowCount(); r++) { 230: for (int c = 0; c < getColumnsCount(); c++) { 231: if (get(r, c) != that.get(r, c)) { 232: return false; 233: } 234: } 235: } 236: return super.equals(obj); 237: } 238: 239: }