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</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    @Override
131    public int getItemCount() {
132        return getRowCount() * getColumnsCount();
133    }
134
135
136    /**
137     * Returns the row of the specified item.
138     *
139     * @param itemIndex the index of the item.
140     *
141     * @return The row of the specified item.
142     */
143    public int getItemRow(int itemIndex) {
144        //assert itemIndex >= 0 && itemIndex < getItemCount();
145        return itemIndex / getColumnsCount();
146    }
147
148
149    /**
150     * Returns the number of rows in this matrix series.
151     *
152     * @return The number of rows in this matrix series.
153     */
154    public int getRowCount() {
155        return this.data.length;
156    }
157
158
159    /**
160     * Returns the value of the specified item in this matrix series.
161     *
162     * @param i the row of the item.
163     * @param j the column of the item.
164     *
165     * @return The value of the specified item in this matrix series.
166     *
167     * @see #getItem(int)
168     * @see #update(int, int, double)
169     */
170    public double get(int i, int j) {
171        return this.data[i][j];
172    }
173
174
175    /**
176     * Updates the value of the specified item in this matrix series.
177     *
178     * @param i the row of the item.
179     * @param j the column of the item.
180     * @param mij the new value for the item.
181     *
182     * @see #get(int, int)
183     */
184    public void update(int i, int j, double mij) {
185        this.data[i][j] = mij;
186        fireSeriesChanged();
187    }
188
189
190    /**
191     * Sets all matrix values to zero and sends a
192     * {@link org.jfree.data.general.SeriesChangeEvent} to all registered
193     * listeners.
194     */
195    public void zeroAll() {
196        int rows = getRowCount();
197        int columns = getColumnsCount();
198
199        for (int row = 0; row < rows; row++) {
200            for (int column = 0; column < columns; column++) {
201                this.data[row][column] = 0.0;
202            }
203        }
204        fireSeriesChanged();
205    }
206
207    /**
208     * Tests this object instance for equality with an arbitrary object.
209     *
210     * @param obj  the object (<code>null</code> permitted).
211     *
212     * @return A boolean.
213     */
214    @Override
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}