Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asynchronous Stream Publisher - Example #377

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/************************************************************************
* Licensed under Public Domain (CC0) *
* *
* To the extent possible under law, the person who associated CC0 with *
* this code has waived all copyright and related or neighboring *
* rights to this code. *
* *
* You should have received a copy of the CC0 legalcode along with this *
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.*
************************************************************************/

package org.reactivestreams.example.unicast;

import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.Executor;

/**
* SupplierPublisher uses a supplier with only one 'get()' method for supplying values
* from unknown streams like files or sockets.
* The supplier informs about the end of stream by returning a 'null' value.
*/
public class SupplierPublisher<T> extends AsyncIterablePublisher<T> {
public SupplierPublisher(final Supplier<T> supplier, final Executor executor) {
super(new Iterable<T>() {
@Override public Iterator<T> iterator() {
return new Iterator<T>() {
private T elem = supplier.get();
@Override public boolean hasNext() { return elem != null; }
@Override public T next() {
if (!hasNext()) return Collections.<T>emptyList().iterator().next();
else {
T prev = elem;
elem = supplier.get();
return prev;
}
}
@Override public void remove() { throw new UnsupportedOperationException(); }
};
}
}, executor);
}

static interface Supplier<T> {
T get();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/************************************************************************
* Licensed under Public Domain (CC0) *
* *
* To the extent possible under law, the person who associated CC0 with *
* this code has waived all copyright and related or neighboring *
* rights to this code. *
* *
* You should have received a copy of the CC0 legalcode along with this *
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.*
************************************************************************/

package org.reactivestreams.example.unicast;

import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.reactivestreams.Publisher;
import org.reactivestreams.tck.PublisherVerification;
import org.reactivestreams.tck.TestEnvironment;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@Test // Must be here for TestNG to find and run this, do not remove
public class SupplierPublisherTest extends PublisherVerification<Integer> {

private ExecutorService e;
@BeforeClass void before() { e = Executors.newFixedThreadPool(4); }
@AfterClass void after() { if (e != null) e.shutdown(); }

public SupplierPublisherTest() {
super(new TestEnvironment());
}

@SuppressWarnings("unchecked")
@Override public Publisher<Integer> createPublisher(final long elements) {
assert(elements <= maxElementsFromPublisher());
return new SupplierPublisher<Integer>(new SupplierPublisher.Supplier<Integer>() {
private int at;
@Override
public Integer get() {
return at < elements ? at++ : null;
}
}, e);
}

@Override public Publisher<Integer> createFailedPublisher() {
return new AsyncIterablePublisher<Integer>(new Iterable<Integer>() {
@Override public Iterator<Integer> iterator() {
throw new RuntimeException("Error state signal!");
}
}, e);
}

@Override public long maxElementsFromPublisher() {
return Integer.MAX_VALUE;
}
}