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