Fixed SharedValueManager to create separate empty instances in anticipation of instances becoming mutable.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@893057 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-12-22 01:41:16 +00:00
parent f11c66506d
commit 65a41cce66
4 changed files with 24 additions and 13 deletions

View File

@ -56,9 +56,12 @@ public final class RowRecordsAggregate extends RecordAggregate {
/** Creates a new instance of ValueRecordsAggregate */ /** Creates a new instance of ValueRecordsAggregate */
public RowRecordsAggregate() { public RowRecordsAggregate() {
this(SharedValueManager.EMPTY); this(SharedValueManager.createEmpty());
} }
private RowRecordsAggregate(SharedValueManager svm) { private RowRecordsAggregate(SharedValueManager svm) {
if (svm == null) {
throw new IllegalArgumentException("SharedValueManager must be provided.");
}
_rowRecords = new TreeMap<Integer, RowRecord>(); _rowRecords = new TreeMap<Integer, RowRecord>();
_valuesAgg = new ValueRecordsAggregate(); _valuesAgg = new ValueRecordsAggregate();
_unknownRecords = new ArrayList<Record>(); _unknownRecords = new ArrayList<Record>();
@ -68,6 +71,8 @@ public final class RowRecordsAggregate extends RecordAggregate {
/** /**
* @param rs record stream with all {@link SharedFormulaRecord} * @param rs record stream with all {@link SharedFormulaRecord}
* {@link ArrayRecord}, {@link TableRecord} {@link MergeCellsRecord} Records removed * {@link ArrayRecord}, {@link TableRecord} {@link MergeCellsRecord} Records removed
* @param svm an initialised {@link SharedValueManager} (from the shared formula, array
* and table records of the current sheet). Never <code>null</code>.
*/ */
public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) { public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) {
this(svm); this(svm);

View File

@ -109,8 +109,14 @@ public final class SharedValueManager {
} }
} }
public static final SharedValueManager EMPTY = new SharedValueManager( /**
* @return a new empty {@link SharedValueManager}.
*/
public static final SharedValueManager createEmpty() {
// Note - must create distinct instances because they are assumed to be mutable.
return new SharedValueManager(
new SharedFormulaRecord[0], new CellReference[0], new ArrayRecord[0], new TableRecord[0]); new SharedFormulaRecord[0], new CellReference[0], new ArrayRecord[0], new TableRecord[0]);
}
private final ArrayRecord[] _arrayRecords; private final ArrayRecord[] _arrayRecords;
private final TableRecord[] _tableRecords; private final TableRecord[] _tableRecords;
private final Map<SharedFormulaRecord, SharedFormulaGroup> _groupsBySharedFormulaRecord; private final Map<SharedFormulaRecord, SharedFormulaGroup> _groupsBySharedFormulaRecord;
@ -144,7 +150,7 @@ public final class SharedValueManager {
public static SharedValueManager create(SharedFormulaRecord[] sharedFormulaRecords, public static SharedValueManager create(SharedFormulaRecord[] sharedFormulaRecords,
CellReference[] firstCells, ArrayRecord[] arrayRecords, TableRecord[] tableRecords) { CellReference[] firstCells, ArrayRecord[] arrayRecords, TableRecord[] tableRecords) {
if (sharedFormulaRecords.length + firstCells.length + arrayRecords.length + tableRecords.length < 1) { if (sharedFormulaRecords.length + firstCells.length + arrayRecords.length + tableRecords.length < 1) {
return EMPTY; return createEmpty();
} }
return new SharedValueManager(sharedFormulaRecords, firstCells, arrayRecords, tableRecords); return new SharedValueManager(sharedFormulaRecords, firstCells, arrayRecords, tableRecords);
} }

View File

@ -37,7 +37,7 @@ public final class TestFormulaRecordAggregate extends TestCase {
f.setCachedResultTypeString(); f.setCachedResultTypeString();
StringRecord s = new StringRecord(); StringRecord s = new StringRecord();
s.setString("abc"); s.setString("abc");
FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.EMPTY); FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.createEmpty());
assertEquals("abc", fagg.getStringValue()); assertEquals("abc", fagg.getStringValue());
} }
@ -54,7 +54,7 @@ public final class TestFormulaRecordAggregate extends TestCase {
fr.setValue(2.0); fr.setValue(2.0);
StringRecord sr = new StringRecord(); StringRecord sr = new StringRecord();
sr.setString("NA"); sr.setString("NA");
SharedValueManager svm = SharedValueManager.EMPTY; SharedValueManager svm = SharedValueManager.createEmpty();
FormulaRecordAggregate fra; FormulaRecordAggregate fra;
try { try {

View File

@ -63,24 +63,24 @@ public final class TestRowRecordsAggregate extends TestCase {
/** /**
* Prior to Aug 2008, POI would re-serialize spreadsheets with {@link ArrayRecord}s or * Prior to Aug 2008, POI would re-serialize spreadsheets with {@link ArrayRecord}s or
* {@link TableRecord}s with those records out of order. Similar to * {@link TableRecord}s with those records out of order. Similar to
* {@link SharedFormulaRecord}s, these records should appear immediately after the first * {@link SharedFormulaRecord}s, these records should appear immediately after the first
* {@link FormulaRecord}s that they apply to (and only once).<br/> * {@link FormulaRecord}s that they apply to (and only once).<br/>
*/ */
public void testArraysAndTables() { public void testArraysAndTables() {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls"); HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls");
Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0); Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0);
int countArrayFormulas = verifySharedValues(sheetRecs, ArrayRecord.class); int countArrayFormulas = verifySharedValues(sheetRecs, ArrayRecord.class);
assertEquals(5, countArrayFormulas); assertEquals(5, countArrayFormulas);
int countTableFormulas = verifySharedValues(sheetRecs, TableRecord.class); int countTableFormulas = verifySharedValues(sheetRecs, TableRecord.class);
assertEquals(3, countTableFormulas); assertEquals(3, countTableFormulas);
// Note - SharedFormulaRecords are currently not re-serialized by POI (each is extracted // Note - SharedFormulaRecords are currently not re-serialized by POI (each is extracted
// into many non-shared formulas), but if they ever were, the same rules would apply. // into many non-shared formulas), but if they ever were, the same rules would apply.
int countSharedFormulas = verifySharedValues(sheetRecs, SharedFormulaRecord.class); int countSharedFormulas = verifySharedValues(sheetRecs, SharedFormulaRecord.class);
assertEquals(0, countSharedFormulas); assertEquals(0, countSharedFormulas);
if (false) { // set true to observe re-serialized file if (false) { // set true to observe re-serialized file
File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls"); File f = new File(System.getProperty("java.io.tmpdir") + "/testArraysAndTables-out.xls");
@ -96,7 +96,7 @@ public final class TestRowRecordsAggregate extends TestCase {
} }
private static int verifySharedValues(Record[] recs, Class shfClass) { private static int verifySharedValues(Record[] recs, Class shfClass) {
int result =0; int result =0;
for(int i=0; i<recs.length; i++) { for(int i=0; i<recs.length; i++) {
Record rec = recs[i]; Record rec = recs[i];
@ -105,7 +105,7 @@ public final class TestRowRecordsAggregate extends TestCase {
Record prevRec = recs[i-1]; Record prevRec = recs[i-1];
if (!(prevRec instanceof FormulaRecord)) { if (!(prevRec instanceof FormulaRecord)) {
throw new AssertionFailedError("Bad record order at index " throw new AssertionFailedError("Bad record order at index "
+ i + ": Formula record expected but got (" + i + ": Formula record expected but got ("
+ prevRec.getClass().getName() + ")"); + prevRec.getClass().getName() + ")");
} }
verifySharedFormula((FormulaRecord) prevRec, rec); verifySharedFormula((FormulaRecord) prevRec, rec);
@ -127,7 +127,7 @@ public final class TestRowRecordsAggregate extends TestCase {
* The functionality change being tested here is actually not critical to the overall fix * The functionality change being tested here is actually not critical to the overall fix
* for bug 46280, since the fix involved making sure the that offending <i>PivotTable</i> * for bug 46280, since the fix involved making sure the that offending <i>PivotTable</i>
* records do not get into {@link RowRecordsAggregate}.<br/> * records do not get into {@link RowRecordsAggregate}.<br/>
* This fix in {@link RowRecordsAggregate} was implemented anyway since any {@link * This fix in {@link RowRecordsAggregate} was implemented anyway since any {@link
* UnknownRecord} has the potential of being 'continued'. * UnknownRecord} has the potential of being 'continued'.
*/ */
public void testUnknownContinue_bug46280() { public void testUnknownContinue_bug46280() {
@ -140,7 +140,7 @@ public final class TestRowRecordsAggregate extends TestCase {
RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0); RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0);
RowRecordsAggregate rra; RowRecordsAggregate rra;
try { try {
rra = new RowRecordsAggregate(rs, SharedValueManager.EMPTY); rra = new RowRecordsAggregate(rs, SharedValueManager.createEmpty());
} catch (RuntimeException e) { } catch (RuntimeException e) {
if (e.getMessage().startsWith("Unexpected record type")) { if (e.getMessage().startsWith("Unexpected record type")) {
throw new AssertionFailedError("Identified bug 46280a"); throw new AssertionFailedError("Identified bug 46280a");