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

org.sejda.impl.sambox.component.excel.DataTable Maven / Gradle / Ivy

/*
 * Copyright 2016 by Eduard Weissmann ([email protected]).
 *
 * This file is part of the Sejda source code
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package org.sejda.impl.sambox.component.excel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.TreeSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataTable {

    private static final Logger LOG = LoggerFactory.getLogger(DataTable.class);

    private final List> data = new ArrayList<>();
    private final TreeSet pageNumbers = new TreeSet<>();

    public DataTable(int pageNumber) {
        this.pageNumbers.add(pageNumber);
    }

    public DataTable(Collection pageNumbers) {
        this.pageNumbers.addAll(pageNumbers);
    }

    public void addRow(List dataRow) {
        data.add(dataRow);
    }

    public void addRows(List> dataRows) {
        dataRows.forEach(this::addRow);
    }

    public List headerRow() {
        return data.get(0);
    }

    public boolean hasSameHeaderAs(DataTable other) {
        String thisHeader = String.join("", this.headerRow()).trim();
        String otherHeader = String.join("", other.headerRow()).trim();
        LOG.debug("Comparing header columns: '{}' and '{}'", thisHeader, otherHeader);

        return thisHeader.equalsIgnoreCase(otherHeader);
    }

    public boolean hasSameColumnsAs(DataTable other) {
        LOG.debug("Comparing header columns size: {} and {}", this.headerRow().size(), other.headerRow().size());
        return other.headerRow().size() == this.headerRow().size();
    }

    public List> getData() {
        return data;
    }

    public TreeSet getPageNumbers() {
        return pageNumbers;
    }

    public DataTable mergeWith(DataTable other) {
        TreeSet resultPageNumbers = new TreeSet<>();
        resultPageNumbers.addAll(this.pageNumbers);
        resultPageNumbers.addAll(other.pageNumbers);

        DataTable result = new DataTable(resultPageNumbers);
        result.addRows(this.data);

        List> otherData = other.data;
        if(this.hasSameHeaderAs(other)) {
            otherData.remove(0);
        }

        result.addRows(otherData);

        return result;
    }

    public String getPagesAsString() {
        StringBuilder sb = new StringBuilder();
        if(this.pageNumbers.size() > 1) {
            sb.append("Pages ");
            int i = 0;
            for(Integer pageNumber: pageNumbers) {
                if(i != 0) {
                    sb.append(", ");
                }
                sb.append(pageNumber);
                i++;
            }
        } else {
            sb.append("Page ").append(pageNumbers.iterator().next());
        }

        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy