
org.omnifaces.utils.stream.CombinedCollector Maven / Gradle / Ivy
/*
* Copyright 2021 OmniFaces
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.omnifaces.utils.stream;
import java.util.AbstractMap;
import java.util.Collections;
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.function.Supplier;
import java.util.stream.Collector;
// TODO find proper pair option
class CombinedCollector implements Collector, Map.Entry> {
private final Collector collector1;
private final Collector collector2;
CombinedCollector(Collector collector1, Collector collector2) {
this.collector1 = collector1;
this.collector2 = collector2;
}
@Override
public Supplier> supplier() {
return () -> new AbstractMap.SimpleEntry<>(collector1.supplier().get(), collector2.supplier().get());
}
@Override
public BiConsumer, T> accumulator() {
BiConsumer accumulator1 = collector1.accumulator();
BiConsumer accumulator2 = collector2.accumulator();
return (pair, element) -> {
accumulator1.accept(pair.getKey(), element);
accumulator2.accept(pair.getValue(), element);
};
}
@Override
public BinaryOperator> combiner() {
BinaryOperator combiner1 = collector1.combiner();
BinaryOperator combiner2 = collector2.combiner();
return (pair1, pair2) -> {
I1 i1 = combiner1.apply(pair1.getKey(), pair2.getKey());
I2 i2 = combiner2.apply(pair1.getValue(), pair2.getValue());
return new AbstractMap.SimpleEntry<>(i1, i2);
};
}
@Override
public Function, Map.Entry> finisher() {
Function finisher1 = collector1.finisher();
Function finisher2 = collector2.finisher();
return (pair) -> {
R1 r1 = finisher1.apply(pair.getKey());
R2 r2 = finisher2.apply(pair.getValue());
return new AbstractMap.SimpleEntry<>(r1, r2);
};
}
@Override
public Set characteristics() {
// TODO correctly determine elements
return Collections.emptySet();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy