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.

IntPtg.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.ptg;
  16. import java.util.Map;
  17. import java.util.function.Supplier;
  18. import org.apache.poi.util.GenericRecordUtil;
  19. import org.apache.poi.util.LittleEndianInput;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. /**
  22. * Integer (unsigned short integer) Stores an unsigned short value (java int) in a formula
  23. */
  24. public final class IntPtg extends ScalarConstantPtg {
  25. // 16 bit unsigned integer
  26. private static final int MIN_VALUE = 0x0000;
  27. private static final int MAX_VALUE = 0xFFFF;
  28. /**
  29. * Excel represents integers 0..65535 with the tInt token.
  30. *
  31. * @return <code>true</code> if the specified value is within the range of values
  32. * <tt>IntPtg</tt> can represent.
  33. */
  34. public static boolean isInRange(int i) {
  35. return i >= MIN_VALUE && i <= MAX_VALUE;
  36. }
  37. public static final int SIZE = 3;
  38. public static final byte sid = 0x1e;
  39. private final int field_1_value;
  40. public IntPtg(LittleEndianInput in) {
  41. this(in.readUShort());
  42. }
  43. public IntPtg(int value) {
  44. if (!isInRange(value)) {
  45. throw new IllegalArgumentException("value is out of range: " + value);
  46. }
  47. field_1_value = value;
  48. }
  49. public int getValue() {
  50. return field_1_value;
  51. }
  52. public void write(LittleEndianOutput out) {
  53. out.writeByte(sid + getPtgClass());
  54. out.writeShort(getValue());
  55. }
  56. @Override
  57. public byte getSid() {
  58. return sid;
  59. }
  60. public int getSize() {
  61. return SIZE;
  62. }
  63. public String toFormulaString() {
  64. return String.valueOf(getValue());
  65. }
  66. @Override
  67. public IntPtg copy() {
  68. return this;
  69. }
  70. @Override
  71. public Map<String, Supplier<?>> getGenericProperties() {
  72. return GenericRecordUtil.getGenericProperties("value", this::getValue);
  73. }
  74. }