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.example.unicast;
013
014import java.util.Collections;
015import java.util.Iterator;
016import java.util.concurrent.Executor;
017import org.reactivestreams.Subscription;
018import org.reactivestreams.Subscriber;
019import org.reactivestreams.Publisher;
020
021public class NumberIterablePublisher extends AsyncIterablePublisher<Integer> {
022    public NumberIterablePublisher(final int from, final int to, final Executor executor) {
023        super(new Iterable<Integer>() {
024          { if(from > to) throw new IllegalArgumentException("from must be equal or greater than to!"); }
025          @Override public Iterator<Integer> iterator() {
026            return new Iterator<Integer>() {
027              private int at = from;
028              @Override public boolean hasNext() { return at < to; }
029              @Override public Integer next() {
030                if (!hasNext()) return Collections.<Integer>emptyList().iterator().next();
031                else return at++;
032              }
033              @Override public void remove() { throw new UnsupportedOperationException(); }
034            };
035          }
036        }, executor);
037    }
038}