net.yudichev.jiotty.common.lang.DeduplicatingConsumer Maven / Gradle / Ivy
package net.yudichev.jiotty.common.lang;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import static com.google.common.base.Preconditions.checkNotNull;
public final class DeduplicatingConsumer implements Consumer {
private final EqualityComparator equalityComparator;
private final Consumer delegate;
private final AtomicReference lastValue = new AtomicReference<>();
public DeduplicatingConsumer(EqualityComparator equalityComparator, Consumer delegate) {
this.equalityComparator = checkNotNull(equalityComparator);
this.delegate = checkNotNull(delegate);
}
@Override
public void accept(T t) {
T previousValue = lastValue.getAndSet(t);
if (previousValue == null || !equalityComparator.areEqual(previousValue, t)) {
delegate.accept(t);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy