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.

ForkedEvaluationCell.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.eval.forked;
  16. import org.apache.poi.ss.formula.eval.BlankEval;
  17. import org.apache.poi.ss.formula.eval.BoolEval;
  18. import org.apache.poi.ss.formula.eval.ErrorEval;
  19. import org.apache.poi.ss.formula.eval.NumberEval;
  20. import org.apache.poi.ss.formula.eval.StringEval;
  21. import org.apache.poi.ss.formula.eval.ValueEval;
  22. import org.apache.poi.ss.formula.EvaluationCell;
  23. import org.apache.poi.ss.formula.EvaluationSheet;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. /**
  26. * Represents a cell being used for forked evaluation that has had a value set different from the
  27. * corresponding cell in the shared master workbook.
  28. *
  29. * @author Josh Micich
  30. */
  31. final class ForkedEvaluationCell implements EvaluationCell {
  32. private final EvaluationSheet _sheet;
  33. /** corresponding cell from master workbook */
  34. private final EvaluationCell _masterCell;
  35. private boolean _booleanValue;
  36. private int _cellType;
  37. private int _errorValue;
  38. private double _numberValue;
  39. private String _stringValue;
  40. public ForkedEvaluationCell(ForkedEvaluationSheet sheet, EvaluationCell masterCell) {
  41. _sheet = sheet;
  42. _masterCell = masterCell;
  43. // start with value blank, but expect construction to be immediately
  44. setValue(BlankEval.instance); // followed by a proper call to setValue()
  45. }
  46. public Object getIdentityKey() {
  47. return _masterCell.getIdentityKey();
  48. }
  49. public void setValue(ValueEval value) {
  50. Class<? extends ValueEval> cls = value.getClass();
  51. if (cls == NumberEval.class) {
  52. _cellType = Cell.CELL_TYPE_NUMERIC;
  53. _numberValue = ((NumberEval)value).getNumberValue();
  54. return;
  55. }
  56. if (cls == StringEval.class) {
  57. _cellType = Cell.CELL_TYPE_STRING;
  58. _stringValue = ((StringEval)value).getStringValue();
  59. return;
  60. }
  61. if (cls == BoolEval.class) {
  62. _cellType = Cell.CELL_TYPE_BOOLEAN;
  63. _booleanValue = ((BoolEval)value).getBooleanValue();
  64. return;
  65. }
  66. if (cls == ErrorEval.class) {
  67. _cellType = Cell.CELL_TYPE_ERROR;
  68. _errorValue = ((ErrorEval)value).getErrorCode();
  69. return;
  70. }
  71. if (cls == BlankEval.class) {
  72. _cellType = Cell.CELL_TYPE_BLANK;
  73. return;
  74. }
  75. throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")");
  76. }
  77. public void copyValue(Cell destCell) {
  78. switch (_cellType) {
  79. case Cell.CELL_TYPE_BLANK: destCell.setCellType(Cell.CELL_TYPE_BLANK); return;
  80. case Cell.CELL_TYPE_NUMERIC: destCell.setCellValue(_numberValue); return;
  81. case Cell.CELL_TYPE_BOOLEAN: destCell.setCellValue(_booleanValue); return;
  82. case Cell.CELL_TYPE_STRING: destCell.setCellValue(_stringValue); return;
  83. case Cell.CELL_TYPE_ERROR: destCell.setCellErrorValue((byte)_errorValue); return;
  84. }
  85. throw new IllegalStateException("Unexpected data type (" + _cellType + ")");
  86. }
  87. private void checkCellType(int expectedCellType) {
  88. if (_cellType != expectedCellType) {
  89. throw new RuntimeException("Wrong data type (" + _cellType + ")");
  90. }
  91. }
  92. public int getCellType() {
  93. return _cellType;
  94. }
  95. public boolean getBooleanCellValue() {
  96. checkCellType(Cell.CELL_TYPE_BOOLEAN);
  97. return _booleanValue;
  98. }
  99. public int getErrorCellValue() {
  100. checkCellType(Cell.CELL_TYPE_ERROR);
  101. return _errorValue;
  102. }
  103. public double getNumericCellValue() {
  104. checkCellType(Cell.CELL_TYPE_NUMERIC);
  105. return _numberValue;
  106. }
  107. public String getStringCellValue() {
  108. checkCellType(Cell.CELL_TYPE_STRING);
  109. return _stringValue;
  110. }
  111. public EvaluationSheet getSheet() {
  112. return _sheet;
  113. }
  114. public int getRowIndex() {
  115. return _masterCell.getRowIndex();
  116. }
  117. public int getColumnIndex() {
  118. return _masterCell.getColumnIndex();
  119. }
  120. public int getCachedFormulaResultType() {
  121. return _masterCell.getCachedFormulaResultType();
  122. }
  123. }