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

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