jdash.events.producer.ComparingPagesEventProducer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdash-events Show documentation
Show all versions of jdash-events Show documentation
Extension of the client module that allows to emit events when changes are detected between the results of
two subsequent requests.
The newest version!
package jdash.events.producer;
import jdash.client.GDClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuples;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static reactor.function.TupleUtils.function;
class ComparingPagesEventProducer implements GDEventProducer {
private final PagesComparator pagesComparator;
private Set previous0;
private Set previous1;
ComparingPagesEventProducer(PagesComparator pagesComparator) {
this.pagesComparator = pagesComparator;
}
private Set intersection(Set a, Set b) {
return a.stream()
.filter(aEl -> b.stream().anyMatch(bEl -> idEquals(aEl, bEl)))
.collect(Collectors.toUnmodifiableSet());
}
private Set union(Set a, Set b) {
return Set.copyOf(Stream.concat(a.stream(), b.stream())
.collect(Collectors.toUnmodifiableMap(pagesComparator::idGetter, Function.identity(), (o1, o2) -> o1))
.values());
}
private Set subtract(Set a, Set b) {
return a.stream()
.filter(aEl -> b.stream().noneMatch(bEl -> idEquals(aEl, bEl)))
.collect(Collectors.toUnmodifiableSet());
}
private boolean idEquals(T e1, T e2) {
return pagesComparator.idGetter(e1) == pagesComparator.idGetter(e2);
}
@Override
public Flux