001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-present, by David Gilbert 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-present, by Barak Naveh and Contributors. 031 * 032 * Original Author: Barak Naveh; 033 * Contributor(s): David Gilbert; 034 * Zhitao Wang; 035 * 036 */ 037 038package org.jfree.data.xy; 039 040import java.io.Serializable; 041 042import org.jfree.data.general.Series; 043 044/** 045 * Represents a dense matrix M[i,j] where each Mij item of the matrix has a 046 * value (default is 0). 047 */ 048public class MatrixSeries extends Series implements Serializable { 049 050 /** For serialization. */ 051 private static final long serialVersionUID = 7934188527308315704L; 052 053 /** Series matrix values */ 054 protected double[][] data; 055 056 /** 057 * Constructs a new matrix series. 058 * <p> 059 * By default, all matrix items are initialzed to 0. 060 * </p> 061 * 062 * @param name series name ({@code null} not permitted). 063 * @param rows the number of rows. 064 * @param columns the number of columns. 065 */ 066 public MatrixSeries(String name, int rows, int columns) { 067 super(name); 068 this.data = new double[rows][columns]; 069 zeroAll(); 070 } 071 072 /** 073 * Returns the number of columns in this matrix series. 074 * 075 * @return The number of columns in this matrix series. 076 */ 077 public int getColumnsCount() { 078 return this.data[0].length; 079 } 080 081 082 /** 083 * Return the matrix item at the specified index. Note that this method 084 * creates a new {@code double} instance every time it is called. 085 * 086 * @param itemIndex item index. 087 * 088 * @return The matrix item at the specified index. 089 * 090 * @see #get(int, int) 091 */ 092 public Number getItem(int itemIndex) { 093 int i = getItemRow(itemIndex); 094 int j = getItemColumn(itemIndex); 095 return get(i, j); 096 } 097 098 099 /** 100 * Returns the column of the specified item. 101 * 102 * @param itemIndex the index of the item. 103 * 104 * @return The column of the specified item. 105 */ 106 public int getItemColumn(int itemIndex) { 107 //assert itemIndex >= 0 && itemIndex < getItemCount(); 108 return itemIndex % getColumnsCount(); 109 } 110 111 112 /** 113 * Returns the number of items in the series. 114 * 115 * @return The item count. 116 */ 117 @Override 118 public int getItemCount() { 119 return getRowCount() * getColumnsCount(); 120 } 121 122 123 /** 124 * Returns the row of the specified item. 125 * 126 * @param itemIndex the index of the item. 127 * 128 * @return The row of the specified item. 129 */ 130 public int getItemRow(int itemIndex) { 131 //assert itemIndex >= 0 && itemIndex < getItemCount(); 132 return itemIndex / getColumnsCount(); 133 } 134 135 136 /** 137 * Returns the number of rows in this matrix series. 138 * 139 * @return The number of rows in this matrix series. 140 */ 141 public int getRowCount() { 142 return this.data.length; 143 } 144 145 146 /** 147 * Returns the value of the specified item in this matrix series. 148 * 149 * @param i the row of the item. 150 * @param j the column of the item. 151 * 152 * @return The value of the specified item in this matrix series. 153 * 154 * @see #getItem(int) 155 * @see #update(int, int, double) 156 */ 157 public double get(int i, int j) { 158 return this.data[i][j]; 159 } 160 161 162 /** 163 * Updates the value of the specified item in this matrix series. 164 * 165 * @param i the row of the item. 166 * @param j the column of the item. 167 * @param mij the new value for the item. 168 * 169 * @see #get(int, int) 170 */ 171 public void update(int i, int j, double mij) { 172 this.data[i][j] = mij; 173 fireSeriesChanged(); 174 } 175 176 177 /** 178 * Sets all matrix values to zero and sends a 179 * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 180 * listeners. 181 */ 182 public void zeroAll() { 183 int rows = getRowCount(); 184 int columns = getColumnsCount(); 185 186 for (int row = 0; row < rows; row++) { 187 for (int column = 0; column < columns; column++) { 188 this.data[row][column] = 0.0; 189 } 190 } 191 fireSeriesChanged(); 192 } 193 194 /** 195 * Tests this object instance for equality with an arbitrary object. 196 * 197 * @param obj the object ({@code null} permitted). 198 * 199 * @return A boolean. 200 */ 201 @Override 202 public boolean equals(Object obj) { 203 if (obj == this) { 204 return true; 205 } 206 if (!(obj instanceof MatrixSeries)) { 207 return false; 208 } 209 MatrixSeries that = (MatrixSeries) obj; 210 if (!(getRowCount() == that.getRowCount())) { 211 return false; 212 } 213 if (!(getColumnsCount() == that.getColumnsCount())) { 214 return false; 215 } 216 for (int r = 0; r < getRowCount(); r++) { 217 for (int c = 0; c < getColumnsCount(); c++) { 218 if (get(r, c) != that.get(r, c)) { 219 return false; 220 } 221 } 222 } 223 return super.equals(obj); 224 } 225 226}