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.

CellValue.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.usermodel;
  16. import org.apache.poi.ss.formula.eval.ErrorEval;
  17. /**
  18. * Mimics the 'data view' of a cell. This allows formula evaluator
  19. * to return a CellValue instead of precasting the value to String
  20. * or Number or boolean type.
  21. */
  22. public final class CellValue {
  23. public static final CellValue TRUE = new CellValue(CellType.BOOLEAN, 0.0, true, null, 0);
  24. public static final CellValue FALSE= new CellValue(CellType.BOOLEAN, 0.0, false, null, 0);
  25. private final CellType _cellType;
  26. private final double _numberValue;
  27. private final boolean _booleanValue;
  28. private final String _textValue;
  29. private final int _errorCode;
  30. private CellValue(CellType cellType, double numberValue, boolean booleanValue,
  31. String textValue, int errorCode) {
  32. _cellType = cellType;
  33. _numberValue = numberValue;
  34. _booleanValue = booleanValue;
  35. _textValue = textValue;
  36. _errorCode = errorCode;
  37. }
  38. public CellValue(double numberValue) {
  39. this(CellType.NUMERIC, numberValue, false, null, 0);
  40. }
  41. public static CellValue valueOf(boolean booleanValue) {
  42. return booleanValue ? TRUE : FALSE;
  43. }
  44. public CellValue(String stringValue) {
  45. this(CellType.STRING, 0.0, false, stringValue, 0);
  46. }
  47. public static CellValue getError(int errorCode) {
  48. return new CellValue(CellType.ERROR, 0.0, false, null, errorCode);
  49. }
  50. /**
  51. * @return Returns the booleanValue.
  52. */
  53. public boolean getBooleanValue() {
  54. return _booleanValue;
  55. }
  56. /**
  57. * @return Returns the numberValue.
  58. */
  59. public double getNumberValue() {
  60. return _numberValue;
  61. }
  62. /**
  63. * @return Returns the stringValue.
  64. */
  65. public String getStringValue() {
  66. return _textValue;
  67. }
  68. /**
  69. * @return Returns the cellType.
  70. * @since POI 3.15
  71. */
  72. public CellType getCellTypeEnum() {
  73. return _cellType;
  74. }
  75. /**
  76. * @return Returns the cellType.
  77. * @deprecated POI 3.15. Use {@link #getCellTypeEnum()} instead.
  78. * In the future, the signature of this method will be changed to return a
  79. * {@link CellType}.
  80. */
  81. @Deprecated
  82. public int getCellType() {
  83. return _cellType.getCode();
  84. }
  85. /**
  86. * @return Returns the errorValue.
  87. */
  88. public byte getErrorValue() {
  89. return (byte) _errorCode;
  90. }
  91. public String toString() {
  92. StringBuffer sb = new StringBuffer(64);
  93. sb.append(getClass().getName()).append(" [");
  94. sb.append(formatAsString());
  95. sb.append("]");
  96. return sb.toString();
  97. }
  98. public String formatAsString() {
  99. switch (_cellType) {
  100. case NUMERIC:
  101. return String.valueOf(_numberValue);
  102. case STRING:
  103. return '"' + _textValue + '"';
  104. case BOOLEAN:
  105. return _booleanValue ? "TRUE" : "FALSE";
  106. case ERROR:
  107. return ErrorEval.getText(_errorCode);
  108. default:
  109. return "<error unexpected cell type " + _cellType + ">";
  110. }
  111. }
  112. }