/*
 * Copyright 2015 Axel Howind (axel@dua3.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.dua3.meja.model.poi;

import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;

import com.dua3.meja.model.AbstractRow;
import com.dua3.meja.model.Cell;
import com.dua3.meja.model.Row;

/**
 *
 * @author axel
 */
public final class PoiRow extends AbstractRow {

    /**
     *
     */
    final org.apache.poi.ss.usermodel.Row poiRow;

    /**
     * Construct row from existing POI row instance.
     *
     * @param sheet the sheet
     * @param row   the row
     */
    public PoiRow(PoiSheet sheet, org.apache.poi.ss.usermodel.Row row) {
        super(sheet, row.getRowNum());
        this.poiRow = row;
    }

    @Override
    public void copy(Row other) {
        for (Cell cell : other) {
            getCell(cell.getColumnNumber()).copy(cell);
        }
    }

    @Override
    public boolean equals(Object obj) {
        return obj != null && getClass() == obj.getClass() && poiRow.equals(((PoiRow) obj).poiRow);
    }

    @Override
    public PoiCell getCell(int col) {
        org.apache.poi.ss.usermodel.Cell poiCell = poiRow.getCell(col, MissingCellPolicy.CREATE_NULL_AS_BLANK);
        return new PoiCell(this, poiCell);
    }

    @Override
    public PoiCell getCellIfExists(int col) {
        org.apache.poi.ss.usermodel.Cell poiCell = poiRow.getCell(col);
        return poiCell != null ? new PoiCell(this, poiCell) : null;
    }

    @Override
    public int getFirstCellNum() {
        return poiRow.getFirstCellNum();
    }

    @Override
    public int getLastCellNum() {
        return poiRow.getLastCellNum() - 1;
    }

    @Override
    public int hashCode() {
        return poiRow.hashCode();
    }

    /**
     * Update first and last column numbers.
     *
     * @param columnNumber
     *  the column number
     */
    void setColumnUsed(int columnNumber) {
        getSheet().setColumnUsed(columnNumber);
    }

    @Override
    public PoiSheet getSheet() {
        return (PoiSheet) (super.getSheet());
    }

    @Override
    public PoiWorkbook getWorkbook() {
        return getSheet().getWorkbook();
    }
}

@@ -2016,38 +2016,91 @@
 Num() %7B%0A        
-return
+// POI returns -1 for empty rows%0A        return Math.max(0,
  poiRow.getFirst
@@ -2108,16 +2108,17 @@
 ellNum()
+)
 ;%0A    %7D%0A
@@ -2166,38 +2166,91 @@
 Num() %7B%0A        
-return
+// POI returns -1 for empty rows%0A        return Math.max(0,
  poiRow.getLastC
@@ -2261,11 +2261,10 @@
 um()
- - 
+)-
 1;%0A 

fix getLastCellNum() returning -2 for empty rows, getFirstCellNum() -1