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.

SharedValueRecordBase.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 org.apache.poi.hssf.util.CellRangeAddress8Bit;
  17. import org.apache.poi.util.LittleEndianInput;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. /**
  20. * Common base class for {@link SharedFormulaRecord}, {@link ArrayRecord} and
  21. * {@link TableRecord} which are have similarities.
  22. *
  23. * @author Josh Micich
  24. */
  25. public abstract class SharedValueRecordBase extends StandardRecord {
  26. private CellRangeAddress8Bit _range;
  27. protected SharedValueRecordBase(CellRangeAddress8Bit range) {
  28. _range = range;
  29. }
  30. protected SharedValueRecordBase() {
  31. this(new CellRangeAddress8Bit(0, 0, 0, 0));
  32. }
  33. /**
  34. * reads only the range (1 {@link CellRangeAddress8Bit}) from the stream
  35. */
  36. public SharedValueRecordBase(LittleEndianInput in) {
  37. _range = new CellRangeAddress8Bit(in);
  38. }
  39. public final CellRangeAddress8Bit getRange() {
  40. return _range;
  41. }
  42. public final int getFirstRow() {
  43. return _range.getFirstRow();
  44. }
  45. public final int getLastRow() {
  46. return _range.getLastRow();
  47. }
  48. public final int getFirstColumn() {
  49. return (short) _range.getFirstColumn();
  50. }
  51. public final int getLastColumn() {
  52. return (short) _range.getLastColumn();
  53. }
  54. protected int getDataSize() {
  55. return CellRangeAddress8Bit.ENCODED_SIZE + getExtraDataSize();
  56. }
  57. protected abstract int getExtraDataSize();
  58. protected abstract void serializeExtraData(LittleEndianOutput out);
  59. public void serialize(LittleEndianOutput out) {
  60. _range.serialize(out);
  61. serializeExtraData(out);
  62. }
  63. /**
  64. * @return <code>true</code> if (rowIx, colIx) is within the range ({@link #getRange()})
  65. * of this shared value object.
  66. */
  67. public final boolean isInRange(int rowIx, int colIx) {
  68. CellRangeAddress8Bit r = _range;
  69. return r.getFirstRow() <= rowIx
  70. && r.getLastRow() >= rowIx
  71. && r.getFirstColumn() <= colIx
  72. && r.getLastColumn() >= colIx;
  73. }
  74. /**
  75. * @return <code>true</code> if (rowIx, colIx) describes the first cell in this shared value
  76. * object's range ({@link #getRange()})
  77. */
  78. public final boolean isFirstCell(int rowIx, int colIx) {
  79. CellRangeAddress8Bit r = getRange();
  80. return r.getFirstRow() == rowIx && r.getFirstColumn() == colIx;
  81. }
  82. }