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.

XSSFEvaluationSheet.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.xssf.usermodel;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import org.apache.poi.ss.formula.EvaluationCell;
  19. import org.apache.poi.ss.formula.EvaluationSheet;
  20. import org.apache.poi.ss.usermodel.Cell;
  21. import org.apache.poi.ss.usermodel.Row;
  22. import org.apache.poi.util.Internal;
  23. /**
  24. * XSSF wrapper for a sheet under evaluation
  25. */
  26. @Internal
  27. final class XSSFEvaluationSheet implements EvaluationSheet {
  28. private final XSSFSheet _xs;
  29. private Map<CellKey, EvaluationCell> _cellCache;
  30. public XSSFEvaluationSheet(XSSFSheet sheet) {
  31. _xs = sheet;
  32. }
  33. public XSSFSheet getXSSFSheet() {
  34. return _xs;
  35. }
  36. /* (non-Javadoc)
  37. * @see org.apache.poi.ss.formula.EvaluationSheet#getlastRowNum()
  38. * @since POI 4.0.0
  39. */
  40. @Override
  41. public int getLastRowNum() {
  42. return _xs.getLastRowNum();
  43. }
  44. /* (non-Javadoc)
  45. * @see org.apache.poi.ss.formula.EvaluationSheet#isRowHidden(int)
  46. * @since POI 4.1.0
  47. */
  48. @Override
  49. public boolean isRowHidden(int rowIndex) {
  50. final XSSFRow row = _xs.getRow(rowIndex);
  51. if (row == null) return false;
  52. return row.getZeroHeight();
  53. }
  54. /* (non-JavaDoc), inherit JavaDoc from EvaluationWorkbook
  55. * @since POI 3.15 beta 3
  56. */
  57. @Override
  58. public void clearAllCachedResultValues() {
  59. _cellCache = null;
  60. }
  61. @Override
  62. public EvaluationCell getCell(int rowIndex, int columnIndex) {
  63. // shortcut evaluation if reference is outside the bounds of existing data
  64. // see issue #61841 for impact on VLOOKUP in particular
  65. if (rowIndex > getLastRowNum()) {
  66. return null;
  67. }
  68. // cache for performance: ~30% speedup due to caching
  69. if (_cellCache == null) {
  70. _cellCache = new HashMap<>(_xs.getLastRowNum() * 3);
  71. for (final Row row : _xs) {
  72. final int rowNum = row.getRowNum();
  73. for (final Cell cell : row) {
  74. // cast is safe, the iterator is just defined using the interface
  75. final CellKey key = new CellKey(rowNum, cell.getColumnIndex());
  76. final EvaluationCell evalcell = new XSSFEvaluationCell((XSSFCell) cell, this);
  77. _cellCache.put(key, evalcell);
  78. }
  79. }
  80. }
  81. final CellKey key = new CellKey(rowIndex, columnIndex);
  82. EvaluationCell evalcell = _cellCache.get(key);
  83. // If cache is stale, update cache with this one cell
  84. // This is a compromise between rebuilding the entire cache
  85. // (which would quickly defeat the benefit of the cache)
  86. // and not caching at all.
  87. // See bug 59958: Add cells on the fly to the evaluation sheet cache on cache miss
  88. if (evalcell == null) {
  89. XSSFRow row = _xs.getRow(rowIndex);
  90. if (row == null) {
  91. return null;
  92. }
  93. XSSFCell cell = row.getCell(columnIndex);
  94. if (cell == null) {
  95. return null;
  96. }
  97. evalcell = new XSSFEvaluationCell(cell, this);
  98. _cellCache.put(key, evalcell);
  99. }
  100. return evalcell;
  101. }
  102. private static class CellKey {
  103. private final int _row;
  104. private final int _col;
  105. private int _hash = -1; //lazily computed
  106. protected CellKey(int row, int col) {
  107. _row = row;
  108. _col = col;
  109. }
  110. @Override
  111. public int hashCode() {
  112. if ( _hash == -1 ) {
  113. _hash = (17 * 37 + _row) * 37 + _col;
  114. }
  115. return _hash;
  116. }
  117. @Override
  118. public boolean equals(Object obj) {
  119. if (!(obj instanceof CellKey)) {
  120. return false;
  121. }
  122. // assumes other object is one of us, otherwise ClassCastException is thrown
  123. final CellKey oKey = (CellKey) obj;
  124. return _row == oKey._row && _col == oKey._col;
  125. }
  126. }
  127. }