001    /**
002     * =========================================
003     * LibFormula : a free Java formula library
004     * =========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/libformula/
007     *
008     * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     *
026     * ------------
027     * $Id$
028     * ------------
029     * (C) Copyright 2006-2007, by Pentaho Corporation.
030     */
031    package org.jfree.formula.typing.sequence;
032    
033    import org.jfree.formula.EvaluationException;
034    import org.jfree.formula.FormulaContext;
035    import org.jfree.formula.typing.ArrayCallback;
036    import org.jfree.formula.typing.Type;
037    
038    /**
039     * @author Cedric Pronzato
040     */
041    public class NumberSequence
042    {
043      private int rowCursor = 0;
044      private int columnCursor = 0;
045      private Number number;
046      private ArrayCallback array;
047      private FormulaContext context;
048    
049      /**
050       * Empty number sequence.
051       */
052      public NumberSequence(final FormulaContext context)
053      {
054        this.context = context;
055      }
056    
057      /**
058       * Number sequence bounded to only one number item.
059       *
060       * @param n A number
061       */
062      public NumberSequence(final Number n, final FormulaContext context)
063      {
064        number = n;
065        this.context = context;
066      }
067    
068      /**
069       * Number sequence bounded to an array.
070       *
071       * @param array
072       */
073      public NumberSequence(final ArrayCallback array, final FormulaContext context)
074      {
075        this.array = array;
076        this.context = context;
077      }
078    
079      public boolean hasNext() throws EvaluationException
080      {
081        // empty sequence
082        if (number == null && array == null)
083        {
084          return false;
085        }
086        // sequence of one number
087        if (number != null && rowCursor == 0)
088        {
089          return true;
090        }
091    
092        // else array
093        if (array != null)
094        {
095          final int rowCount = array.getRowCount();
096          final int columnCount = array.getColumnCount();
097          if (array != null && rowCursor < rowCount && columnCursor < columnCount)
098          {
099            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    }