All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.akarnokd.rxjava2.internal.operators.PublisherIterableSource Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
/**
 * Copyright 2015 David Karnok and Netflix, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 * the License for the specific language governing permissions and limitations under the License.
 */

package hu.akarnokd.rxjava2.internal.operators;

import java.util.Iterator;
import java.util.concurrent.atomic.*;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.internal.subscriptions.*;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;

public final class PublisherIterableSource extends AtomicBoolean implements Publisher {
    /** */
    private static final long serialVersionUID = 9051303031779816842L;
    
    final Iterable source;
    public PublisherIterableSource(Iterable source) {
        this.source = source;
    }
    
    @Override
    public void subscribe(Subscriber s) {
        Iterator it;
        try {
            it = source.iterator();
        } catch (Throwable e) {
            EmptySubscription.error(e, s);
            return;
        }
        boolean hasNext;
        try {
            hasNext = it.hasNext();
        } catch (Throwable e) {
            EmptySubscription.error(e, s);
            return;
        }
        if (!hasNext) {
            EmptySubscription.complete(s);
            return;
        }
        s.onSubscribe(new IteratorSourceSubscription(it, s));
    }
    
    static final class IteratorSourceSubscription extends AtomicLong implements Subscription {
        /** */
        private static final long serialVersionUID = 8931425802102883003L;
        final Iterator it;
        final Subscriber subscriber;
        
        volatile boolean cancelled;
        
        public IteratorSourceSubscription(Iterator it, Subscriber subscriber) {
            this.it = it;
            this.subscriber = subscriber;
        }
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            if (BackpressureHelper.add(this, n) != 0L) {
                return;
            }
            long r = n;
            long r0 = n;
            final Subscriber subscriber = this.subscriber;
            final Iterator it = this.it;
            for (;;) {
                if (cancelled) {
                    return;
                }

                long e = 0L;
                while (r != 0L) {
                    T v;
                    try {
                        v = it.next();
                    } catch (Throwable ex) {
                        subscriber.onError(ex);
                        return;
                    }
                    
                    if (v == null) {
                        subscriber.onError(new NullPointerException("Iterator returned a null element"));
                        return;
                    }
                    
                    subscriber.onNext(v);
                    
                    if (cancelled) {
                        return;
                    }
                    
                    boolean hasNext;
                    try {
                        hasNext = it.hasNext();
                    } catch (Throwable ex) {
                        subscriber.onError(ex);
                        return;
                    }
                    if (!hasNext) {
                        subscriber.onComplete();
                        return;
                    }
                    
                    r--;
                    e--;
                }
                if (e != 0L && r0 != Long.MAX_VALUE) {
                    r = addAndGet(e);
                }
                if (r == 0L) {
                    break;
                }
            }
        }
        @Override
        public void cancel() {
            cancelled = true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy