001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 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 048package org.jfree.data.xy; 049 050import java.io.Serializable; 051 052import 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 */ 058public 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} 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} 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 return get(i, j); 106 } 107 108 109 /** 110 * Returns the column of the specified item. 111 * 112 * @param itemIndex the index of the item. 113 * 114 * @return The column of the specified item. 115 */ 116 public int getItemColumn(int itemIndex) { 117 //assert itemIndex >= 0 && itemIndex < getItemCount(); 118 return itemIndex % getColumnsCount(); 119 } 120 121 122 /** 123 * Returns the number of items in the series. 124 * 125 * @return The item count. 126 */ 127 @Override 128 public int getItemCount() { 129 return getRowCount() * getColumnsCount(); 130 } 131 132 133 /** 134 * Returns the row of the specified item. 135 * 136 * @param itemIndex the index of the item. 137 * 138 * @return The row of the specified item. 139 */ 140 public int getItemRow(int itemIndex) { 141 //assert itemIndex >= 0 && itemIndex < getItemCount(); 142 return itemIndex / getColumnsCount(); 143 } 144 145 146 /** 147 * Returns the number of rows in this matrix series. 148 * 149 * @return The number of rows in this matrix series. 150 */ 151 public int getRowCount() { 152 return this.data.length; 153 } 154 155 156 /** 157 * Returns the value of the specified item in this matrix series. 158 * 159 * @param i the row of the item. 160 * @param j the column of the item. 161 * 162 * @return The value of the specified item in this matrix series. 163 * 164 * @see #getItem(int) 165 * @see #update(int, int, double) 166 */ 167 public double get(int i, int j) { 168 return this.data[i][j]; 169 } 170 171 172 /** 173 * Updates the value of the specified item in this matrix series. 174 * 175 * @param i the row of the item. 176 * @param j the column of the item. 177 * @param mij the new value for the item. 178 * 179 * @see #get(int, int) 180 */ 181 public void update(int i, int j, double mij) { 182 this.data[i][j] = mij; 183 fireSeriesChanged(); 184 } 185 186 187 /** 188 * Sets all matrix values to zero and sends a 189 * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 190 * listeners. 191 */ 192 public void zeroAll() { 193 int rows = getRowCount(); 194 int columns = getColumnsCount(); 195 196 for (int row = 0; row < rows; row++) { 197 for (int column = 0; column < columns; column++) { 198 this.data[row][column] = 0.0; 199 } 200 } 201 fireSeriesChanged(); 202 } 203 204 /** 205 * Tests this object instance for equality with an arbitrary object. 206 * 207 * @param obj the object ({@code null} permitted). 208 * 209 * @return A boolean. 210 */ 211 @Override 212 public boolean equals(Object obj) { 213 if (obj == this) { 214 return true; 215 } 216 if (!(obj instanceof MatrixSeries)) { 217 return false; 218 } 219 MatrixSeries that = (MatrixSeries) obj; 220 if (!(getRowCount() == that.getRowCount())) { 221 return false; 222 } 223 if (!(getColumnsCount() == that.getColumnsCount())) { 224 return false; 225 } 226 for (int r = 0; r < getRowCount(); r++) { 227 for (int c = 0; c < getColumnsCount(); c++) { 228 if (get(r, c) != that.get(r, c)) { 229 return false; 230 } 231 } 232 } 233 return super.equals(obj); 234 } 235 236}