Source for org.jfree.report.expressions.sys.GroupByExpression

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, 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:  * $Id: GroupByExpression.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.expressions.sys;
  32: 
  33: import java.util.ArrayList;
  34: import java.util.Arrays;
  35: 
  36: import org.jfree.report.DataFlags;
  37: import org.jfree.report.DataRow;
  38: import org.jfree.report.DataSourceException;
  39: import org.jfree.report.expressions.AbstractExpression;
  40: 
  41: /**
  42:  * Creation-Date: 08.10.2006, 16:28:37
  43:  *
  44:  * @author Thomas Morgner
  45:  */
  46: public class GroupByExpression extends AbstractExpression
  47: {
  48:   private ArrayList fields;
  49:   private transient String[] fieldsCached;
  50: 
  51:   public GroupByExpression()
  52:   {
  53:     this.fields = new ArrayList();
  54:   }
  55: 
  56:   /**
  57:    * Return the current expression value. <P> The value depends (obviously) on
  58:    * the expression implementation.
  59:    *
  60:    * @return the value of the function.
  61:    */
  62:   public Object computeValue() throws DataSourceException
  63:   {
  64:     final DataRow dr = getDataRow();
  65:     final String[] columns = getField();
  66:     for (int i = 0; i < columns.length; i++)
  67:     {
  68:       final String column = columns[i];
  69:       final DataFlags df = dr.getFlags(column);
  70:       if (df == null)
  71:       {
  72:         // invalid column or invalid implementation ...
  73:         continue;
  74:       }
  75:       if (df.isChanged())
  76:       {
  77:         //Log.debug ("Field: " + df.getName() + " has changed to " + df.getValue());
  78:         return Boolean.TRUE;
  79:       }
  80:     }
  81:     return Boolean.FALSE;
  82:   }
  83: 
  84:   public void setField (final int index, final String field)
  85:   {
  86:     if (fields.size() == index)
  87:     {
  88:       fields.add(field);
  89:     }
  90:     else
  91:     {
  92:       fields.set(index, field);
  93:     }
  94:     fieldsCached = null;
  95:   }
  96: 
  97:   public String getField (final int index)
  98:   {
  99:     return (String) fields.get(index);
 100:   }
 101: 
 102:   public int getFieldCount ()
 103:   {
 104:     return fields.size();
 105:   }
 106: 
 107:   public String[] getField ()
 108:   {
 109:     if (fieldsCached == null)
 110:     {
 111:       fieldsCached = (String[]) fields.toArray(new String[fields.size()]);
 112:     }
 113:     return (String[]) fieldsCached.clone();
 114:   }
 115: 
 116:   public void setField (final String[] fields)
 117:   {
 118:     this.fields.clear();
 119:     this.fields.addAll(Arrays.asList(fields));
 120:     this.fieldsCached = (String[]) fields.clone();
 121:   }
 122: 
 123:   public Object clone() throws CloneNotSupportedException
 124:   {
 125:     final GroupByExpression co = (GroupByExpression) super.clone();
 126:     co.fields = (ArrayList) fields.clone();
 127:     co.fieldsCached = fieldsCached;
 128:     return co;
 129:   }
 130: }