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

org.aksw.jenax.arq.aggregation.AccMap2 Maven / Gradle / Ivy

The newest version!
package org.aksw.jenax.arq.aggregation;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.aksw.commons.collector.domain.Accumulator;
import org.aksw.commons.collector.domain.Aggregator;


/**
 *
 *
 * @author raven
 *
 * @param 
 * @param 
 * @param 
 * @param 
 */
public class AccMap2>
    implements Accumulator>
{
    protected BiFunction mapper;
    protected C subAgg;

    protected Map> state = new HashMap<>();

    public AccMap2(Function mapper, C subAgg) {
        this((binding, rowNum) -> mapper.apply(binding), subAgg);
    }

    public AccMap2(BiFunction mapper, C subAgg) {
        this.mapper = mapper;
        this.subAgg = subAgg;
    }

    @Override
    public void accumulate(B binding, E env) {
        // TODO Keep track of the relative binding index
        K k = mapper.apply(binding, -1l);
        Accumulator subAcc = state.get(k);
        if(subAcc == null) {
            subAcc = subAgg.createAccumulator();
            state.put(k, subAcc);
        }
        subAcc.accumulate(binding, env);
    }

    @Override
    public Map getValue() {
        Map result = new HashMap();

        for(Entry> entry : state.entrySet()) {
            K k = entry.getKey();
            V v = entry.getValue().getValue();

            result.put(k, v);
        }

        return result;
    }

    public static > AccMap2 create(Function mapper, C subAgg) {
        BiFunction fn = (binding, rowNum) -> mapper.apply(binding);
        AccMap2 result = new AccMap2<>(fn, subAgg);
        return result;
    }

    public static > AccMap2 create(BiFunction mapper, C subAgg) {
        AccMap2 result = new AccMap2<>(mapper, subAgg);
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy