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

com.aol.cyclops.streams.HotStreamImpl Maven / Gradle / Ivy

There is a newer version: 7.3.1
Show newest version
package com.aol.cyclops.streams;

import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.function.Function;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import uk.co.real_logic.agrona.concurrent.OneToOneConcurrentArrayQueue;

import com.aol.cyclops.sequence.HotStream;
import com.aol.cyclops.sequence.SequenceM;
import com.aol.cyclops.streams.spliterators.ClosingSpliterator;

public class HotStreamImpl implements HotStream{

	private final Stream stream;
	private final AtomicReferenceArray> connections = new AtomicReferenceArray<>(10);
	private final AtomicBoolean open =new AtomicBoolean(true);
	private volatile int connected=0;
	
	public HotStreamImpl(Stream stream){
		this.stream = stream;	
	}
	
	public HotStream init(Executor exec){
		CompletableFuture.runAsync( ()-> {
			
			stream.forEach(a->{
					int local = connected;
					
					for(int i=0;i connect(){
		return connect(new OneToOneConcurrentArrayQueue(256));
	}
	
	@Override
	public SequenceM connect(Queue queue) {
		connections.getAndSet(connected, queue);
		connected++;
		return StreamUtils.sequenceM(StreamSupport.stream(
                new ClosingSpliterator(Long.MAX_VALUE, queue,open), false),Optional.empty());
	}

	@Override
	public > R connectTo(Queue queue,Function,R> to) {
		return to.apply(connect(queue));
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy