001package org.reactivestreams.tck.support; 002 003import java.util.Collections; 004import java.util.Iterator; 005import java.util.concurrent.Executor; 006import org.reactivestreams.Subscription; 007import org.reactivestreams.Subscriber; 008import org.reactivestreams.Publisher; 009import org.reactivestreams.example.unicast.AsyncIterablePublisher; 010 011public class HelperPublisher<T> extends AsyncIterablePublisher<T> { 012 013 public HelperPublisher(final int from, final int to, final Function<Integer, T> create, final Executor executor) { 014 super(new Iterable<T>() { 015 { if(from > to) throw new IllegalArgumentException("from must be equal or greater than to!"); } 016 @Override public Iterator<T> iterator() { 017 return new Iterator<T>() { 018 private int at = from; 019 @Override public boolean hasNext() { return at < to; } 020 @Override public T next() { 021 if (!hasNext()) return Collections.<T>emptyList().iterator().next(); 022 else try { 023 return create.apply(at++); 024 } catch (Throwable t) { 025 throw new IllegalStateException(String.format("Failed to create element for id %d!", at - 1), t); 026 } 027 } 028 @Override public void remove() { throw new UnsupportedOperationException(); } 029 }; 030 } 031 }, executor); 032 } 033}