https://bz.apache.org/bugzilla/show_bug.cgi?id=65916

--- Comment #11 from moimoi.chk <moimoi....@gmail.com> ---
When you do a remove operation with an iterator, it is not enough to simply
remove it from the list.
It must be done as in the following code.
The following modification is not a clean fix, but it works.


package org.apache.poi.xssf.usermodel;

public Iterator<Cell> cellIterator() {
        Iterator<Cell> originalIterator = (Iterator<Cell>)(Iterator<? extends
Cell>)_cells.values().iterator();

        return new Iterator<Cell>() {
            Cell lastNext = null;

            @Override
            public boolean hasNext() {
                return originalIterator.hasNext();
            }

            @Override
            public Cell next() {
                return lastNext = originalIterator.next();
            }

            @Override
            public void remove() {
                Cell cell = lastNext;

                if (cell.getRow() != XSSFRow.this) {
                    throw new IllegalArgumentException("Specified cell does not
belong to this row");
                }
                //noinspection SuspiciousMethodCalls
                if(!_cells.containsValue(cell)) {
                    throw new IllegalArgumentException("the row does not
contain this cell");
                }

                XSSFCell xcell = (XSSFCell)cell;
                if(xcell.isPartOfArrayFormulaGroup()) {
                    xcell.setCellFormula(null); // to remove the array formula
                }
                if(cell.getCellType() == CellType.FORMULA) {
                    _sheet.getWorkbook().onDeleteFormula(xcell);
                }
                // Performance optimization for bug 57840: explicit boxing is
slightly faster than auto-unboxing, though may use more memory
                final Integer colI = Integer.valueOf(cell.getColumnIndex()); //
NOSONAR
                XSSFCell removed = (XSSFCell) cell;

                // also remove the corresponding CTCell from the _row.cArray,
                // it may not be at the same position right now
                // thus search for it
                int i = 0;
                for (CTCell ctCell : _row.getCArray()) {
                    if(ctCell == removed.getCTCell()) {
                        _row.removeC(i);
                    }
                    i++;
                }

                originalIterator.remove();
            }
        };
    }

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@poi.apache.org
For additional commands, e-mail: dev-h...@poi.apache.org

Reply via email to