You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ForkedEvaluationSheet.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula.eval.forked;
  16. import java.util.Arrays;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. import org.apache.poi.ss.formula.EvaluationCell;
  20. import org.apache.poi.ss.formula.EvaluationSheet;
  21. import org.apache.poi.ss.formula.EvaluationWorkbook;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.util.CellReference;
  26. import org.apache.poi.util.Internal;
  27. /**
  28. * Represents a sheet being used for forked evaluation. Initially, objects of this class contain
  29. * only the cells from the master workbook. By calling {@link #getOrCreateUpdatableCell(int, int)},
  30. * the master cell object is logically replaced with a {@link ForkedEvaluationCell} instance, which
  31. * will be used in all subsequent evaluations.<br>
  32. *
  33. * For POI internal use only
  34. */
  35. @Internal
  36. final class ForkedEvaluationSheet implements EvaluationSheet {
  37. private final EvaluationSheet _masterSheet;
  38. /**
  39. * Only cells which have been split are put in this map. (This has been done to conserve memory).
  40. */
  41. private final Map<RowColKey, ForkedEvaluationCell> _sharedCellsByRowCol;
  42. public ForkedEvaluationSheet(EvaluationSheet masterSheet) {
  43. _masterSheet = masterSheet;
  44. _sharedCellsByRowCol = new HashMap<>();
  45. }
  46. /* (non-Javadoc)
  47. * @see org.apache.poi.ss.formula.EvaluationSheet#getlastRowNum()
  48. * @since POI 4.0.0
  49. */
  50. public int getlastRowNum() {
  51. return _masterSheet.getlastRowNum();
  52. }
  53. @Override
  54. public EvaluationCell getCell(int rowIndex, int columnIndex) {
  55. RowColKey key = new RowColKey(rowIndex, columnIndex);
  56. ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
  57. if (result == null) {
  58. return _masterSheet.getCell(rowIndex, columnIndex);
  59. }
  60. return result;
  61. }
  62. public ForkedEvaluationCell getOrCreateUpdatableCell(int rowIndex, int columnIndex) {
  63. RowColKey key = new RowColKey(rowIndex, columnIndex);
  64. ForkedEvaluationCell result = _sharedCellsByRowCol.get(key);
  65. if (result == null) {
  66. EvaluationCell mcell = _masterSheet.getCell(rowIndex, columnIndex);
  67. if (mcell == null) {
  68. CellReference cr = new CellReference(rowIndex, columnIndex);
  69. throw new UnsupportedOperationException("Underlying cell '"
  70. + cr.formatAsString() + "' is missing in master sheet.");
  71. }
  72. result = new ForkedEvaluationCell(this, mcell);
  73. _sharedCellsByRowCol.put(key, result);
  74. }
  75. return result;
  76. }
  77. public void copyUpdatedCells(Sheet sheet) {
  78. RowColKey[] keys = new RowColKey[_sharedCellsByRowCol.size()];
  79. _sharedCellsByRowCol.keySet().toArray(keys);
  80. Arrays.sort(keys);
  81. for (int i = 0; i < keys.length; i++) {
  82. RowColKey key = keys[i];
  83. Row row = sheet.getRow(key.getRowIndex());
  84. if (row == null) {
  85. row = sheet.createRow(key.getRowIndex());
  86. }
  87. Cell destCell = row.getCell(key.getColumnIndex());
  88. if (destCell == null) {
  89. destCell = row.createCell(key.getColumnIndex());
  90. }
  91. ForkedEvaluationCell srcCell = _sharedCellsByRowCol.get(key);
  92. srcCell.copyValue(destCell);
  93. }
  94. }
  95. public int getSheetIndex(EvaluationWorkbook mewb) {
  96. return mewb.getSheetIndex(_masterSheet);
  97. }
  98. /* (non-Javadoc)
  99. * leave the map alone, if it needs resetting, reusing this class is probably a bad idea.
  100. * @see org.apache.poi.ss.formula.EvaluationSheet#clearAllCachedResultValues()
  101. *
  102. * @since POI 3.15 beta 3
  103. */
  104. @Override
  105. public void clearAllCachedResultValues() {
  106. _masterSheet.clearAllCachedResultValues();
  107. }
  108. // FIXME: serves same purpose as org.apache.poi.xssf.usermodel.XSSFEvaluationSheet$CellKey
  109. private static final class RowColKey implements Comparable<RowColKey>{
  110. private final int _rowIndex;
  111. private final int _columnIndex;
  112. public RowColKey(int rowIndex, int columnIndex) {
  113. _rowIndex = rowIndex;
  114. _columnIndex = columnIndex;
  115. }
  116. @Override
  117. public boolean equals(Object obj) {
  118. if (!(obj instanceof RowColKey)) {
  119. return false;
  120. }
  121. RowColKey other = (RowColKey) obj;
  122. return _rowIndex == other._rowIndex && _columnIndex == other._columnIndex;
  123. }
  124. @Override
  125. public int hashCode() {
  126. return _rowIndex ^ _columnIndex;
  127. }
  128. @Override
  129. public int compareTo(RowColKey o) {
  130. int cmp = _rowIndex - o._rowIndex;
  131. if (cmp != 0) {
  132. return cmp;
  133. }
  134. return _columnIndex - o._columnIndex;
  135. }
  136. public int getRowIndex() {
  137. return _rowIndex;
  138. }
  139. public int getColumnIndex() {
  140. return _columnIndex;
  141. }
  142. }
  143. }