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

com.yelstream.topp.furnace.reactive.integration.SubscriberToIterable Maven / Gradle / Ivy

Go to download

Topp Furnace Reactive Integration provides interactions between selected reactive frameworks.

The newest version!
package com.yelstream.topp.furnace.reactive.integration;

import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Flow.Subscriber;
import java.util.concurrent.Flow.Subscription;

public class SubscriberToIterable implements Iterable, Subscriber {

    private final BlockingQueue queue = new ArrayBlockingQueue<>(100);
    private volatile boolean completed = false;
    private volatile Throwable error = null;

    @Override
    public void onSubscribe(Subscription subscription) {
        subscription.request(Long.MAX_VALUE); // Request all items
    }

    @Override
    public void onNext(T item) {
        try {
            queue.put(item); // Block if the queue is full
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    @Override
    public void onError(Throwable throwable) {
        error = throwable;
        completed = true;
    }

    @Override
    public void onComplete() {
        completed = true;
    }

    @Override
    public Iterator iterator() {
        return new Iterator() {
            @Override
            public boolean hasNext() {
                while (!completed || !queue.isEmpty()) {
                    if (!queue.isEmpty()) {
                        return true;
                    }
                    // Busy-wait until there is an item or the stream is completed
                    Thread.onSpinWait();
                }
                return false;
            }

            @Override
            public T next() {
                try {
                    while (true) {
                        T item = queue.poll();
                        if (item != null) {
                            return item;
                        }
                        if (completed && queue.isEmpty()) {
                            throw new java.util.NoSuchElementException();
                        }
                        // Busy-wait until there is an item
                        Thread.onSpinWait();
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy