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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.EvaluationCell;
  17. import org.apache.poi.ss.formula.EvaluationSheet;
  18. import org.apache.poi.ss.formula.eval.BlankEval;
  19. import org.apache.poi.ss.formula.eval.BoolEval;
  20. import org.apache.poi.ss.formula.eval.ErrorEval;
  21. import org.apache.poi.ss.formula.eval.NumberEval;
  22. import org.apache.poi.ss.formula.eval.StringEval;
  23. import org.apache.poi.ss.formula.eval.ValueEval;
  24. import org.apache.poi.ss.usermodel.Cell;
  25. import org.apache.poi.ss.usermodel.CellType;
  26. import org.apache.poi.ss.util.CellRangeAddress;
  27. /**
  28. * Represents a cell being used for forked evaluation that has had a value set different from the
  29. * corresponding cell in the shared master workbook.
  30. *
  31. * @author Josh Micich
  32. */
  33. final class ForkedEvaluationCell implements EvaluationCell {
  34. private final EvaluationSheet _sheet;
  35. /** corresponding cell from master workbook */
  36. private final EvaluationCell _masterCell;
  37. private boolean _booleanValue;
  38. private CellType _cellType;
  39. private int _errorValue;
  40. private double _numberValue;
  41. private String _stringValue;
  42. public ForkedEvaluationCell(ForkedEvaluationSheet sheet, EvaluationCell masterCell) {
  43. _sheet = sheet;
  44. _masterCell = masterCell;
  45. // start with value blank, but expect construction to be immediately
  46. setValue(BlankEval.instance); // followed by a proper call to setValue()
  47. }
  48. @Override
  49. public Object getIdentityKey() {
  50. return _masterCell.getIdentityKey();
  51. }
  52. public void setValue(ValueEval value) {
  53. Class<? extends ValueEval> cls = value.getClass();
  54. if (cls == NumberEval.class) {
  55. _cellType = CellType.NUMERIC;
  56. _numberValue = ((NumberEval)value).getNumberValue();
  57. return;
  58. }
  59. if (cls == StringEval.class) {
  60. _cellType = CellType.STRING;
  61. _stringValue = ((StringEval)value).getStringValue();
  62. return;
  63. }
  64. if (cls == BoolEval.class) {
  65. _cellType = CellType.BOOLEAN;
  66. _booleanValue = ((BoolEval)value).getBooleanValue();
  67. return;
  68. }
  69. if (cls == ErrorEval.class) {
  70. _cellType = CellType.ERROR;
  71. _errorValue = ((ErrorEval)value).getErrorCode();
  72. return;
  73. }
  74. if (cls == BlankEval.class) {
  75. _cellType = CellType.BLANK;
  76. return;
  77. }
  78. throw new IllegalArgumentException("Unexpected value class (" + cls.getName() + ")");
  79. }
  80. public void copyValue(Cell destCell) {
  81. switch (_cellType) {
  82. case BLANK: destCell.setCellType(CellType.BLANK); return;
  83. case NUMERIC: destCell.setCellValue(_numberValue); return;
  84. case BOOLEAN: destCell.setCellValue(_booleanValue); return;
  85. case STRING: destCell.setCellValue(_stringValue); return;
  86. case ERROR: destCell.setCellErrorValue((byte)_errorValue); return;
  87. default: throw new IllegalStateException("Unexpected data type (" + _cellType + ")");
  88. }
  89. }
  90. private void checkCellType(CellType expectedCellType) {
  91. if (_cellType != expectedCellType) {
  92. throw new RuntimeException("Wrong data type (" + _cellType + ")");
  93. }
  94. }
  95. /**
  96. * Will return {@link CellType} in a future version of POI.
  97. * For forwards compatibility, do not hard-code cell type literals in your code.
  98. *
  99. * @return cell type
  100. * @deprecated 3.15. Will return a {@link CellType} enum in the future.
  101. */
  102. @Override
  103. public int getCellType() {
  104. return _cellType.getCode();
  105. }
  106. /**
  107. * @since POI 3.15 beta 3
  108. * @deprecated POI 3.15 beta 3.
  109. * Will be deleted when we make the CellType enum transition. See bug 59791.
  110. */
  111. @Override
  112. public CellType getCellTypeEnum() {
  113. return _cellType;
  114. }
  115. @Override
  116. public boolean getBooleanCellValue() {
  117. checkCellType(CellType.BOOLEAN);
  118. return _booleanValue;
  119. }
  120. @Override
  121. public int getErrorCellValue() {
  122. checkCellType(CellType.ERROR);
  123. return _errorValue;
  124. }
  125. @Override
  126. public double getNumericCellValue() {
  127. checkCellType(CellType.NUMERIC);
  128. return _numberValue;
  129. }
  130. @Override
  131. public String getStringCellValue() {
  132. checkCellType(CellType.STRING);
  133. return _stringValue;
  134. }
  135. @Override
  136. public EvaluationSheet getSheet() {
  137. return _sheet;
  138. }
  139. @Override
  140. public int getRowIndex() {
  141. return _masterCell.getRowIndex();
  142. }
  143. @Override
  144. public int getColumnIndex() {
  145. return _masterCell.getColumnIndex();
  146. }
  147. @Override
  148. public CellRangeAddress getArrayFormulaRange() {
  149. return _masterCell.getArrayFormulaRange();
  150. }
  151. @Override
  152. public boolean isPartOfArrayFormulaGroup() {
  153. return _masterCell.isPartOfArrayFormulaGroup();
  154. }
  155. /**
  156. * Will return {@link CellType} in a future version of POI.
  157. * For forwards compatibility, do not hard-code cell type literals in your code.
  158. *
  159. * @return cell type of cached formula result
  160. * @deprecated 3.15. Will return a {@link CellType} enum in the future.
  161. */
  162. @Override
  163. public int getCachedFormulaResultType() {
  164. return _masterCell.getCachedFormulaResultType();
  165. }
  166. /**
  167. * @since POI 3.15 beta 3
  168. * @deprecated POI 3.15 beta 3.
  169. * Will be deleted when we make the CellType enum transition. See bug 59791.
  170. */
  171. @Override
  172. public CellType getCachedFormulaResultTypeEnum() {
  173. return _masterCell.getCachedFormulaResultTypeEnum();
  174. }
  175. }