Save This Page
Home » jcommon-1.0.13 » org.jfree » data » xy » [javadoc | source]
    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    * Changes
   37    * -------
   38    * 10-Jul-2003 : Version 1 contributed by Barak Naveh (DG);
   39    * 10-Feb-2004 : Fixed Checkstyle complaints (DG);
   40    * 21-May-2004 : Fixed bug 940188 - problem in getItemColumn() and 
   41    *               getItemRow() (DG);
   42    * ------------- JFREECHART 1.0.x ---------------------------------------------
   43    * 27-Nov-2006 : Fixed bug in equals() method (DG);
   44    * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
   45    *
   46    */
   47   
   48   package org.jfree.data.xy;
   49   
   50   import java.io.Serializable;
   51   
   52   import org.jfree.data.general.Series;
   53   
   54   /**
   55    * Represents a dense matrix M[i,j] where each Mij item of the matrix has a
   56    * value (default is 0).
   57    */
   58   public class MatrixSeries extends Series implements Serializable {
   59       
   60       /** For serialization. */
   61       private static final long serialVersionUID = 7934188527308315704L;    
   62       
   63       /** Series matrix values */
   64       protected double[][] data;
   65   
   66       /**
   67        * Constructs a new matrix series.
   68        * <p>
   69        * By default, all matrix items are initialzed to 0.
   70        * </p>
   71        *
   72        * @param name  series name (<code>null</code> not permitted).
   73        * @param rows  the number of rows.
   74        * @param columns  the number of columns.
   75        */
   76       public MatrixSeries(String name, int rows, int columns) {
   77           super(name);
   78           this.data = new double[rows][columns];
   79           zeroAll();
   80       }
   81   
   82       /**
   83        * Returns the number of columns in this matrix series.
   84        *
   85        * @return The number of columns in this matrix series.
   86        */
   87       public int getColumnsCount() {
   88           return this.data[0].length;
   89       }
   90   
   91   
   92       /**
   93        * Return the matrix item at the specified index.  Note that this method
   94        * creates a new <code>Double</code> instance every time it is called.
   95        *
   96        * @param itemIndex item index.
   97        *
   98        * @return The matrix item at the specified index.
   99        * 
  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       public int getItemCount() {
  131           return getRowCount() * getColumnsCount();
  132       }
  133   
  134   
  135       /**
  136        * Returns the row of the specified item.
  137        *
  138        * @param itemIndex the index of the item.
  139        *
  140        * @return The row of the specified item.
  141        */
  142       public int getItemRow(int itemIndex) {
  143           //assert itemIndex >= 0 && itemIndex < getItemCount();
  144           return itemIndex / getColumnsCount();
  145       }
  146   
  147   
  148       /**
  149        * Returns the number of rows in this matrix series.
  150        *
  151        * @return The number of rows in this matrix series.
  152        */
  153       public int getRowCount() {
  154           return this.data.length;
  155       }
  156   
  157   
  158       /**
  159        * Returns the value of the specified item in this matrix series.
  160        *
  161        * @param i the row of the item.
  162        * @param j the column of the item.
  163        *
  164        * @return The value of the specified item in this matrix series.
  165        * 
  166        * @see #getItem(int)
  167        * @see #update(int, int, double)
  168        */
  169       public double get(int i, int j) {
  170           return this.data[i][j];
  171       }
  172   
  173   
  174       /**
  175        * Updates the value of the specified item in this matrix series.
  176        *
  177        * @param i the row of the item.
  178        * @param j the column of the item.
  179        * @param mij the new value for the item.
  180        * 
  181        * @see #get(int, int)
  182        */
  183       public void update(int i, int j, double mij) {
  184           this.data[i][j] = mij;
  185           fireSeriesChanged();
  186       }
  187   
  188   
  189       /**
  190        * Sets all matrix values to zero and sends a 
  191        * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 
  192        * listeners.
  193        */
  194       public void zeroAll() {
  195           int rows = getRowCount();
  196           int columns = getColumnsCount();
  197   
  198           for (int row = 0; row < rows; row++) {
  199               for (int column = 0; column < columns; column++) {
  200                   this.data[row][column] = 0.0;
  201               }
  202           }
  203           fireSeriesChanged();
  204       }
  205       
  206       /**
  207        * Tests this object instance for equality with an arbitrary object.
  208        * 
  209        * @param obj  the object (<code>null</code> permitted).
  210        * 
  211        * @return A boolean.
  212        */
  213       public boolean equals(Object obj) {
  214           if (obj == this) {
  215               return true;   
  216           }
  217           if (!(obj instanceof MatrixSeries)) {
  218               return false;
  219           }
  220           MatrixSeries that = (MatrixSeries) obj;
  221           if (!(getRowCount() == that.getRowCount())) {
  222               return false;
  223           }
  224           if (!(getColumnsCount() == that.getColumnsCount())) {
  225               return false;   
  226           }
  227           for (int r = 0; r < getRowCount(); r++) {
  228               for (int c = 0; c < getColumnsCount(); c++) {
  229                   if (get(r, c) != that.get(r, c)) {
  230                       return false;
  231                   }
  232               }
  233           }
  234           return super.equals(obj);
  235       }
  236       
  237   }

Save This Page
Home » jcommon-1.0.13 » org.jfree » data » xy » [javadoc | source]