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.

CacheAreaEval.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 org.apache.poi.ss.formula.eval.AreaEval;
  17. import org.apache.poi.ss.formula.eval.AreaEvalBase;
  18. import org.apache.poi.ss.formula.eval.BlankEval;
  19. import org.apache.poi.ss.formula.eval.ValueEval;
  20. import org.apache.poi.ss.formula.ptg.AreaI;
  21. import org.apache.poi.ss.formula.ptg.AreaI.OffsetArea;
  22. import org.apache.poi.ss.util.CellReference;
  23. /**
  24. * @author Robert Hulbert
  25. * Provides holding structure for temporary values in arrays during the evaluation process.
  26. * As such, Row/Column references do not actually correspond to data in the file.
  27. */
  28. public final class CacheAreaEval extends AreaEvalBase {
  29. /* Value Containter */
  30. private final ValueEval[] _values;
  31. public CacheAreaEval(AreaI ptg, ValueEval[] values) {
  32. super(ptg);
  33. _values = values;
  34. }
  35. public CacheAreaEval(int firstRow, int firstColumn, int lastRow, int lastColumn, ValueEval[] values) {
  36. super(firstRow, firstColumn, lastRow, lastColumn);
  37. _values = values;
  38. }
  39. public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) {
  40. return getRelativeValue(-1, relativeRowIndex, relativeColumnIndex);
  41. }
  42. public ValueEval getRelativeValue(int sheetIndex, int relativeRowIndex, int relativeColumnIndex) {
  43. int oneDimensionalIndex = relativeRowIndex * getWidth() + relativeColumnIndex;
  44. return _values[oneDimensionalIndex];
  45. }
  46. public AreaEval offset(int relFirstRowIx, int relLastRowIx,
  47. int relFirstColIx, int relLastColIx) {
  48. AreaI area = new OffsetArea(getFirstRow(), getFirstColumn(),
  49. relFirstRowIx, relLastRowIx, relFirstColIx, relLastColIx);
  50. int height = area.getLastRow() - area.getFirstRow() + 1;
  51. int width = area.getLastColumn() - area.getFirstColumn() + 1;
  52. ValueEval[] newVals = new ValueEval[height * width];
  53. int startRow = area.getFirstRow() - getFirstRow();
  54. int startCol = area.getFirstColumn() - getFirstColumn();
  55. for (int j = 0; j < height; j++) {
  56. for (int i = 0; i < width; i++) {
  57. ValueEval temp;
  58. /* CacheAreaEval is only temporary value representation, does not equal sheet selection
  59. * so any attempts going beyond the selection results in BlankEval
  60. */
  61. if (startRow + j > getLastRow() || startCol + i > getLastColumn()) {
  62. temp = BlankEval.instance;
  63. }
  64. else {
  65. temp = _values[(startRow + j) * getWidth() + (startCol + i)];
  66. }
  67. newVals[j * width + i] = temp;
  68. }
  69. }
  70. return new CacheAreaEval(area, newVals);
  71. }
  72. public TwoDEval getRow(int rowIndex) {
  73. if (rowIndex >= getHeight()) {
  74. throw new IllegalArgumentException("Invalid rowIndex " + rowIndex
  75. + ". Allowable range is (0.." + getHeight() + ").");
  76. }
  77. int absRowIndex = getFirstRow() + rowIndex;
  78. ValueEval[] values = new ValueEval[getWidth()];
  79. for (int i = 0; i < values.length; i++) {
  80. values[i] = getRelativeValue(rowIndex, i);
  81. }
  82. return new CacheAreaEval(absRowIndex, getFirstColumn() , absRowIndex, getLastColumn(), values);
  83. }
  84. public TwoDEval getColumn(int columnIndex) {
  85. if (columnIndex >= getWidth()) {
  86. throw new IllegalArgumentException("Invalid columnIndex " + columnIndex
  87. + ". Allowable range is (0.." + getWidth() + ").");
  88. }
  89. int absColIndex = getFirstColumn() + columnIndex;
  90. ValueEval[] values = new ValueEval[getHeight()];
  91. for (int i = 0; i < values.length; i++) {
  92. values[i] = getRelativeValue(i, columnIndex);
  93. }
  94. return new CacheAreaEval(getFirstRow(), absColIndex, getLastRow(), absColIndex, values);
  95. }
  96. public String toString() {
  97. CellReference crA = new CellReference(getFirstRow(), getFirstColumn());
  98. CellReference crB = new CellReference(getLastRow(), getLastColumn());
  99. return getClass().getName() + "[" +
  100. crA.formatAsString() +
  101. ':' +
  102. crB.formatAsString() +
  103. "]";
  104. }
  105. }