org.infinispan.functional.impl.FunctionalNotifierImpl Maven / Gradle / Ivy
package org.infinispan.functional.impl;
import org.infinispan.commons.api.functional.EntryView.ReadEntryView;
import org.infinispan.commons.util.Experimental;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* @since 8.0
*/
@Experimental
public final class FunctionalNotifierImpl implements FunctionalNotifier {
final List>> onCreates = new CopyOnWriteArrayList<>();
final List, ReadEntryView>> onModifies = new CopyOnWriteArrayList<>();
final List>> onRemoves = new CopyOnWriteArrayList<>();
final List>> onWrites = new CopyOnWriteArrayList<>();
final List> rwListeners = new CopyOnWriteArrayList<>();
final List> writeListeners = new CopyOnWriteArrayList<>();
@Override
public AutoCloseable add(WriteListener l) {
writeListeners.add(l);
return new ListenerCloseable<>(l, writeListeners);
}
@Override
public AutoCloseable add(ReadWriteListener l) {
rwListeners.add(l);
return new ListenerCloseable<>(l, rwListeners);
}
@Override
public AutoCloseable onCreate(Consumer> f) {
onCreates.add(f);
return new ListenerCloseable<>(f, onCreates);
}
@Override
public AutoCloseable onModify(BiConsumer, ReadEntryView> f) {
onModifies.add(f);
return new ListenerCloseable<>(f, onModifies);
}
@Override
public AutoCloseable onRemove(Consumer> f) {
onRemoves.add(f);
return new ListenerCloseable<>(f, onRemoves);
}
@Override
public AutoCloseable onWrite(Consumer> f) {
onWrites.add(f);
return new ListenerCloseable<>(f, onWrites);
}
@Override
public void notifyOnCreate(ReadEntryView created) {
onCreates.forEach(c -> c.accept(created));
rwListeners.forEach(rwl -> rwl.onCreate(created));
}
@Override
public void notifyOnModify(ReadEntryView before, ReadEntryView after) {
onModifies.forEach(c -> c.accept(before, after));
rwListeners.forEach(rwl -> rwl.onModify(before, after));
}
@Override
public void notifyOnRemove(ReadEntryView removed) {
onRemoves.forEach(c -> c.accept(removed));
rwListeners.forEach(rwl -> rwl.onRemove(removed));
}
@Override
public void notifyOnWrite(Supplier> write) {
if (!onWrites.isEmpty()) onWrites.forEach(c -> c.accept(write.get()));
if (!writeListeners.isEmpty()) writeListeners.forEach(wl -> wl.onWrite(write.get()));
}
private static final class ListenerCloseable implements AutoCloseable {
final T f;
final List list;
private ListenerCloseable(T f, List list) {
this.f = f;
this.list = list;
}
@Override
public void close() throws Exception {
list.remove(f);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy