All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.thorbenkuck.network.stream.DistributingSource Maven / Gradle / Ivy

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();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy