All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dua3.meja.model.generic.GenericRow Maven / Gradle / Ivy

There is a newer version: 6.2.1
Show newest version
/*
 * Copyright 2015 Axel Howind ([email protected]).
 *
 * 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.generic;

import java.util.ArrayList;
import java.util.Iterator;

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

/**
 *
 * @author Axel Howind ([email protected])
 */
public class GenericRow extends AbstractRow {

    private final ArrayList cells;

    /**
     * Construct a new {@code GenericRow}.
     *
     * @param sheet     the sheet the row belongs to
     * @param rowNumber the row number
     */
    public GenericRow(GenericSheet sheet, int rowNumber) {
        super(sheet, rowNumber);
        this.cells = new ArrayList<>(sheet.getColumnCount());
    }

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

    @Override
    public GenericCell getCell(int col) {
        reserve(col);
        return cells.get(col);
    }

    @Override
    public GenericCell getCellIfExists(int col) {
        return col < cells.size() ? cells.get(col) : null;
    }

    @Override
    public int getFirstCellNum() {
        return 0;
    }

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

    @Override
    public Iterator iterator() {
        return new IteratorAdapter<>(cells.iterator());
    }

    private void reserve(int col) {
        if (col >= cells.size()) {
            GenericCellStyle cellStyle = getSheet().getWorkbook().getDefaultCellStyle();
            cells.ensureCapacity(col + 1);
            for (int colNum = cells.size(); colNum <= col; colNum++) {
                cells.add(new GenericCell(this, colNum, cellStyle));
            }
            getSheet().reserveColumn(col);
        }
    }

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy