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

com.github.olaaronsson.process.histogram.LifeCycleHistogramCollector Maven / Gradle / Ivy

The newest version!
package com.github.olaaronsson.process.histogram;

import static java.util.stream.Collector.Characteristics.CONCURRENT;
import static java.util.stream.Collector.Characteristics.UNORDERED;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collector;

public class LifeCycleHistogramCollector implements Collector, Map> {

	private final LifeCycleProvider lifeCycle;

	public LifeCycleHistogramCollector(LifeCycleProvider lifeCycle) {
		this.lifeCycle = lifeCycle;
	}

	public Enum[] getCycle() {
		return lifeCycle.getCycle();
	}

	@Override
	public java.util.function.Supplier> supplier() {
		Map map = new LinkedHashMap<>();
		Arrays.stream(lifeCycle.getCycle()).forEach(s -> map.put(s.name(), 0));
		return () -> map;
	}

	@Override
	public BiConsumer, LifeCycleProvider> accumulator() {
		return (Map acc, LifeCycleProvider candidate) -> acc.put(candidate.getCurrentStatus().name(), acc.get(candidate.getCurrentStatus().name()) + 1);
	}

	@Override
	public BinaryOperator> combiner() {
		return (Map result1, Map result2) -> {
			Map map = new LinkedHashMap<>();
			Arrays.stream(lifeCycle.getCycle()).forEach(s -> map.put(s.name(), (result1.get(s.name()) + result2.get(s.name()))));
			return map;
		};
	}

	@Override
	public Function, Map> finisher() {
		return Function.identity();
	}

	@Override
	public Set characteristics() {
		return EnumSet.of(CONCURRENT, UNORDERED);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy