Source for org.jfree.formula.typing.sequence.NumberSequence

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id$
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula.typing.sequence;
  32: 
  33: import org.jfree.formula.EvaluationException;
  34: import org.jfree.formula.FormulaContext;
  35: import org.jfree.formula.typing.ArrayCallback;
  36: import org.jfree.formula.typing.Type;
  37: 
  38: /**
  39:  * @author Cedric Pronzato
  40:  */
  41: public class NumberSequence
  42: {
  43:   private int rowCursor = 0;
  44:   private int columnCursor = 0;
  45:   private Number number;
  46:   private ArrayCallback array;
  47:   private FormulaContext context;
  48: 
  49:   /**
  50:    * Empty number sequence.
  51:    */
  52:   public NumberSequence(final FormulaContext context)
  53:   {
  54:     this.context = context;
  55:   }
  56: 
  57:   /**
  58:    * Number sequence bounded to only one number item.
  59:    *
  60:    * @param n A number
  61:    */
  62:   public NumberSequence(final Number n, final FormulaContext context)
  63:   {
  64:     number = n;
  65:     this.context = context;
  66:   }
  67: 
  68:   /**
  69:    * Number sequence bounded to an array.
  70:    *
  71:    * @param array
  72:    */
  73:   public NumberSequence(final ArrayCallback array, final FormulaContext context)
  74:   {
  75:     this.array = array;
  76:     this.context = context;
  77:   }
  78: 
  79:   public boolean hasNext() throws EvaluationException
  80:   {
  81:     // empty sequence
  82:     if (number == null && array == null)
  83:     {
  84:       return false;
  85:     }
  86:     // sequence of one number
  87:     if (number != null && rowCursor == 0)
  88:     {
  89:       return true;
  90:     }
  91: 
  92:     // else array
  93:     if (array != null)
  94:     {
  95:       final int rowCount = array.getRowCount();
  96:       final int columnCount = array.getColumnCount();
  97:       if (array != null && rowCursor < rowCount && columnCursor < columnCount)
  98:       {
  99:         for (; rowCursor < rowCount; rowCursor++)
 100:         {
 101:           for (; columnCursor < columnCount; columnCursor++)
 102:           {
 103:             final Type type = array.getType(rowCursor, columnCursor);
 104:             final boolean b = type.isFlagSet(Type.NUMERIC_TYPE);
 105: 
 106:             if (b)
 107:             {
 108:               return true;
 109:             }
 110:           }
 111:           columnCursor = 0; // go to start of the next row
 112:         }
 113:       }
 114:     }
 115: 
 116:     return false;
 117:   }
 118: 
 119:   public Number nextNumber() throws EvaluationException
 120:   {
 121:     if (number != null && rowCursor == 0)
 122:     {
 123:       rowCursor++;
 124:       return number;
 125:     }
 126:     if (array != null)
 127:     {
 128:       final Type type = array.getType(rowCursor, columnCursor);
 129:       final Object value = array.getValue(rowCursor, columnCursor);
 130:       final Number number = context.getTypeRegistry().convertToNumber(type, value);
 131:       // advance
 132:       if (columnCursor == array.getColumnCount() - 1)
 133:       {
 134:         rowCursor++;
 135:         columnCursor = 0;
 136:       }
 137:       else
 138:       {
 139:         columnCursor++;
 140:       }
 141:       return number;
 142: 
 143:     }
 144:     return null;
 145:   }
 146: 
 147: }