org.cloudfoundry.identity.uaa.util.MapCollector Maven / Gradle / Ivy
The newest version!
package org.cloudfoundry.identity.uaa.util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class MapCollector implements Collector, HashMap> {
private final Function keyMapper;
private final Function valueMapper;
public MapCollector(Function keyMapper, Function valueMapper) {
this.keyMapper = keyMapper;
this.valueMapper = valueMapper;
}
@Override
public Supplier> supplier() {
return HashMap::new;
}
@Override
public BiConsumer, T> accumulator() {
return (m, item) -> m.put(keyMapper.apply(item), valueMapper.apply(item));
}
@Override
public BinaryOperator> combiner() {
return (left, right) -> { throw new IllegalStateException(String.format("Duplicate key %s", left)); };
}
@Override
public Function, HashMap> finisher() {
return m -> m;
}
@Override
public Set characteristics() {
return Collections.singleton(Characteristics.UNORDERED);
}
}