Source for org.jfree.report.data.StaticDataRow

   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: StaticDataRow.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.data;
  32: 
  33: import java.util.Collections;
  34: import java.util.HashMap;
  35: import java.util.Map;
  36: 
  37: import org.jfree.report.DataFlags;
  38: import org.jfree.report.DataRow;
  39: import org.jfree.report.DataSourceException;
  40: import org.jfree.report.util.IntegerCache;
  41: 
  42: /**
  43:  * This is a static datarow holding a value for each name in the datarow. This
  44:  * datarow does not hold dataflags and thus does not track the changes done to
  45:  * the data inside.
  46:  * <p/>
  47:  * The StaticDataRow is a derived view and is used to provide a safe collection
  48:  * of the values of the previous datarow.
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public class StaticDataRow implements DataRow
  53: {
  54:   private String[] names;
  55:   private Object[] values;
  56:   private Map nameCache;
  57: 
  58:   protected StaticDataRow()
  59:   {
  60:   }
  61: 
  62:   protected StaticDataRow(final StaticDataRow dataRow)
  63:   {
  64:     if (dataRow == null)
  65:     {
  66:       throw new NullPointerException();
  67:     }
  68: 
  69:     this.nameCache = dataRow.nameCache;
  70:     this.names = dataRow.names;
  71:     this.values = dataRow.values;
  72:   }
  73: 
  74:   public StaticDataRow(final DataRow dataRow) throws DataSourceException
  75:   {
  76:     if (dataRow == null)
  77:     {
  78:       throw new NullPointerException();
  79:     }
  80: 
  81:     final HashMap nameCache = new HashMap();
  82:     synchronized (dataRow)
  83:     {
  84:       final int columnCount = dataRow.getColumnCount();
  85:       this.names = new String[columnCount];
  86:       this.values = new Object[dataRow.getColumnCount()];
  87:       for (int i = 0; i < columnCount; i++)
  88:       {
  89:         names[i] = dataRow.getColumnName(i);
  90:         values[i] = dataRow.get(i);
  91:         if (names[i] != null)
  92:         {
  93:           nameCache.put(names[i], IntegerCache.getInteger(i));
  94:         }
  95:       }
  96:     }
  97:     this.nameCache = Collections.unmodifiableMap(nameCache);
  98:   }
  99: 
 100:   public StaticDataRow(final String[] names, final Object[] values)
 101:   {
 102:     setData(names, values);
 103:   }
 104: 
 105:   protected void setData(final String[] names, final Object[] values)
 106:   {
 107:     if (names == null)
 108:     {
 109:       throw new NullPointerException();
 110:     }
 111:     if (values == null)
 112:     {
 113:       throw new NullPointerException();
 114:     }
 115:     if (names.length != values.length)
 116:     {
 117:       throw new IndexOutOfBoundsException();
 118:     }
 119:     this.names = (String[]) names.clone();
 120:     this.values = (Object[]) values.clone();
 121: 
 122:     final HashMap nameCache = new HashMap();
 123:     for (int i = 0; i < names.length; i++)
 124:     {
 125:       final String name = names[i];
 126:       if (name != null)
 127:       {
 128:         nameCache.put(name, IntegerCache.getInteger(i));
 129:       }
 130:     }
 131:     this.nameCache = Collections.unmodifiableMap(nameCache);
 132:   }
 133: 
 134:   protected void updateData(final Object[] values)
 135:   {
 136:     if (values.length != this.values.length)
 137:     {
 138:       throw new IllegalArgumentException("You should preserve the number of columns.");
 139:     }
 140: 
 141:     this.values = (Object[]) values.clone();
 142:   }
 143: 
 144:   /**
 145:    * Returns the value of the expression or column in the tablemodel using the
 146:    * given column number as index. For functions and expressions, the
 147:    * <code>getValue()</code> method is called and for columns from the
 148:    * tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets
 149:    * called.
 150:    *
 151:    * @param col the item index.
 152:    * @return the value.
 153:    * @throws IllegalStateException if the datarow detected a deadlock.
 154:    */
 155:   public Object get(final int col) throws DataSourceException
 156:   {
 157:     return values[col];
 158:   }
 159: 
 160:   /**
 161:    * Returns the value of the function, expression or column using its specific
 162:    * name. The given name is translated into a valid column number and the the
 163:    * column is queried. For functions and expressions, the
 164:    * <code>getValue()</code> method is called and for columns from the
 165:    * tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets
 166:    * called.
 167:    *
 168:    * @param col the item index.
 169:    * @return the value.
 170:    * @throws IllegalStateException if the datarow detected a deadlock.
 171:    */
 172:   public Object get(final String col) throws DataSourceException
 173:   {
 174:     final Integer idx = (Integer) nameCache.get(col);
 175:     if (idx == null)
 176:     {
 177:       return null;
 178:     }
 179:     return values[idx.intValue()];
 180:   }
 181: 
 182:   /**
 183:    * Returns the name of the column, expression or function. For columns from
 184:    * the tablemodel, the tablemodels <code>getColumnName</code> method is
 185:    * called. For functions, expressions and report properties the assigned name
 186:    * is returned.
 187:    *
 188:    * @param col the item index.
 189:    * @return the name.
 190:    */
 191:   public String getColumnName(final int col) throws DataSourceException
 192:   {
 193:     return names[col];
 194:   }
 195: 
 196:   /**
 197:    * Returns the number of columns, expressions and functions and marked
 198:    * ReportProperties in the report.
 199:    *
 200:    * @return the item count.
 201:    */
 202:   public int getColumnCount() throws DataSourceException
 203:   {
 204:     return values.length;
 205:   }
 206: 
 207:   public DataFlags getFlags(final String col) throws DataSourceException
 208:   {
 209:     return new DefaultDataFlags(col, get(col), false);
 210:   }
 211: 
 212:   public DataFlags getFlags(final int col) throws DataSourceException
 213:   {
 214:     return new DefaultDataFlags(names[col], values[col], false);
 215:   }
 216: }