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.7KB

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