    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2005, 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-2005, 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     * $Id: MatrixSeries.java,v 1.5.2.1 2005/10/25 21:36:51 mungady Exp $
    037     *
    038     * Changes
    039     * -------
    040     * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
    041     * 10-Feb-2004 : Fixed Checkstyle complaints (DG);
    042     * 21-May-2004 : Fixed bug 940188 - problem in getItemColumn() and
    043     *               getItemRow() (DG);
    044     *
    045     */
    046
    047    package org.jfree.data.xy;
    048
    049    import java.io.Serializable;
    050
    051    import org.jfree.data.general.Series;
    052
    053    /**
    054     * Represents a dense matrix M[i,j] where each Mij item of the matrix has a
    055     * value (default is 0).
    056     *
    057     * @author Barak Naveh
    058     */
    059    public class MatrixSeries extends Series implements Serializable {
    060
    061        /** For serialization. */
    062        private static final long serialVersionUID = 7934188527308315704L;
    063
    064        /** Series matrix values */
    065        protected double[][] data;
    066
    067        /**
    068         * Constructs a new matrix series.
    069         * <p>
    070         * By default, all matrix items are initialzed to 0.
    071         * </p>
    072         *
    073         * @param name  series name (<code>null</code> not permitted).
    074         * @param rows  the number of rows.
    075         * @param columns  the number of columns.
    076         */
    077        public MatrixSeries(String name, int rows, int columns) {
    078            super(name);
    079            this.data = new double[rows][columns];
    080            zeroAll();
    081        }
    082
    083        /**
    084         * Returns the number of columns in this matrix series.
    085         *
    086         * @return The number of columns in this matrix series.
    087         */
    088        public int getColumnsCount() {
    089            return this.data[0].length;
    090        }
    091
    092
    093        /**
    094         * Return the matrix item at the specified index.
    095         *
    096         * @param itemIndex item index.
    097         *
    098         * @return The matrix item at the specified index.
    099         */
    100        public Number getItem(int itemIndex) {
    101            int i = getItemRow(itemIndex);
    102            int j = getItemColumn(itemIndex);
    103
    104            Number n = new Double(get(i, j));
    105
    106            return n;
    107        }
    108
    109
    110        /**
    111         * Returns the column of the specified item.
    112         *
    113         * @param itemIndex the index of the item.
    114         *
    115         * @return The column of the specified item.
    116         */
    117        public int getItemColumn(int itemIndex) {
    118            //assert itemIndex >= 0 && itemIndex < getItemCount();
    119            return itemIndex % getColumnsCount();
    120        }
    121
    122
    123        /**
    124         * Returns the number of items in the series.
    125         *
    126         * @return The item count.
    127         */
    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        public double get(int i, int j) {
    165            return this.data[i][j];
    166        }
    167
    168
    169        /**
    170         * Updates the value of the specified item in this matrix series.
    171         *
    172         * @param i the row of the item.
    173         * @param j the column of the item.
    174         * @param mij the new value for the item.
    175         */
    176        public void update(int i, int j, double mij) {
    177            this.data[i][j] = mij;
    178            fireSeriesChanged();
    179        }
    180
    181
    182        /**
    183         * Sets all matrix values to zero and sends a
    184         * {@link org.jfree.data.general.SeriesChangeEvent} to all registered
    185         * listeners.
    186         */
    187        public void zeroAll() {
    188            int rows = getRowCount();
    189            int columns = getColumnsCount();
    190
    191            for (int row = 0; row < rows; row++) {
    192                for (int column = 0; column < columns; column++) {
    193                    this.data[row][column] = 0.0;
    194                }
    195            }
    196            fireSeriesChanged();
    197        }
    198
    199        /**
    200         * Tests this object instance for equality with an arbitrary object.
    201         *
    202         * @param obj  the object (<code>null</code> permitted).
    203         *
    204         * @return A boolean.
    205         */
    206        public boolean equals(Object obj) {
    207            if (obj == this) {
    208                return true;
    209            }
    210            if (obj instanceof MatrixSeries && super.equals(obj)) {
    211                MatrixSeries m = (MatrixSeries) obj;
    212                if (!(getRowCount() == m.getRowCount())) {
    213                    return false;
    214                }
    215                if (!(getColumnsCount() == m.getColumnsCount())) {
    216                    return false;
    217                }
    218                return true;
    219            }
    220            return false;
    221        }
    222
    223    }