
no.digipost.collection.AdaptableCollector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of digg Show documentation
Show all versions of digg Show documentation
Some stellar general purpose utils.
/**
* Copyright (C) Posten Norge AS
*
* 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
*
* http://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 no.digipost.collection;
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;
import java.util.stream.Stream;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collector.Characteristics.IDENTITY_FINISH;
import static java.util.stream.Collectors.toSet;
/**
* A {@link Collector} which offers some means for adapting it into new Collectors,
* in particular the ability to {@link #andThen(Function) map the result}.
*/
public final class AdaptableCollector implements Collector {
public static AdaptableCollector of(Supplier supplier,
BiConsumer accumulator,
BinaryOperator combiner,
Function finisher,
Set characteristics) {
return new AdaptableCollector<>(supplier, accumulator, combiner, finisher, characteristics.stream());
}
public static AdaptableCollector of(Supplier supplier,
BiConsumer accumulator,
BinaryOperator combiner,
Function finisher,
Characteristics ... characteristics) {
return new AdaptableCollector<>(supplier, accumulator, combiner, finisher, Stream.of(characteristics));
}
private final Supplier supplier;
private final BiConsumer accumulator;
private final BinaryOperator combiner;
private final Function finisher;
private final Set characteristics;
private AdaptableCollector(Supplier supplier,
BiConsumer accumulator,
BinaryOperator combiner,
Function finisher,
Stream characteristics) {
this.supplier = supplier;
this.accumulator = accumulator;
this.combiner = combiner;
this.finisher = finisher;
this.characteristics = unmodifiableSet(characteristics.collect(toSet()));
}
/**
* Create a new {@code Collector} which maps the resulting container to something else
* using the given {@code resultMapper} {@link Function}.
*
* @param The new result type after applying the mapper functions.
* @param resultMapper the function used to map the collector's result
*
* @return a new Collector
*/
public AdaptableCollector andThen(Function super R, M> resultMapper) {
return new AdaptableCollector<>(supplier, accumulator, combiner, finisher.andThen(resultMapper), characteristics.stream().filter(c -> c != IDENTITY_FINISH));
}
@Override
public Supplier supplier() {
return supplier;
}
@Override
public BiConsumer accumulator() {
return accumulator;
}
@Override
public BinaryOperator combiner() {
return combiner;
}
@Override
public Function finisher() {
return finisher;
}
@Override
public Set characteristics() {
return characteristics;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy