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

org.ttzero.excel.reader.RowBlock Maven / Gradle / Ivy

/*
 * Copyright (c) 2019-2020, [email protected] All Rights Reserved.
 *
 * 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 org.ttzero.excel.reader;

import org.ttzero.excel.entity.e3.DBCell;
import org.ttzero.excel.entity.style.Styles;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static org.ttzero.excel.manager.Const.ROW_BLOCK_SIZE;

/**
 * All cells in the Cell Table are divided into blocks
 * of 32 consecutive rows, called Row Blocks.
 * The first Row Block starts with the first used row in that sheet.
 * Inside each Row Block there will occur ROW records describing
 * the properties of the rows, and cell records with all
 * the cell contents in this Row Block
 *
 * @author guanquan.wang at 2019-03-06 18:43
 */
public class RowBlock implements Iterator {
    BIFF8Row[] rows;
    public DBCell dbCell;
    public int nm;

    private int i;
    AllInOneCell bCell;
    // index
    private final Map index;

    /**
     * Shared 32 rows
     */
    private RowBlock() {
        this.rows = new BIFF8Row[ROW_BLOCK_SIZE];
        this.dbCell = new DBCell();

        for (int i = 0; i < ROW_BLOCK_SIZE; i++) {
            rows[i] = new BIFF8Row();
        }
        dbCell.firstCellOffsets = new short[ROW_BLOCK_SIZE];
        bCell = new AllInOneCell();
        index = new HashMap<>(ROW_BLOCK_SIZE << 1);
    }

    public static RowBlock share() {
        RowBlock rb = RowBlockHandler.handler;
        rb.nm = rb.i = 0;
        return rb;
    }

    private static class RowBlockHandler {
        private static final RowBlock handler = new RowBlock();
    }

    void resetIndex() {
        index.clear();
        for (int j = 0; j < nm; j++) {
            BIFF8Row row = rows[j];
            index.put(row.index, row);
        }
    }

    BIFF8Row getByRow(int row) {
        return index.get(row);
    }

    // -- PUBLIC-
    public void setGlobalEntry(SharedStrings sst, Styles styles) {
        for (int i = 0; i < ROW_BLOCK_SIZE; i++) {
            rows[i].sst = sst;
            rows[i].styles = styles;
        }
    }

    public void setHeaderRow(HeaderRow header) {
        for (int i = 0; i < ROW_BLOCK_SIZE; i++) {
            rows[i].setHr(header);
        }
    }

    @Override
    public BIFF8Row next() {
        return rows[i++];
    }

    @Override
    public boolean hasNext() {
        return i < nm;
    }

    public BIFF8Row firstRow() {
        return rows[0];
    }

    public BIFF8Row lastRow() {
        return rows[nm - 1];
    }

    public int size() {
        return nm;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < nm; i++) {
            buf.append(rows[i]).append("\n");
        }
        return buf.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy