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

fr.ird.observe.dto.report.operations.GroupByLength 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.commons.lang3.mutable.MutableInt;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created on 21/09/2021.
 *
 * @author Tony Chemit - [email protected]
 * @since 5.0.44
 */
@AutoService(ReportOperationConsumer.class)
public class GroupByLength implements ReportOperationConsumer {

    @Override
    public DataMatrix consume(String parameters, ReportRequestExecutor requestExecutor, Report report, Set tripId, DataMatrix incoming) {
        // Première passe pour grouper par classe de taille
        Map data = new LinkedHashMap<>();
        for (int row = 0, nbRows = incoming.getHeight(); row < nbRows; row++) {
            String length = (String) incoming.getValue(0, row);
            Integer count = Integer.valueOf(incoming.getValue(1, row).toString());
            MutableInt mutableInt = data.computeIfAbsent(length, k -> new MutableInt());
            mutableInt.add(count);
        }
        // Deuxième passe pour remplir la matrice
        Set lengths = new HashSet<>();
        for (int row = 0; row < incoming.getHeight(); row++) {
            lengths.add((String) incoming.getValue(0, row));
        }
        DataMatrix result = createTmpMatrix(0, incoming.getHeight(), incoming.getWidth(), lengths.size());
        int row = 0;
        for (Map.Entry entry : data.entrySet()) {
            String length = entry.getKey();
            MutableInt mutableInt = entry.getValue();
            result.setValue(0, row, length);
            result.setValue(1, row, mutableInt.intValue());
            row++;
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy