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

io.inugami.commons.data.AggregatorHelper Maven / Gradle / Ivy

There is a newer version: 3.3.5
Show newest version
/* --------------------------------------------------------------------
 *  Inugami
 * --------------------------------------------------------------------
 *
 * 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, version 3.
 *
 * 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 .
 */
package io.inugami.commons.data;

import io.inugami.api.exceptions.Asserts;
import io.inugami.commons.data.model.Record;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Aggregator
 *
 * @author patrick_guillerm
 * @since 8 nov. 2017
 */
public class AggregatorHelper {

    // =========================================================================
    // SUM OBJECT
    // =========================================================================
    public  R sumObject(final T ref, final T newValue, final Function extractor) {
        Asserts.assertNotNull("Extractor function is mandatory!", extractor);
        R result = ref == null ? null : extractor.apply(ref);

        if (newValue != null) {
            if (result == null) {
                result = extractor.apply(newValue);
            } else {
                final R newValueExtracted = extractor.apply(newValue);
                if (newValueExtracted != null) {
                    result = sumGenericType(result, newValueExtracted);
                }
            }
        }

        return result;
    }

    public  R sumGenericType(final R ref, final R newValue) {
        R                result     = null;
        final NumberType numberType = NumberType.getType(ref);
        Asserts.assertNotNull("Can't manage number type " + ref.getClass().getName(), numberType);

        switch (numberType) {
            case BYTE:
                result = (R) Byte.valueOf((byte) ((Byte) ref + (Byte) newValue));
                break;
            case SHORT:
                result = (R) Short.valueOf((short) ((Short) ref + (Short) newValue));
                break;
            case INTEGER:
                result = (R) Integer.valueOf((Integer) ref + (Integer) newValue);
                break;
            case LONG:
                result = (R) Long.valueOf((Long) ref + (Long) newValue);
                break;
            case FLOAT:
                result = (R) Float.valueOf((Float) ref + (Float) newValue);
                break;
            case DOUBLE:
                result = (R) Double.valueOf((Double) ref + (Double) newValue);
                break;
            case BIG_DECIMAL:
                result = (R) ((BigDecimal) ref).add((BigDecimal) newValue);
                break;
            default:
                break;
        }

        return result;
    }

    // =========================================================================
    // SUM MAP
    // =========================================================================
    public  Map sumMap(final Map ref, final Map newValue) {
        Asserts.assertNotNull("Reference map is mandatory!", ref);
        Asserts.assertNotNull("new map is mandatory!", newValue);

        final Map result    = new HashMap<>();
        final List   foundKeys = new ArrayList<>();

        for (final Map.Entry entry : ref.entrySet()) {
            final K key = entry.getKey();
            if (newValue.containsKey(key)) {
                result.put(key, sumGenericType(entry.getValue(), newValue.get(key)));
                foundKeys.add(key);
            } else {
                result.put(key, entry.getValue());
            }
        }

        for (final Map.Entry entry : newValue.entrySet()) {
            final K key = entry.getKey();
            if (!foundKeys.contains(key)) {
                if (ref.containsKey(key)) {
                    result.put(key, sumGenericType(entry.getValue(), newValue.get(key)));
                } else {
                    result.put(key, entry.getValue());
                }
            }
        }

        return result;
    }

    // =========================================================================
    // KEEP RECORD
    // =========================================================================
    public Record keepRecord(final double value, final long timestamp, final Record previousRecord, final String unit) {
        Asserts.assertNotNull("Value number is mandatory!", value);

        Record result = null;
        if (previousRecord == null) {
            result = new Record(timestamp, value, unit);
        } else {
            result = value > previousRecord.getValue() ? new Record(timestamp, value, unit) : previousRecord;
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy