From: Josh Micich Date: Tue, 22 Dec 2009 01:41:16 +0000 (+0000) Subject: Fixed SharedValueManager to create separate empty instances in anticipation of instan... X-Git-Tag: REL_3_7_BETA1~194 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4b14bbc353009474702dd67712a225f5686cd532;p=poi.git 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 --- diff --git a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java index 5b9393719a..ade64fba6f 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/RowRecordsAggregate.java @@ -56,9 +56,12 @@ public final class RowRecordsAggregate extends RecordAggregate { /** Creates a new instance of ValueRecordsAggregate */ public RowRecordsAggregate() { - this(SharedValueManager.EMPTY); + this(SharedValueManager.createEmpty()); } private RowRecordsAggregate(SharedValueManager svm) { + if (svm == null) { + throw new IllegalArgumentException("SharedValueManager must be provided."); + } _rowRecords = new TreeMap(); _valuesAgg = new ValueRecordsAggregate(); _unknownRecords = new ArrayList(); @@ -68,6 +71,8 @@ public final class RowRecordsAggregate extends RecordAggregate { /** * @param rs record stream with all {@link SharedFormulaRecord} * {@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 null. */ public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) { this(svm); diff --git a/src/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java b/src/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java index 8c4d19d2b8..4327ee63af 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/SharedValueManager.java @@ -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]); + } private final ArrayRecord[] _arrayRecords; private final TableRecord[] _tableRecords; private final Map _groupsBySharedFormulaRecord; @@ -144,7 +150,7 @@ public final class SharedValueManager { public static SharedValueManager create(SharedFormulaRecord[] sharedFormulaRecords, CellReference[] firstCells, ArrayRecord[] arrayRecords, TableRecord[] tableRecords) { if (sharedFormulaRecords.length + firstCells.length + arrayRecords.length + tableRecords.length < 1) { - return EMPTY; + return createEmpty(); } return new SharedValueManager(sharedFormulaRecords, firstCells, arrayRecords, tableRecords); } diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java b/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java index 48449f40f0..aedabd0d57 100644 --- a/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java +++ b/src/testcases/org/apache/poi/hssf/record/aggregates/TestFormulaRecordAggregate.java @@ -37,7 +37,7 @@ public final class TestFormulaRecordAggregate extends TestCase { f.setCachedResultTypeString(); StringRecord s = new StringRecord(); s.setString("abc"); - FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.EMPTY); + FormulaRecordAggregate fagg = new FormulaRecordAggregate(f, s, SharedValueManager.createEmpty()); assertEquals("abc", fagg.getStringValue()); } @@ -54,7 +54,7 @@ public final class TestFormulaRecordAggregate extends TestCase { fr.setValue(2.0); StringRecord sr = new StringRecord(); sr.setString("NA"); - SharedValueManager svm = SharedValueManager.EMPTY; + SharedValueManager svm = SharedValueManager.createEmpty(); FormulaRecordAggregate fra; try { diff --git a/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java b/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java index 6c0ae1d3ae..14a968612d 100644 --- a/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java +++ b/src/testcases/org/apache/poi/hssf/record/aggregates/TestRowRecordsAggregate.java @@ -63,24 +63,24 @@ public final class TestRowRecordsAggregate extends TestCase { /** * 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 FormulaRecord}s that they apply to (and only once).
*/ public void testArraysAndTables() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("testArraysAndTables.xls"); Record[] sheetRecs = RecordInspector.getRecords(wb.getSheetAt(0), 0); - + int countArrayFormulas = verifySharedValues(sheetRecs, ArrayRecord.class); assertEquals(5, countArrayFormulas); int countTableFormulas = verifySharedValues(sheetRecs, TableRecord.class); assertEquals(3, countTableFormulas); // 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); assertEquals(0, countSharedFormulas); - + if (false) { // set true to observe re-serialized file 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) { - + int result =0; for(int i=0; iPivotTable * records do not get into {@link RowRecordsAggregate}.
- * 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'. */ public void testUnknownContinue_bug46280() { @@ -140,7 +140,7 @@ public final class TestRowRecordsAggregate extends TestCase { RecordStream rs = new RecordStream(Arrays.asList(inRecs), 0); RowRecordsAggregate rra; try { - rra = new RowRecordsAggregate(rs, SharedValueManager.EMPTY); + rra = new RowRecordsAggregate(rs, SharedValueManager.createEmpty()); } catch (RuntimeException e) { if (e.getMessage().startsWith("Unexpected record type")) { throw new AssertionFailedError("Identified bug 46280a");