| 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 10756 2009-12-02 15:58:24Z 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(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(DataRow dataRow) throws DataSourceException |
| 75 |
{ |
| 76 |
if (dataRow == null) |
| 77 |
{ |
| 78 |
throw new NullPointerException(); |
| 79 |
} |
| 80 |
|
| 81 |
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(String[] names, Object[] values) |
| 101 |
{ |
| 102 |
setData(names, values); |
| 103 |
} |
| 104 |
|
| 105 |
protected void setData(String[] names, Object[] values) |
| 106 |
{ |
| 107 |
if (names == null) |
| 108 |
{ |
| 109 |
throw new NullPointerException(); |
| 110 |
} |
| 111 |
if (values == null) |
| 112 |
{ |
| 113 |
throw new NullPointerException(); |
| 114 |
} |
| 115 |
final int length; |
| 116 |
if (names.length == values.length) |
| 117 |
{ |
| 118 |
length = names.length; |
| 119 |
this.names = (String[]) names.clone(); |
| 120 |
this.values = (Object[]) values.clone(); |
| 121 |
} |
| 122 |
else |
| 123 |
{ |
| 124 |
length = Math.min(names.length, values.length); |
| 125 |
this.names = new String[length]; |
| 126 |
this.values = new Object[length]; |
| 127 |
System.arraycopy(names, 0, this.names, 0, length); |
| 128 |
System.arraycopy(values, 0, this.values, 0, length); |
| 129 |
} |
| 130 |
|
| 131 |
final HashMap nameCache = new HashMap(); |
| 132 |
for (int i = 0; i < length; i++) |
| 133 |
{ |
| 134 |
final String name = names[i]; |
| 135 |
if (name != null) |
| 136 |
{ |
| 137 |
nameCache.put(name, IntegerCache.getInteger(i)); |
| 138 |
} |
| 139 |
} |
| 140 |
this.nameCache = Collections.unmodifiableMap(nameCache); |
| 141 |
} |
| 142 |
|
| 143 |
protected void updateData(final Object[] values) |
| 144 |
{ |
| 145 |
if (values.length != this.values.length) |
| 146 |
{ |
| 147 |
throw new IllegalArgumentException("You should preserve the number of columns."); |
| 148 |
} |
| 149 |
|
| 150 |
this.values = (Object[]) values.clone(); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns the value of the expression or column in the tablemodel using the |
| 155 |
* given column number as index. For functions and expressions, the |
| 156 |
* <code>getValue()</code> method is called and for columns from the |
| 157 |
* tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets |
| 158 |
* called. |
| 159 |
* |
| 160 |
* @param col the item index. |
| 161 |
* @return the value. |
| 162 |
* @throws IllegalStateException if the datarow detected a deadlock. |
| 163 |
*/ |
| 164 |
public Object get(int col) throws DataSourceException |
| 165 |
{ |
| 166 |
return values[col]; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the value of the function, expression or column using its specific |
| 171 |
* name. The given name is translated into a valid column number and the the |
| 172 |
* column is queried. For functions and expressions, the |
| 173 |
* <code>getValue()</code> method is called and for columns from the |
| 174 |
* tablemodel the tablemodel method <code>getValueAt(row, column)</code> gets |
| 175 |
* called. |
| 176 |
* |
| 177 |
* @param col the item index. |
| 178 |
* @return the value. |
| 179 |
* @throws IllegalStateException if the datarow detected a deadlock. |
| 180 |
*/ |
| 181 |
public Object get(String col) throws DataSourceException |
| 182 |
{ |
| 183 |
final Integer idx = (Integer) nameCache.get(col); |
| 184 |
if (idx == null) |
| 185 |
{ |
| 186 |
return null; |
| 187 |
} |
| 188 |
return values[idx.intValue()]; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns the name of the column, expression or function. For columns from |
| 193 |
* the tablemodel, the tablemodels <code>getColumnName</code> method is |
| 194 |
* called. For functions, expressions and report properties the assigned name |
| 195 |
* is returned. |
| 196 |
* |
| 197 |
* @param col the item index. |
| 198 |
* @return the name. |
| 199 |
*/ |
| 200 |
public String getColumnName(int col) throws DataSourceException |
| 201 |
{ |
| 202 |
return names[col]; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns the number of columns, expressions and functions and marked |
| 207 |
* ReportProperties in the report. |
| 208 |
* |
| 209 |
* @return the item count. |
| 210 |
*/ |
| 211 |
public int getColumnCount() throws DataSourceException |
| 212 |
{ |
| 213 |
return values.length; |
| 214 |
} |
| 215 |
|
| 216 |
public DataFlags getFlags(String col) throws DataSourceException |
| 217 |
{ |
| 218 |
return new DefaultDataFlags(col, get(col), false); |
| 219 |
} |
| 220 |
|
| 221 |
public DataFlags getFlags(int col) throws DataSourceException |
| 222 |
{ |
| 223 |
return new DefaultDataFlags(names[col], values[col], false); |
| 224 |
} |
| 225 |
} |