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.

RefPtgBase.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.formula;
  16. import org.apache.poi.ss.util.CellReference;
  17. import org.apache.poi.util.BitField;
  18. import org.apache.poi.util.BitFieldFactory;
  19. import org.apache.poi.util.LittleEndianInput;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. /**
  22. * ReferencePtgBase - handles references (such as A1, A2, IA4)
  23. *
  24. * @author Andrew C. Oliver (acoliver@apache.org)
  25. * @author Jason Height (jheight at chariot dot net dot au)
  26. */
  27. public abstract class RefPtgBase extends OperandPtg {
  28. /** The row index - zero based unsigned 16 bit value */
  29. private int field_1_row;
  30. /**
  31. * Field 2 - lower 8 bits is the zero based unsigned byte column index - bit
  32. * 16 - isRowRelative - bit 15 - isColumnRelative
  33. */
  34. private int field_2_col;
  35. private static final BitField rowRelative = BitFieldFactory.getInstance(0x8000);
  36. private static final BitField colRelative = BitFieldFactory.getInstance(0x4000);
  37. /**
  38. * YK: subclasses of RefPtgBase are used by the FormulaParser and FormulaEvaluator accross HSSF and XSSF.
  39. * The bit mask should accomodate the maximum number of avaiable columns, i.e. 0x3FFF.
  40. *
  41. * @see org.apache.poi.ss.SpreadsheetVersion
  42. */
  43. private static final BitField column = BitFieldFactory.getInstance(0x3FFF);
  44. protected RefPtgBase() {
  45. // Required for clone methods
  46. }
  47. protected RefPtgBase(CellReference c) {
  48. setRow(c.getRow());
  49. setColumn(c.getCol());
  50. setColRelative(!c.isColAbsolute());
  51. setRowRelative(!c.isRowAbsolute());
  52. }
  53. protected final void readCoordinates(LittleEndianInput in) {
  54. field_1_row = in.readUShort();
  55. field_2_col = in.readUShort();
  56. }
  57. protected final void writeCoordinates(LittleEndianOutput out) {
  58. out.writeShort(field_1_row);
  59. out.writeShort(field_2_col);
  60. }
  61. public final void setRow(int rowIndex) {
  62. field_1_row = rowIndex;
  63. }
  64. /**
  65. * @return the row number as an int
  66. */
  67. public final int getRow() {
  68. return field_1_row;
  69. }
  70. public final boolean isRowRelative() {
  71. return rowRelative.isSet(field_2_col);
  72. }
  73. public final void setRowRelative(boolean rel) {
  74. field_2_col = rowRelative.setBoolean(field_2_col, rel);
  75. }
  76. public final boolean isColRelative() {
  77. return colRelative.isSet(field_2_col);
  78. }
  79. public final void setColRelative(boolean rel) {
  80. field_2_col = colRelative.setBoolean(field_2_col, rel);
  81. }
  82. public final void setColumn(int col) {
  83. field_2_col = column.setValue(field_2_col, col);
  84. }
  85. public final int getColumn() {
  86. return column.getValue(field_2_col);
  87. }
  88. protected final String formatReferenceAsString() {
  89. // Only make cell references as needed. Memory is an issue
  90. CellReference cr = new CellReference(getRow(), getColumn(), !isRowRelative(), !isColRelative());
  91. return cr.formatAsString();
  92. }
  93. public final byte getDefaultOperandClass() {
  94. return Ptg.CLASS_REF;
  95. }
  96. }