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.

BoolErrRecord.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.hssf.record;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.ss.usermodel.FormulaError;
  19. import org.apache.poi.util.GenericRecordUtil;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. import org.apache.poi.util.RecordFormatException;
  22. /**
  23. * Creates new BoolErrRecord. (0x0205)
  24. */
  25. public final class BoolErrRecord extends CellRecord {
  26. public static final short sid = 0x0205;
  27. private int _value;
  28. /**
  29. * If <code>true</code>, this record represents an error cell value,
  30. * otherwise this record represents a boolean cell value
  31. */
  32. private boolean _isError;
  33. /** Creates new BoolErrRecord */
  34. public BoolErrRecord() {}
  35. public BoolErrRecord(BoolErrRecord other) {
  36. super(other);
  37. _value = other._value;
  38. _isError = other._isError;
  39. }
  40. /**
  41. * @param in the RecordInputstream to read the record from
  42. */
  43. public BoolErrRecord(RecordInputStream in) {
  44. super(in);
  45. switch (in.remaining()) {
  46. case 2:
  47. _value = in.readByte();
  48. break;
  49. case 3:
  50. _value = in.readUShort();
  51. break;
  52. default:
  53. throw new RecordFormatException("Unexpected size ("
  54. + in.remaining() + ") for BOOLERR record.");
  55. }
  56. int flag = in.readUByte();
  57. switch (flag) {
  58. case 0:
  59. _isError = false;
  60. break;
  61. case 1:
  62. _isError = true;
  63. break;
  64. default:
  65. throw new RecordFormatException("Unexpected isError flag ("
  66. + flag + ") for BOOLERR record.");
  67. }
  68. }
  69. /**
  70. * set the boolean value for the cell
  71. *
  72. * @param value representing the boolean value
  73. */
  74. public void setValue(boolean value) {
  75. _value = value ? 1 : 0;
  76. _isError = false;
  77. }
  78. /**
  79. * set the error value for the cell. See {@link FormulaError} for valid codes.
  80. *
  81. * @param value error representing the error value
  82. * this value can only be 0,7,15,23,29,36 or 42
  83. * see bugzilla bug 16560 for an explanation
  84. */
  85. public void setValue(byte value) {
  86. setValue(FormulaError.forInt(value));
  87. }
  88. /**
  89. * set the error value for the cell
  90. *
  91. * @param value error representing the error value
  92. * this value can only be 0,7,15,23,29,36 or 42
  93. * see bugzilla bug 16560 for an explanation
  94. */
  95. public void setValue(FormulaError value) {
  96. switch(value) {
  97. case NULL:
  98. case DIV0:
  99. case VALUE:
  100. case REF:
  101. case NAME:
  102. case NUM:
  103. case NA:
  104. _value = value.getCode();
  105. _isError = true;
  106. return;
  107. default:
  108. throw new IllegalArgumentException("Error Value can only be 0,7,15,23,29,36 or 42. It cannot be "+value.getCode()+" ("+value+")");
  109. }
  110. }
  111. /**
  112. * get the value for the cell
  113. *
  114. * @return boolean representing the boolean value
  115. */
  116. public boolean getBooleanValue() {
  117. return _value != 0;
  118. }
  119. /**
  120. * get the error value for the cell
  121. *
  122. * @return byte representing the error value
  123. */
  124. public byte getErrorValue() {
  125. return (byte)_value;
  126. }
  127. /**
  128. * Indicates whether the call holds a boolean value
  129. *
  130. * @return boolean true if the cell holds a boolean value
  131. */
  132. public boolean isBoolean() {
  133. return !_isError;
  134. }
  135. /**
  136. * Indicates whether the call holds an error value
  137. *
  138. * @return boolean true if the cell holds an error value
  139. */
  140. public boolean isError() {
  141. return _isError;
  142. }
  143. @Override
  144. protected String getRecordName() {
  145. return "BOOLERR";
  146. }
  147. @Override
  148. protected void serializeValue(LittleEndianOutput out) {
  149. out.writeByte(_value);
  150. out.writeByte(_isError ? 1 : 0);
  151. }
  152. @Override
  153. protected int getValueDataSize() {
  154. return 2;
  155. }
  156. public short getSid() {
  157. return sid;
  158. }
  159. @Override
  160. public BoolErrRecord copy() {
  161. return new BoolErrRecord(this);
  162. }
  163. @Override
  164. public HSSFRecordTypes getGenericRecordType() {
  165. return HSSFRecordTypes.BOOL_ERR;
  166. }
  167. @Override
  168. public Map<String, Supplier<?>> getGenericProperties() {
  169. return GenericRecordUtil.getGenericProperties(
  170. "base", super::getGenericProperties,
  171. "isBoolean", this::isBoolean,
  172. "booleanVal", this::getBooleanValue,
  173. "isError", this::isError,
  174. "errorVal", this::getErrorValue,
  175. "errorTxt", () -> isError() ? FormulaError.forInt(getErrorValue()).getString() : null
  176. );
  177. }
  178. }