| 1 |
/** |
| 2 |
* =========================================== |
| 3 |
* JFreeReport : a free Java reporting library |
| 4 |
* =========================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2001-2007, by Object Refinery Ltd, 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 |
* FastGlobalView.java |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors. |
| 29 |
*/ |
| 30 |
package org.jfree.report.data; |
| 31 |
|
| 32 |
import java.util.HashMap; |
| 33 |
import java.util.HashSet; |
| 34 |
import java.util.Arrays; |
| 35 |
|
| 36 |
import org.jfree.report.DataRow; |
| 37 |
import org.jfree.report.JFreeReportBoot; |
| 38 |
import org.jfree.report.DataFlags; |
| 39 |
import org.jfree.report.DataSourceException; |
| 40 |
import org.jfree.report.util.IntegerCache; |
| 41 |
import org.pentaho.reporting.libraries.base.util.DebugLog; |
| 42 |
import org.pentaho.reporting.libraries.base.util.ObjectUtilities; |
| 43 |
|
| 44 |
/** |
| 45 |
* Creation-Date: 10.08.2007, 14:07:32 |
| 46 |
* |
| 47 |
* @author Thomas Morgner |
| 48 |
*/ |
| 49 |
public final class FastGlobalView implements DataRow |
| 50 |
{ |
| 51 |
|
| 52 |
private static final boolean DEBUG = false; |
| 53 |
private HashSet duplicateColumns; |
| 54 |
private HashSet invalidColumns; |
| 55 |
private boolean modifiableNameCache; |
| 56 |
private HashMap nameCache; |
| 57 |
private String[] columnNames; |
| 58 |
private DataFlags[] columnValue; |
| 59 |
private DataFlags[] columnOldValue; |
| 60 |
private boolean[] columnChanged; |
| 61 |
private int[] columnPrev; |
| 62 |
private int length; |
| 63 |
private boolean warnInvalidColumns; |
| 64 |
|
| 65 |
public FastGlobalView(final FastGlobalView parent) |
| 66 |
{ |
| 67 |
if (parent.modifiableNameCache) |
| 68 |
{ |
| 69 |
this.duplicateColumns = (HashSet) parent.duplicateColumns.clone(); |
| 70 |
this.nameCache = (HashMap) parent.nameCache.clone(); |
| 71 |
this.modifiableNameCache = false; |
| 72 |
this.columnNames = (String[]) parent.columnNames.clone(); |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
this.duplicateColumns = parent.duplicateColumns; |
| 77 |
this.nameCache = parent.nameCache; |
| 78 |
this.columnNames = parent.columnNames; |
| 79 |
this.modifiableNameCache = false; |
| 80 |
} |
| 81 |
this.columnChanged = parent.columnChanged.clone(); |
| 82 |
this.columnValue = parent.columnValue.clone(); |
| 83 |
this.columnOldValue = parent.columnOldValue.clone(); |
| 84 |
this.columnPrev = parent.columnPrev.clone(); |
| 85 |
this.length = parent.length; |
| 86 |
this.warnInvalidColumns = parent.warnInvalidColumns; |
| 87 |
this.invalidColumns = parent.invalidColumns; |
| 88 |
} |
| 89 |
|
| 90 |
public FastGlobalView() |
| 91 |
{ |
| 92 |
this.warnInvalidColumns = "true".equals(JFreeReportBoot.getInstance().getGlobalConfig().getConfigProperty("org.jfree.report.WarnInvalidColumns")); |
| 93 |
if (warnInvalidColumns) |
| 94 |
{ |
| 95 |
this.invalidColumns = new HashSet(); |
| 96 |
} |
| 97 |
|
| 98 |
this.duplicateColumns = new HashSet(); |
| 99 |
this.nameCache = new HashMap(); |
| 100 |
this.modifiableNameCache = true; |
| 101 |
this.columnChanged = new boolean[20]; |
| 102 |
this.columnNames = new String[20]; |
| 103 |
this.columnValue = new DataFlags[20]; |
| 104 |
this.columnOldValue = new DataFlags[20]; |
| 105 |
this.columnPrev = new int[20]; |
| 106 |
} |
| 107 |
|
| 108 |
public Object get(final int col) throws DataSourceException |
| 109 |
{ |
| 110 |
final DataFlags flag = getFlags(col); |
| 111 |
if (flag == null) |
| 112 |
{ |
| 113 |
return null; |
| 114 |
} |
| 115 |
return flag.getValue(); |
| 116 |
} |
| 117 |
|
| 118 |
public Object get(final String col) throws DataSourceException |
| 119 |
{ |
| 120 |
final DataFlags flag = getFlags(col); |
| 121 |
if (flag == null) |
| 122 |
{ |
| 123 |
return null; |
| 124 |
} |
| 125 |
return flag.getValue(); |
| 126 |
} |
| 127 |
|
| 128 |
public String getColumnName(final int col) |
| 129 |
{ |
| 130 |
if (col < 0 || col >= length) |
| 131 |
{ |
| 132 |
throw new IndexOutOfBoundsException("Column-Index " + col + " is invalid."); |
| 133 |
} |
| 134 |
return columnNames[col]; |
| 135 |
} |
| 136 |
|
| 137 |
public int findColumn(final String name) |
| 138 |
{ |
| 139 |
final Integer o = (Integer) nameCache.get(name); |
| 140 |
if (o == null) |
| 141 |
{ |
| 142 |
return -1; |
| 143 |
} |
| 144 |
return o.intValue(); |
| 145 |
} |
| 146 |
|
| 147 |
public int getColumnCount() |
| 148 |
{ |
| 149 |
return length; |
| 150 |
} |
| 151 |
|
| 152 |
public FastGlobalView derive() |
| 153 |
{ |
| 154 |
return new FastGlobalView(this); |
| 155 |
} |
| 156 |
|
| 157 |
public FastGlobalView advance() |
| 158 |
{ |
| 159 |
final FastGlobalView advanced = new FastGlobalView(this); |
| 160 |
System.arraycopy(advanced.columnValue, 0, advanced.columnOldValue, 0, length); |
| 161 |
Arrays.fill(advanced.columnChanged, false); |
| 162 |
return advanced; |
| 163 |
} |
| 164 |
|
| 165 |
public void removeColumn(final String name) |
| 166 |
{ |
| 167 |
final boolean needToRebuildCache; |
| 168 |
int idx = -1; |
| 169 |
if (duplicateColumns.contains(name)) |
| 170 |
{ |
| 171 |
needToRebuildCache = true; |
| 172 |
// linear index search from the end .. |
| 173 |
for (int i = length - 1; i >= 0; i -= 1) |
| 174 |
{ |
| 175 |
if (ObjectUtilities.equal(name, columnNames[i])) |
| 176 |
{ |
| 177 |
idx = i; |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
if (idx < 0) |
| 182 |
{ |
| 183 |
return; |
| 184 |
} |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
needToRebuildCache = false; |
| 189 |
final Integer o = (Integer) nameCache.get(name); |
| 190 |
if (o == null) |
| 191 |
{ |
| 192 |
return; |
| 193 |
} |
| 194 |
idx = o.intValue(); |
| 195 |
} |
| 196 |
|
| 197 |
if (DEBUG) |
| 198 |
{ |
| 199 |
DebugLog.log("Removing column " + name + " (Length: " + length + " NameCache: " + |
| 200 |
nameCache.size() + ", Idx: " + idx); |
| 201 |
} |
| 202 |
|
| 203 |
if (modifiableNameCache == false) |
| 204 |
{ |
| 205 |
this.duplicateColumns = (HashSet) duplicateColumns.clone(); |
| 206 |
this.columnNames = (String[]) columnNames.clone(); |
| 207 |
this.nameCache = (HashMap) nameCache.clone(); |
| 208 |
this.modifiableNameCache = true; |
| 209 |
} |
| 210 |
|
| 211 |
if (idx == (length - 1)) |
| 212 |
{ |
| 213 |
columnChanged[idx] = false; |
| 214 |
columnNames[idx] = null; |
| 215 |
columnValue[idx] = null; |
| 216 |
if (columnPrev[idx] == -1) |
| 217 |
{ |
| 218 |
nameCache.remove(name); |
| 219 |
} |
| 220 |
else |
| 221 |
{ |
| 222 |
nameCache.put(name, IntegerCache.getInteger(columnPrev[idx])); |
| 223 |
} |
| 224 |
// thats the easy case .. |
| 225 |
length -= 1; |
| 226 |
if (needToRebuildCache) |
| 227 |
{ |
| 228 |
if (columnPrev[idx] == -1) |
| 229 |
{ |
| 230 |
DebugLog.log("Column marked as duplicate but no duplicate index recorded: " + name); |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
if (columnPrev[columnPrev[idx]] == -1) |
| 235 |
{ |
| 236 |
duplicateColumns.remove(name); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
if (DEBUG) |
| 244 |
{ |
| 245 |
DebugLog.log("Out of order removeal of a column: " + name); |
| 246 |
} |
| 247 |
|
| 248 |
if (columnPrev[idx] == -1) |
| 249 |
{ |
| 250 |
nameCache.remove(name); |
| 251 |
} |
| 252 |
else |
| 253 |
{ |
| 254 |
nameCache.put(name, IntegerCache.getInteger(columnPrev[idx])); |
| 255 |
} |
| 256 |
|
| 257 |
final int moveStartIdx = idx + 1; |
| 258 |
final int moveLength = length - moveStartIdx; |
| 259 |
System.arraycopy(columnNames, moveStartIdx, columnNames, idx, moveLength); |
| 260 |
System.arraycopy(columnChanged, moveStartIdx, columnChanged, idx, moveLength); |
| 261 |
System.arraycopy(columnOldValue, moveStartIdx, columnOldValue, idx, moveLength); |
| 262 |
System.arraycopy(columnValue, moveStartIdx, columnValue, idx, moveLength); |
| 263 |
System.arraycopy(columnPrev, moveStartIdx, columnPrev, idx, moveLength); |
| 264 |
columnNames[length - 1] = null; |
| 265 |
columnChanged[length - 1] = false; |
| 266 |
columnOldValue[length - 1] = null; |
| 267 |
columnPrev[length - 1] = 0; |
| 268 |
|
| 269 |
// Now it gets expensive: Rebuild the namecache .. |
| 270 |
final int newLength = moveLength + idx; |
| 271 |
nameCache.clear(); |
| 272 |
duplicateColumns.clear(); |
| 273 |
for (int i = 0; i < newLength; i++) |
| 274 |
{ |
| 275 |
final String columnName = columnNames[i]; |
| 276 |
final Integer oldVal = (Integer) nameCache.get(columnName); |
| 277 |
if (nameCache.containsKey(columnName)) |
| 278 |
{ |
| 279 |
duplicateColumns.add(columnName); |
| 280 |
} |
| 281 |
|
| 282 |
nameCache.put(columnName, IntegerCache.getInteger(i)); |
| 283 |
if (oldVal != null) |
| 284 |
{ |
| 285 |
columnPrev[i] = oldVal.intValue(); |
| 286 |
} |
| 287 |
else |
| 288 |
{ |
| 289 |
columnPrev[i] = -1; |
| 290 |
} |
| 291 |
} |
| 292 |
length -= 1; |
| 293 |
if (DEBUG) |
| 294 |
{ |
| 295 |
DebugLog.log("New Namecache: " + nameCache); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
public void putField(final String name, |
| 300 |
final Object value, |
| 301 |
final boolean update) |
| 302 |
{ |
| 303 |
if (DEBUG) |
| 304 |
{ |
| 305 |
if (update) |
| 306 |
{ |
| 307 |
DebugLog.log(" + : " + name); |
| 308 |
} |
| 309 |
else |
| 310 |
{ |
| 311 |
DebugLog.log("Adding: " + name); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
if (update == false) |
| 316 |
{ |
| 317 |
if (modifiableNameCache == false) |
| 318 |
{ |
| 319 |
this.columnNames = (String[]) columnNames.clone(); |
| 320 |
this.nameCache = (HashMap) nameCache.clone(); |
| 321 |
this.modifiableNameCache = true; |
| 322 |
} |
| 323 |
|
| 324 |
final DefaultDataFlags flagedValue = new DefaultDataFlags(name, value, true); |
| 325 |
|
| 326 |
// A new column ... |
| 327 |
ensureCapacity(length + 1); |
| 328 |
columnNames[length] = name; |
| 329 |
columnValue[length] = flagedValue; |
| 330 |
final Integer o = (Integer) nameCache.get(name); |
| 331 |
if (o == null) |
| 332 |
{ |
| 333 |
columnPrev[length] = -1; |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
columnPrev[length] = o.intValue(); |
| 338 |
duplicateColumns.add(name); |
| 339 |
} |
| 340 |
columnChanged[length] = true; |
| 341 |
columnOldValue[length] = null; |
| 342 |
nameCache.put(name, IntegerCache.getInteger(length)); |
| 343 |
length += 1; |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
try |
| 348 |
{ |
| 349 |
// Updating an existing column ... |
| 350 |
final Integer o = (Integer) nameCache.get(name); |
| 351 |
if (o == null) |
| 352 |
{ |
| 353 |
throw new IllegalStateException("Update to a non-existing column: " + name); |
| 354 |
} |
| 355 |
int idx = -1; |
| 356 |
if (duplicateColumns.contains(name)) |
| 357 |
{ |
| 358 |
for (int i = 0; i < length; i += 1) |
| 359 |
{ |
| 360 |
if (columnChanged[i] == false && ObjectUtilities.equal(name, columnNames[i])) |
| 361 |
{ |
| 362 |
idx = i; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
if (idx < 0) |
| 367 |
{ |
| 368 |
idx = o.intValue(); |
| 369 |
} |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
idx = o.intValue(); |
| 374 |
} |
| 375 |
final boolean changed = !ObjectUtilities.equal(value, columnValue[idx].getValue()); |
| 376 |
final DefaultDataFlags flagedValue = new DefaultDataFlags(name, value, changed); |
| 377 |
columnNames[idx] = name; |
| 378 |
columnOldValue[idx] = columnValue[idx]; |
| 379 |
columnValue[idx] = flagedValue; |
| 380 |
columnChanged[idx] = changed; |
| 381 |
} |
| 382 |
catch (DataSourceException ex) |
| 383 |
{ |
| 384 |
ex.printStackTrace(); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
private void ensureCapacity(final int requestedSize) |
| 390 |
{ |
| 391 |
final int capacity = this.columnNames.length; |
| 392 |
if (capacity > requestedSize) |
| 393 |
{ |
| 394 |
return; |
| 395 |
} |
| 396 |
final int newSize = Math.max(capacity << 1, requestedSize + 10); |
| 397 |
|
| 398 |
final String[] newColumnNames = new String[newSize]; |
| 399 |
System.arraycopy(columnNames, 0, newColumnNames, 0, length); |
| 400 |
this.columnNames = newColumnNames; |
| 401 |
|
| 402 |
final boolean[] newColumnChanged = new boolean[newSize]; |
| 403 |
System.arraycopy(columnChanged, 0, newColumnChanged, 0, length); |
| 404 |
this.columnChanged = newColumnChanged; |
| 405 |
|
| 406 |
final int[] newColumnPrev = new int[newSize]; |
| 407 |
System.arraycopy(columnPrev, 0, newColumnPrev, 0, length); |
| 408 |
this.columnPrev = newColumnPrev; |
| 409 |
|
| 410 |
final DataFlags[] newColumnValue = new DataFlags[newSize]; |
| 411 |
System.arraycopy(columnValue, 0, newColumnValue, 0, length); |
| 412 |
this.columnValue = newColumnValue; |
| 413 |
|
| 414 |
final DataFlags[] newOldColumnValue = new DataFlags[newSize]; |
| 415 |
System.arraycopy(columnOldValue, 0, newOldColumnValue, 0, length); |
| 416 |
this.columnOldValue = newOldColumnValue; |
| 417 |
} |
| 418 |
|
| 419 |
public DataFlags getFlags(final String col) throws DataSourceException |
| 420 |
{ |
| 421 |
final int idx = findColumn(col); |
| 422 |
if (idx == -1) |
| 423 |
{ |
| 424 |
return null; |
| 425 |
} |
| 426 |
return getFlags(idx); |
| 427 |
} |
| 428 |
|
| 429 |
public DataFlags getFlags(final int col) throws DataSourceException |
| 430 |
{ |
| 431 |
if (col < 0 || col >= length) |
| 432 |
{ |
| 433 |
throw new IndexOutOfBoundsException("Column-Index " + col + " is invalid."); |
| 434 |
} |
| 435 |
|
| 436 |
if (columnChanged[col] != false) |
| 437 |
{ |
| 438 |
return columnValue[col]; |
| 439 |
} |
| 440 |
|
| 441 |
final String columnName = columnNames[col]; |
| 442 |
if (duplicateColumns.contains(columnName)) |
| 443 |
{ |
| 444 |
for (int i = col - 1; i >= 0; i--) |
| 445 |
{ |
| 446 |
if (columnNames[i].equals(columnName) && columnChanged[i] == true) |
| 447 |
{ |
| 448 |
return columnValue[i]; |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
return columnValue[col]; |
| 454 |
} |
| 455 |
} |