I have made a custom collector that uses a MessageDigest to create a hash. In general MessageDigest does not work in parallel. The issue I'm seeing is in the combiner()
method. It is not possible to combine two MessageDigest objects. When I return null it seems to work but if I throw an UnsupportedOperationException
it fails. What is the typical way to implement a collector that doesn't support parallel operations?
class ChecksumCollector implements Collector<String, MessageDigest, ByteBuffer> {
private String algorithm;
ChecksumCollector(final String algorithm) {
this.algorithm = algorithm;
}
@Override
public Supplier<MessageDigest> supplier() {
return () -> {
try {
return MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new UnsupportedOperationException("Could not find MessageDigest for algorithm " + algorithm, e);
}
};
}
@Override
public BiConsumer<MessageDigest, String> accumulator() {
return (md, s) -> md.update(s.getBytes(StandardCharsets.UTF_8));
}
@Override
public BinaryOperator<MessageDigest> combiner() {
return null; //seems to work but hash may not be correct?
//throw new UnsupportedOperationException(LineDuplicationHash.class.getSimpleName() + " does not support parallel streams");
}
@Override
public Function<MessageDigest, ByteBuffer> finisher() {
return md -> ByteBuffer.wrap(md.digest());
}
@Override
public Set<Characteristics> characteristics() {
return Set.of();
}
}