001/***************************************************
002 * Licensed under MIT No Attribution (SPDX: MIT-0) *
003 ***************************************************/
004
005package org.reactivestreams.example.unicast;
006
007import java.util.Collections;
008import java.util.Iterator;
009import java.util.concurrent.Executor;
010import org.reactivestreams.Subscription;
011import org.reactivestreams.Subscriber;
012import org.reactivestreams.Publisher;
013
014public class NumberIterablePublisher extends AsyncIterablePublisher<Integer> {
015    public NumberIterablePublisher(final int from, final int to, final Executor executor) {
016        super(new Iterable<Integer>() {
017          { if(from > to) throw new IllegalArgumentException("from must be equal or greater than to!"); }
018          @Override public Iterator<Integer> iterator() {
019            return new Iterator<Integer>() {
020              private int at = from;
021              @Override public boolean hasNext() { return at < to; }
022              @Override public Integer next() {
023                if (!hasNext()) return Collections.<Integer>emptyList().iterator().next();
024                else return at++;
025              }
026              @Override public void remove() { throw new UnsupportedOperationException(); }
027            };
028          }
029        }, executor);
030    }
031}