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

fr.ird.observe.dto.report.operations.SumRow Maven / Gradle / Ivy

package fr.ird.observe.dto.report.operations;

/*-
 * #%L
 * ObServe Toolkit :: Dto
 * %%
 * Copyright (C) 2017 - 2021 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.auto.service.AutoService;
import fr.ird.observe.dto.report.DataMatrix;
import fr.ird.observe.dto.report.Report;
import fr.ird.observe.dto.report.ReportOperationConsumer;
import fr.ird.observe.dto.report.ReportRequestExecutor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;
import java.util.Set;

/**
 * Created on 21/09/2021.
 *
 * @author Tony Chemit - [email protected]
 * @since 5.0.44
 */
@AutoService(ReportOperationConsumer.class)
public class SumRow implements ReportOperationConsumer {
    private static final Logger log = LogManager.getLogger(SumRow.class);

    @Override
    public DataMatrix consume(String parameters, ReportRequestExecutor requestExecutor, Report report, Set tripId, DataMatrix incoming) {
        DataMatrix tmpMatrix = createTmpMatrix(incoming.getWidth(), 0, 1, incoming.getHeight());
        for (int row = 0, nbRows = incoming.getHeight(); row < nbRows; row++) {
            Object sumRow = getSumRow(row, incoming);
            tmpMatrix.setValue(0, row, sumRow);
        }
        return DataMatrix.merge(incoming, tmpMatrix);
    }

    protected Object getSumRow(int row, DataMatrix incoming) {
        double result = 0d;
        int nbColumns = incoming.getWidth();
        for (int col = 0; col < nbColumns; col++) {
            Serializable o = incoming.getValue(col, row);
            if (o == null || "null".equals(o)) {
                o = 0;
            }
            double d;
            try {
                d = Double.parseDouble(o.toString());
            } catch (NumberFormatException e) {
                // une des données de la colonne n'est pas un count on sort directement
                log.debug(String.format("Could not convert %s to number", o), e);
                return "-";
            }
            result += d;
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy