001/************************************************************************ 002 * Licensed under Public Domain (CC0) * 003 * * 004 * To the extent possible under law, the person who associated CC0 with * 005 * this code has waived all copyright and related or neighboring * 006 * rights to this code. * 007 * * 008 * You should have received a copy of the CC0 legalcode along with this * 009 * work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.* 010 ************************************************************************/ 011 012package org.reactivestreams.tck.flow.support; 013 014import org.reactivestreams.example.unicast.AsyncIterablePublisher; 015 016import java.util.Iterator; 017import java.util.concurrent.Executor; 018 019public class InfiniteHelperPublisher<T> extends AsyncIterablePublisher<T> { 020 021 public InfiniteHelperPublisher(final Function<Integer, T> create, final Executor executor) { 022 super(new Iterable<T>() { 023 @Override public Iterator<T> iterator() { 024 return new Iterator<T>() { 025 private int at = 0; 026 027 @Override public boolean hasNext() { return true; } 028 @Override public T next() { 029 try { 030 return create.apply(at++); // Wraps around on overflow 031 } catch (Throwable t) { 032 throw new IllegalStateException( 033 String.format("Failed to create element in %s for id %s!", getClass().getSimpleName(), at - 1), t); 034 } 035 } 036 @Override public void remove() { throw new UnsupportedOperationException(); } 037 }; 038 } 039 }, executor); 040 } 041}