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.

HSSFRow.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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.usermodel;
  16. import java.util.Iterator;
  17. import java.util.NoSuchElementException;
  18. import org.apache.poi.hssf.record.CellValueRecordInterface;
  19. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  20. import org.apache.poi.hssf.record.RowRecord;
  21. import org.apache.poi.ss.SpreadsheetVersion;
  22. import org.apache.poi.ss.usermodel.Cell;
  23. import org.apache.poi.ss.usermodel.CellStyle;
  24. import org.apache.poi.ss.usermodel.CellType;
  25. import org.apache.poi.ss.usermodel.Row;
  26. import org.apache.poi.ss.usermodel.helpers.RowShifter;
  27. import org.apache.poi.util.Configurator;
  28. /**
  29. * High level representation of a row of a spreadsheet.
  30. *
  31. * Only rows that have cells should be added to a Sheet.
  32. */
  33. public final class HSSFRow implements Row, Comparable<HSSFRow> {
  34. // used for collections
  35. public static final int INITIAL_CAPACITY = Configurator.getIntValue("HSSFRow.ColInitialCapacity", 5);
  36. private int rowNum;
  37. private HSSFCell[] cells;
  38. /**
  39. * reference to low level representation
  40. */
  41. private final RowRecord row;
  42. /**
  43. * reference to containing low level Workbook
  44. */
  45. private final HSSFWorkbook book;
  46. /**
  47. * reference to containing Sheet
  48. */
  49. private final HSSFSheet sheet;
  50. /**
  51. * Creates new HSSFRow from scratch. Only HSSFSheet should do this.
  52. *
  53. * @param book low-level Workbook object containing the sheet that contains this row
  54. * @param sheet low-level Sheet object that contains this Row
  55. * @param rowNum the row number of this row (0 based)
  56. * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int)
  57. */
  58. HSSFRow(HSSFWorkbook book, HSSFSheet sheet, int rowNum) {
  59. this(book, sheet, new RowRecord(rowNum));
  60. }
  61. /**
  62. * Creates an HSSFRow from a low level RowRecord object. Only HSSFSheet should do
  63. * this. HSSFSheet uses this when an existing file is read in.
  64. *
  65. * @param book low-level Workbook object containing the sheet that contains this row
  66. * @param sheet low-level Sheet object that contains this Row
  67. * @param record the low level api object this row should represent
  68. * @see org.apache.poi.hssf.usermodel.HSSFSheet#createRow(int)
  69. */
  70. HSSFRow(HSSFWorkbook book, HSSFSheet sheet, RowRecord record) {
  71. this.book = book;
  72. this.sheet = sheet;
  73. row = record;
  74. setRowNum(record.getRowNumber());
  75. // Size the initial cell list such that a read only case won't waste
  76. // lots of memory, and a create/read followed by adding new cells can
  77. // add a bit without needing a resize
  78. cells = new HSSFCell[record.getLastCol()+INITIAL_CAPACITY];
  79. // Don't trust colIx boundaries as read by other apps
  80. // set the RowRecord empty for the moment
  81. record.setEmpty();
  82. // subsequent calls to createCellFromRecord() will update the colIx boundaries properly
  83. }
  84. /**
  85. * Use this to create new cells within the row and return it.
  86. * <p>
  87. * The cell that is returned is a {@link CellType#BLANK}. The type can be changed
  88. * either through calling <code>setCellValue</code> or <code>setCellType</code>.
  89. *
  90. * @param column - the column number this cell represents
  91. *
  92. * @return HSSFCell a high level representation of the created cell.
  93. * @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
  94. * the maximum number of columns supported by the Excel binary format (.xls)
  95. */
  96. @Override
  97. public HSSFCell createCell(int column)
  98. {
  99. return this.createCell(column,CellType.BLANK);
  100. }
  101. /**
  102. * Use this to create new cells within the row and return it.
  103. * <p>
  104. * The cell that is returned will be of the requested type.
  105. * The type can be changed either through calling setCellValue
  106. * or setCellType, but there is a small overhead to doing this,
  107. * so it is best to create the required type up front.
  108. *
  109. * @param columnIndex - the column number this cell represents
  110. *
  111. * @return HSSFCell a high level representation of the created cell.
  112. * @throws IllegalArgumentException if columnIndex < 0 or greater than 255,
  113. * the maximum number of columns supported by the Excel binary format (.xls)
  114. */
  115. @Override
  116. public HSSFCell createCell(int columnIndex, CellType type)
  117. {
  118. short shortCellNum = (short)columnIndex;
  119. if(columnIndex > 0x7FFF) {
  120. shortCellNum = (short)(0xffff - columnIndex);
  121. }
  122. HSSFCell cell = new HSSFCell(book, sheet, getRowNum(), shortCellNum, type);
  123. addCell(cell);
  124. sheet.getSheet().addValueRecord(getRowNum(), cell.getCellValueRecord());
  125. return cell;
  126. }
  127. /**
  128. * remove the HSSFCell from this row.
  129. * @param cell to remove
  130. */
  131. @Override
  132. public void removeCell(Cell cell) {
  133. if(cell == null) {
  134. throw new IllegalArgumentException("cell must not be null");
  135. }
  136. removeCell((HSSFCell)cell, true);
  137. }
  138. private void removeCell(HSSFCell cell, boolean alsoRemoveRecords) {
  139. int column=cell.getColumnIndex();
  140. if(column < 0) {
  141. throw new RuntimeException("Negative cell indexes not allowed");
  142. }
  143. if(column >= cells.length || cell != cells[column]) {
  144. throw new RuntimeException("Specified cell is not from this row");
  145. }
  146. if(cell.isPartOfArrayFormulaGroup()){
  147. cell.tryToDeleteArrayFormula(null);
  148. }
  149. cells[column]=null;
  150. if(alsoRemoveRecords) {
  151. CellValueRecordInterface cval = cell.getCellValueRecord();
  152. sheet.getSheet().removeValueRecord(getRowNum(), cval);
  153. }
  154. if (cell.getColumnIndex()+1 == row.getLastCol()) {
  155. row.setLastCol(calculateNewLastCellPlusOne(row.getLastCol()));
  156. }
  157. if (cell.getColumnIndex() == row.getFirstCol()) {
  158. row.setFirstCol(calculateNewFirstCell(row.getFirstCol()));
  159. }
  160. }
  161. /**
  162. * Removes all the cells from the row, and their
  163. * records too.
  164. */
  165. protected void removeAllCells() {
  166. for (HSSFCell cell : cells) {
  167. if (cell != null) {
  168. removeCell(cell, true);
  169. }
  170. }
  171. cells=new HSSFCell[INITIAL_CAPACITY];
  172. }
  173. /**
  174. * create a high level HSSFCell object from an existing low level record. Should
  175. * only be called from HSSFSheet or HSSFRow itself.
  176. * @param cell low level cell to create the high level representation from
  177. * @return HSSFCell representing the low level record passed in
  178. */
  179. HSSFCell createCellFromRecord(CellValueRecordInterface cell) {
  180. HSSFCell hcell = new HSSFCell(book, sheet, cell);
  181. addCell(hcell);
  182. int colIx = cell.getColumn();
  183. if (row.isEmpty()) {
  184. row.setFirstCol(colIx);
  185. row.setLastCol(colIx + 1);
  186. } else {
  187. if (colIx < row.getFirstCol()) {
  188. row.setFirstCol(colIx);
  189. } else if (colIx > row.getLastCol()) {
  190. row.setLastCol(colIx + 1);
  191. } /*else {
  192. // added cell is within first and last cells
  193. }*/
  194. }
  195. // TODO - RowRecord column boundaries need to be updated for cell comments too
  196. return hcell;
  197. }
  198. /**
  199. * set the row number of this row.
  200. * @param rowIndex the row number (0-based)
  201. * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535.
  202. */
  203. @Override
  204. public void setRowNum(int rowIndex) {
  205. int maxrow = SpreadsheetVersion.EXCEL97.getLastRowIndex();
  206. if ((rowIndex < 0) || (rowIndex > maxrow)) {
  207. throw new IllegalArgumentException("Invalid row number (" + rowIndex
  208. + ") outside allowable range (0.." + maxrow + ")");
  209. }
  210. rowNum = rowIndex;
  211. if (row != null) {
  212. row.setRowNumber(rowIndex); // used only for KEY comparison (HSSFRow)
  213. }
  214. }
  215. /**
  216. * get row number this row represents
  217. * @return the row number (0 based)
  218. */
  219. @Override
  220. public int getRowNum()
  221. {
  222. return rowNum;
  223. }
  224. /**
  225. * Returns the HSSFSheet this row belongs to
  226. *
  227. * @return the HSSFSheet that owns this row
  228. */
  229. @Override
  230. public HSSFSheet getSheet()
  231. {
  232. return sheet;
  233. }
  234. /**
  235. * Returns the rows outline level. Increased as you
  236. * put it into more groups (outlines), reduced as
  237. * you take it out of them.
  238. */
  239. @Override
  240. public int getOutlineLevel() {
  241. return row.getOutlineLevel();
  242. }
  243. /**
  244. * Moves the supplied cell to a new column, which
  245. * must not already have a cell there!
  246. * @param cell The cell to move
  247. * @param newColumn The new column number (0 based)
  248. */
  249. public void moveCell(HSSFCell cell, short newColumn) {
  250. // Ensure the destination is free
  251. if(cells.length > newColumn && cells[newColumn] != null) {
  252. throw new IllegalArgumentException("Asked to move cell to column " + newColumn + " but there's already a cell there");
  253. }
  254. // Check it's one of ours
  255. if(! cells[cell.getColumnIndex()].equals(cell)) {
  256. throw new IllegalArgumentException("Asked to move a cell, but it didn't belong to our row");
  257. }
  258. // Move the cell to the new position
  259. // (Don't remove the records though)
  260. removeCell(cell, false);
  261. cell.updateCellNum(newColumn);
  262. addCell(cell);
  263. }
  264. /**
  265. * used internally to add a cell.
  266. */
  267. private void addCell(HSSFCell cell) {
  268. int column=cell.getColumnIndex();
  269. // re-allocate cells array as required.
  270. if(column>=cells.length) {
  271. HSSFCell[] oldCells=cells;
  272. // New size based on the same logic as ArrayList
  273. int newSize=oldCells.length*3/2+1;
  274. if(newSize<column+1) {
  275. newSize=column+INITIAL_CAPACITY;
  276. }
  277. cells=new HSSFCell[newSize];
  278. System.arraycopy(oldCells,0,cells,0,oldCells.length);
  279. }
  280. cells[column]=cell;
  281. // fix up firstCol and lastCol indexes
  282. if (row.isEmpty() || column < row.getFirstCol()) {
  283. row.setFirstCol((short)column);
  284. }
  285. if (row.isEmpty() || column >= row.getLastCol()) {
  286. row.setLastCol((short) (column+1)); // +1 -> for one past the last index
  287. }
  288. }
  289. /**
  290. * Get the hssfcell representing a given column (logical cell)
  291. * 0-based. If you ask for a cell that is not defined, then
  292. * you get a null.
  293. * This is the basic call, with no policies applied
  294. *
  295. * @param cellIndex 0 based column number
  296. * @return HSSFCell representing that column or null if undefined.
  297. */
  298. private HSSFCell retrieveCell(int cellIndex) {
  299. if(cellIndex<0||cellIndex>=cells.length) {
  300. return null;
  301. }
  302. return cells[cellIndex];
  303. }
  304. /**
  305. * Get the hssfcell representing a given column (logical cell)
  306. * 0-based. If you ask for a cell that is not defined then
  307. * you get a null, unless you have set a different
  308. * {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} on the base workbook.
  309. *
  310. * @param cellnum 0 based column number
  311. * @return HSSFCell representing that column or null if undefined.
  312. */
  313. @Override
  314. public HSSFCell getCell(int cellnum) {
  315. return getCell(cellnum, book.getMissingCellPolicy());
  316. }
  317. /**
  318. * Get the hssfcell representing a given column (logical cell)
  319. * 0-based. If you ask for a cell that is not defined, then
  320. * your supplied policy says what to do
  321. *
  322. * @param cellnum 0 based column number
  323. * @param policy Policy on blank / missing cells
  324. * @return representing that column or null if undefined + policy allows.
  325. */
  326. @Override
  327. public HSSFCell getCell(int cellnum, MissingCellPolicy policy) {
  328. HSSFCell cell = retrieveCell(cellnum);
  329. switch (policy) {
  330. case RETURN_NULL_AND_BLANK:
  331. return cell;
  332. case RETURN_BLANK_AS_NULL:
  333. boolean isBlank = (cell != null && cell.getCellType() == CellType.BLANK);
  334. return (isBlank) ? null : cell;
  335. case CREATE_NULL_AS_BLANK:
  336. return (cell == null) ? createCell(cellnum, CellType.BLANK) : cell;
  337. default:
  338. throw new IllegalArgumentException("Illegal policy " + policy);
  339. }
  340. }
  341. /**
  342. * get the number of the first cell contained in this row.
  343. * @return short representing the first logical cell in the row, or -1 if the row does not contain any cells.
  344. */
  345. @Override
  346. public short getFirstCellNum() {
  347. if (row.isEmpty()) {
  348. return -1;
  349. }
  350. return (short) row.getFirstCol();
  351. }
  352. /**
  353. * Gets the index of the last cell contained in this row <b>PLUS ONE</b>. The result also
  354. * happens to be the 1-based column number of the last cell. This value can be used as a
  355. * standard upper bound when iterating over cells:
  356. * <pre>
  357. * short minColIx = row.getFirstCellNum();
  358. * short maxColIx = row.getLastCellNum();
  359. * for(short colIx=minColIx; colIx&lt;maxColIx; colIx++) {
  360. * HSSFCell cell = row.getCell(colIx);
  361. * if(cell == null) {
  362. * continue;
  363. * }
  364. * //... do something with cell
  365. * }
  366. * </pre>
  367. *
  368. * @return short representing the last logical cell in the row <b>PLUS ONE</b>, or -1 if the
  369. * row does not contain any cells.
  370. */
  371. @Override
  372. public short getLastCellNum() {
  373. if (row.isEmpty()) {
  374. return -1;
  375. }
  376. return (short) row.getLastCol();
  377. }
  378. /**
  379. * gets the number of defined cells (NOT number of cells in the actual row!).
  380. * That is to say if only columns 0,4,5 have values then there would be 3.
  381. * @return int representing the number of defined cells in the row.
  382. */
  383. @Override
  384. public int getPhysicalNumberOfCells()
  385. {
  386. int count = 0;
  387. for (HSSFCell cell : cells) {
  388. if (cell != null) {
  389. count++;
  390. }
  391. }
  392. return count;
  393. }
  394. /**
  395. * set the row's height or set to ff (-1) for undefined/default-height. Set the height in "twips" or
  396. * 1/20th of a point.
  397. * @param height rowheight or -1 for undefined (use sheet default)
  398. */
  399. @Override
  400. public void setHeight(short height)
  401. {
  402. if(height == -1){
  403. row.setHeight((short)(0xFF | 0x8000));
  404. row.setBadFontHeight(false);
  405. } else {
  406. row.setBadFontHeight(true);
  407. row.setHeight(height);
  408. }
  409. }
  410. /**
  411. * set whether or not to display this row with 0 height
  412. * @param zHeight height is zero or not.
  413. */
  414. @Override
  415. public void setZeroHeight(boolean zHeight) {
  416. row.setZeroHeight(zHeight);
  417. }
  418. /**
  419. * get whether or not to display this row with 0 height
  420. * @return - zHeight height is zero or not.
  421. */
  422. @Override
  423. public boolean getZeroHeight() {
  424. return row.getZeroHeight();
  425. }
  426. /**
  427. * set the row's height in points.
  428. * @param height row height in points, <code>-1</code> means to use the default height
  429. */
  430. @Override
  431. public void setHeightInPoints(float height)
  432. {
  433. if(height == -1){
  434. row.setHeight((short)(0xFF | 0x8000));
  435. row.setBadFontHeight(false);
  436. } else {
  437. row.setBadFontHeight(true);
  438. row.setHeight((short) (height * 20));
  439. }
  440. }
  441. /**
  442. * get the row's height or ff (-1) for undefined/default-height in twips (1/20th of a point)
  443. * @return rowheight or 0xff for undefined (use sheet default)
  444. */
  445. @Override
  446. public short getHeight()
  447. {
  448. short height = row.getHeight();
  449. //The low-order 15 bits contain the row height.
  450. //The 0x8000 bit indicates that the row is standard height (optional)
  451. if ((height & 0x8000) != 0) {
  452. height = sheet.getSheet().getDefaultRowHeight();
  453. } else {
  454. height &= 0x7FFF;
  455. }
  456. return height;
  457. }
  458. /**
  459. * get the row's height or ff (-1) for undefined/default-height in points (20*getHeight())
  460. * @return rowheight or 0xff for undefined (use sheet default)
  461. */
  462. @Override
  463. public float getHeightInPoints()
  464. {
  465. return ((float)getHeight() / 20);
  466. }
  467. /**
  468. * get the lowlevel RowRecord represented by this object - should only be called
  469. * by other parts of the high level API
  470. *
  471. * @return RowRecord this row represents
  472. */
  473. protected RowRecord getRowRecord()
  474. {
  475. return row;
  476. }
  477. /**
  478. * used internally to refresh the "last cell plus one" when the last cell is removed.
  479. * @return 0 when row contains no cells
  480. */
  481. private int calculateNewLastCellPlusOne(int lastcell) {
  482. int cellIx = lastcell - 1;
  483. HSSFCell r = retrieveCell(cellIx);
  484. while (r == null) {
  485. if (cellIx < 0) {
  486. return 0;
  487. }
  488. r = retrieveCell(--cellIx);
  489. }
  490. return cellIx+1;
  491. }
  492. /**
  493. * used internally to refresh the "first cell" when the first cell is removed.
  494. * @return 0 when row contains no cells (also when first cell is occupied)
  495. */
  496. private int calculateNewFirstCell(int firstcell) {
  497. int cellIx = firstcell + 1;
  498. HSSFCell r = retrieveCell(cellIx);
  499. while (r == null) {
  500. if (cellIx <= cells.length) {
  501. return 0;
  502. }
  503. r = retrieveCell(++cellIx);
  504. }
  505. return cellIx;
  506. }
  507. /**
  508. * Is this row formatted? Most aren't, but some rows
  509. * do have whole-row styles. For those that do, you
  510. * can get the formatting from {@link #getRowStyle()}
  511. */
  512. @Override
  513. public boolean isFormatted() {
  514. return row.getFormatted();
  515. }
  516. /**
  517. * Returns the whole-row cell styles. Most rows won't
  518. * have one of these, so will return null. Call
  519. * {@link #isFormatted()} to check first.
  520. */
  521. @Override
  522. public HSSFCellStyle getRowStyle() {
  523. if(!isFormatted()) { return null; }
  524. short styleIndex = row.getXFIndex();
  525. ExtendedFormatRecord xf = book.getWorkbook().getExFormatAt(styleIndex);
  526. return new HSSFCellStyle(styleIndex, xf, book);
  527. }
  528. /**
  529. * Applies a whole-row cell styling to the row.
  530. */
  531. public void setRowStyle(HSSFCellStyle style) {
  532. row.setFormatted(true);
  533. row.setXFIndex(style.getIndex());
  534. }
  535. /**
  536. * Applies a whole-row cell styling to the row.
  537. */
  538. @Override
  539. public void setRowStyle(CellStyle style) {
  540. setRowStyle((HSSFCellStyle)style);
  541. }
  542. /**
  543. * @return cell iterator of the physically defined cells.
  544. * Note that the 4th element might well not be cell 4, as the iterator
  545. * will not return un-defined (null) cells.
  546. * Call getCellNum() on the returned cells to know which cell they are.
  547. * As this only ever works on physically defined cells,
  548. * the {@link org.apache.poi.ss.usermodel.Row.MissingCellPolicy} has no effect.
  549. */
  550. @Override
  551. public Iterator<Cell> cellIterator()
  552. {
  553. return new CellIterator();
  554. }
  555. /**
  556. * Alias for {@link #cellIterator} to allow
  557. * foreach loops
  558. */
  559. @Override
  560. public Iterator<Cell> iterator() {
  561. return cellIterator();
  562. }
  563. /**
  564. * An iterator over the (physical) cells in the row.
  565. */
  566. private class CellIterator implements Iterator<Cell> {
  567. int thisId=-1;
  568. int nextId=-1;
  569. public CellIterator() {
  570. findNext();
  571. }
  572. @Override
  573. public boolean hasNext() {
  574. return nextId < cells.length;
  575. }
  576. @Override
  577. public Cell next() {
  578. if (!hasNext()) {
  579. throw new NoSuchElementException("At last element");
  580. }
  581. HSSFCell cell = cells[nextId];
  582. thisId = nextId;
  583. findNext();
  584. return cell;
  585. }
  586. @Override
  587. public void remove() {
  588. if (thisId == -1) {
  589. throw new IllegalStateException("remove() called before next()");
  590. }
  591. cells[thisId] = null;
  592. }
  593. private void findNext() {
  594. int i = nextId + 1;
  595. for (; i < cells.length; i++) {
  596. if (cells[i] != null) {
  597. break;
  598. }
  599. }
  600. nextId = i;
  601. }
  602. }
  603. /**
  604. * Compares two <code>HSSFRow</code> objects. Two rows are equal if they belong to the same worksheet and
  605. * their row indexes are equal.
  606. *
  607. * @param other the <code>HSSFRow</code> to be compared.
  608. * @return <ul>
  609. * <li>
  610. * the value <code>0</code> if the row number of this <code>HSSFRow</code> is
  611. * equal to the row number of the argument <code>HSSFRow</code>
  612. * </li>
  613. * <li>
  614. * a value less than <code>0</code> if the row number of this this <code>HSSFRow</code> is
  615. * numerically less than the row number of the argument <code>HSSFRow</code>
  616. * </li>
  617. * <li>
  618. * a value greater than <code>0</code> if the row number of this this <code>HSSFRow</code> is
  619. * numerically greater than the row number of the argument <code>HSSFRow</code>
  620. * </li>
  621. * </ul>
  622. * @throws IllegalArgumentException if the argument row belongs to a different worksheet
  623. */
  624. @Override
  625. public int compareTo(HSSFRow other) {
  626. if (this.getSheet() != other.getSheet()) {
  627. throw new IllegalArgumentException("The compared rows must belong to the same sheet");
  628. }
  629. int thisRow = this.getRowNum();
  630. int otherRow = other.getRowNum();
  631. return Integer.compare(thisRow, otherRow);
  632. }
  633. @Override
  634. public boolean equals(Object obj) {
  635. if (!(obj instanceof HSSFRow)) {
  636. return false;
  637. }
  638. HSSFRow other = (HSSFRow) obj;
  639. return (this.getRowNum() == other.getRowNum()) &&
  640. (this.getSheet() == other.getSheet());
  641. }
  642. @Override
  643. public int hashCode() {
  644. return row.hashCode();
  645. }
  646. /**
  647. * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the right.
  648. * @param firstShiftColumnIndex the column to start shifting
  649. * @param lastShiftColumnIndex the column to end shifting
  650. * @param step length of the shifting step
  651. */
  652. @Override
  653. public void shiftCellsRight(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) {
  654. RowShifter.validateShiftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step);
  655. if (lastShiftColumnIndex + step + 1 > cells.length) {
  656. extend(lastShiftColumnIndex + step + 1);
  657. }
  658. for (int columnIndex = lastShiftColumnIndex; columnIndex >= firstShiftColumnIndex; columnIndex--){ // process cells backwards, because of shifting
  659. HSSFCell cell = getCell(columnIndex);
  660. cells[columnIndex+step] = null;
  661. if (cell != null) {
  662. moveCell(cell, (short)(columnIndex+step));
  663. }
  664. }
  665. for (int columnIndex = firstShiftColumnIndex; columnIndex <= firstShiftColumnIndex+step-1; columnIndex++) {
  666. cells[columnIndex] = null;
  667. }
  668. }
  669. private void extend(int newLength) {
  670. HSSFCell[] temp = cells.clone();
  671. cells = new HSSFCell[newLength];
  672. System.arraycopy(temp, 0, cells, 0, temp.length);
  673. }
  674. /**
  675. * Shifts column range [firstShiftColumnIndex-lastShiftColumnIndex] step places to the left.
  676. * @param firstShiftColumnIndex the column to start shifting
  677. * @param lastShiftColumnIndex the column to end shifting
  678. * @param step length of the shifting step
  679. */
  680. @Override
  681. public void shiftCellsLeft(int firstShiftColumnIndex, int lastShiftColumnIndex, int step) {
  682. RowShifter.validateShiftLeftParameters(firstShiftColumnIndex, lastShiftColumnIndex, step);
  683. for (int columnIndex = firstShiftColumnIndex; columnIndex <= lastShiftColumnIndex; columnIndex++){
  684. HSSFCell cell = getCell(columnIndex);
  685. if(cell != null){
  686. cells[columnIndex-step] = null;
  687. moveCell(cell, (short)(columnIndex-step));
  688. } else {
  689. cells[columnIndex-step] = null;
  690. }
  691. }
  692. for (int columnIndex = lastShiftColumnIndex-step+1; columnIndex <= lastShiftColumnIndex; columnIndex++) {
  693. cells[columnIndex] = null;
  694. }
  695. }
  696. }