com.github.thorbenkuck.network.stream.DistributingSource Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easy-net Show documentation
Show all versions of easy-net Show documentation
An EventStream driven, simple to use Client/Server framework
The newest version!
package com.github.thorbenkuck.network.stream;
import java.util.ArrayList;
import java.util.List;
public class DistributingSource implements Source {
private final List> consumers = new ArrayList<>();
private List> copy() {
List> copy;
synchronized (consumers) {
copy = new ArrayList<>(consumers);
}
return copy;
}
@Override
public void push(T t) {
List> copy = copy();
copy.forEach(tSourceConsumer -> tSourceConsumer.accept(t));
}
@Override
public void onEmit(SourceConsumer consumer) {
consumers.add(consumer);
}
@Override
public void close() {
List> copy = copy();
copy.forEach(SourceConsumer::onCancel);
consumers.clear();
}
}