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.

FormulaUsedBlankCellSet.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import org.apache.poi.ss.util.CellReference;
  21. /**
  22. * Optimization - compacts many blank cell references used by a single formula.
  23. */
  24. final class FormulaUsedBlankCellSet {
  25. public static final class BookSheetKey {
  26. private final int _bookIndex;
  27. private final int _sheetIndex;
  28. public BookSheetKey(int bookIndex, int sheetIndex) {
  29. _bookIndex = bookIndex;
  30. _sheetIndex = sheetIndex;
  31. }
  32. @Override
  33. public int hashCode() {
  34. return _bookIndex * 17 + _sheetIndex;
  35. }
  36. @Override
  37. public boolean equals(Object obj) {
  38. if (!(obj instanceof BookSheetKey)) {
  39. return false;
  40. }
  41. BookSheetKey other = (BookSheetKey) obj;
  42. return _bookIndex == other._bookIndex && _sheetIndex == other._sheetIndex;
  43. }
  44. }
  45. private static final class BlankCellSheetGroup {
  46. private final List<BlankCellRectangleGroup> _rectangleGroups;
  47. private int _currentRowIndex;
  48. private int _firstColumnIndex;
  49. private int _lastColumnIndex;
  50. private BlankCellRectangleGroup _currentRectangleGroup;
  51. private int _lastDefinedRow;
  52. public BlankCellSheetGroup(int lastDefinedRow) {
  53. _rectangleGroups = new ArrayList<>();
  54. _currentRowIndex = -1;
  55. _lastDefinedRow = lastDefinedRow;
  56. }
  57. public void addCell(int rowIndex, int columnIndex) {
  58. if (rowIndex > _lastDefinedRow) return;
  59. if (_currentRowIndex == -1) {
  60. _currentRowIndex = rowIndex;
  61. _firstColumnIndex = columnIndex;
  62. _lastColumnIndex = columnIndex;
  63. } else {
  64. if (_currentRowIndex == rowIndex && _lastColumnIndex+1 == columnIndex) {
  65. _lastColumnIndex = columnIndex;
  66. } else {
  67. // cell does not fit on end of current row
  68. if (_currentRectangleGroup == null) {
  69. _currentRectangleGroup = new BlankCellRectangleGroup(_currentRowIndex, _firstColumnIndex, _lastColumnIndex);
  70. } else {
  71. if (!_currentRectangleGroup.acceptRow(_currentRowIndex, _firstColumnIndex, _lastColumnIndex)) {
  72. _rectangleGroups.add(_currentRectangleGroup);
  73. _currentRectangleGroup = new BlankCellRectangleGroup(_currentRowIndex, _firstColumnIndex, _lastColumnIndex);
  74. }
  75. }
  76. _currentRowIndex = rowIndex;
  77. _firstColumnIndex = columnIndex;
  78. _lastColumnIndex = columnIndex;
  79. }
  80. }
  81. }
  82. public boolean containsCell(int rowIndex, int columnIndex) {
  83. if (rowIndex > _lastDefinedRow) return true;
  84. for (int i=_rectangleGroups.size()-1; i>=0; i--) {
  85. BlankCellRectangleGroup bcrg = _rectangleGroups.get(i);
  86. if (bcrg.containsCell(rowIndex, columnIndex)) {
  87. return true;
  88. }
  89. }
  90. if(_currentRectangleGroup != null && _currentRectangleGroup.containsCell(rowIndex, columnIndex)) {
  91. return true;
  92. }
  93. if (_currentRowIndex != -1 && _currentRowIndex == rowIndex) {
  94. if (_firstColumnIndex <= columnIndex && columnIndex <= _lastColumnIndex) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. }
  101. private static final class BlankCellRectangleGroup {
  102. private final int _firstRowIndex;
  103. private final int _firstColumnIndex;
  104. private final int _lastColumnIndex;
  105. private int _lastRowIndex;
  106. public BlankCellRectangleGroup(int firstRowIndex, int firstColumnIndex, int lastColumnIndex) {
  107. _firstRowIndex = firstRowIndex;
  108. _firstColumnIndex = firstColumnIndex;
  109. _lastColumnIndex = lastColumnIndex;
  110. _lastRowIndex = firstRowIndex;
  111. }
  112. public boolean containsCell(int rowIndex, int columnIndex) {
  113. if (columnIndex < _firstColumnIndex) {
  114. return false;
  115. }
  116. if (columnIndex > _lastColumnIndex) {
  117. return false;
  118. }
  119. if (rowIndex < _firstRowIndex) {
  120. return false;
  121. }
  122. if (rowIndex > _lastRowIndex) {
  123. return false;
  124. }
  125. return true;
  126. }
  127. public boolean acceptRow(int rowIndex, int firstColumnIndex, int lastColumnIndex) {
  128. if (firstColumnIndex != _firstColumnIndex) {
  129. return false;
  130. }
  131. if (lastColumnIndex != _lastColumnIndex) {
  132. return false;
  133. }
  134. if (rowIndex != _lastRowIndex+1) {
  135. return false;
  136. }
  137. _lastRowIndex = rowIndex;
  138. return true;
  139. }
  140. @Override
  141. public String toString() {
  142. StringBuffer sb = new StringBuffer(64);
  143. CellReference crA = new CellReference(_firstRowIndex, _firstColumnIndex, false, false);
  144. CellReference crB = new CellReference(_lastRowIndex, _lastColumnIndex, false, false);
  145. sb.append(getClass().getName());
  146. sb.append(" [").append(crA.formatAsString()).append(':').append(crB.formatAsString()).append("]");
  147. return sb.toString();
  148. }
  149. }
  150. private final Map<BookSheetKey, BlankCellSheetGroup> _sheetGroupsByBookSheet;
  151. public FormulaUsedBlankCellSet() {
  152. _sheetGroupsByBookSheet = new HashMap<>();
  153. }
  154. public void addCell(EvaluationWorkbook evalWorkbook, int bookIndex, int sheetIndex, int rowIndex, int columnIndex) {
  155. BlankCellSheetGroup sbcg = getSheetGroup(evalWorkbook, bookIndex, sheetIndex);
  156. sbcg.addCell(rowIndex, columnIndex);
  157. }
  158. private BlankCellSheetGroup getSheetGroup(EvaluationWorkbook evalWorkbook, int bookIndex, int sheetIndex) {
  159. BookSheetKey key = new BookSheetKey(bookIndex, sheetIndex);
  160. BlankCellSheetGroup result = _sheetGroupsByBookSheet.get(key);
  161. if (result == null) {
  162. result = new BlankCellSheetGroup(evalWorkbook.getSheet(sheetIndex).getlastRowNum());
  163. _sheetGroupsByBookSheet.put(key, result);
  164. }
  165. return result;
  166. }
  167. public boolean containsCell(BookSheetKey key, int rowIndex, int columnIndex) {
  168. BlankCellSheetGroup bcsg = _sheetGroupsByBookSheet.get(key);
  169. if (bcsg == null) {
  170. return false;
  171. }
  172. return bcsg.containsCell(rowIndex, columnIndex);
  173. }
  174. public boolean isEmpty() {
  175. return _sheetGroupsByBookSheet.isEmpty();
  176. }
  177. }