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

hu.akarnokd.rxjava2.internal.operators.OperatorConcatMap 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.Queue;
import java.util.concurrent.atomic.AtomicInteger;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.functions.Function;
import hu.akarnokd.rxjava2.internal.queue.*;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionArbiter;
import hu.akarnokd.rxjava2.internal.util.Pow2;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;

public final class OperatorConcatMap implements Operator {
    final Function> mapper;
    final int bufferSize;
    public OperatorConcatMap(Function> mapper, int bufferSize) {
        this.mapper = mapper;
        this.bufferSize = bufferSize;
    }
    @Override
    public Subscriber apply(Subscriber s) {
        SerializedSubscriber ssub = new SerializedSubscriber(s);
        SubscriptionArbiter sa = new SubscriptionArbiter();
        ssub.onSubscribe(sa);
        return new SourceSubscriber(ssub, sa, mapper, bufferSize);
    }
    
    static final class SourceSubscriber extends AtomicInteger implements Subscriber {
        /** */
        private static final long serialVersionUID = 8828587559905699186L;
        final Subscriber actual;
        final SubscriptionArbiter sa;
        final Function> mapper;
        final Subscriber inner;
        final Queue queue;
        final int bufferSize;
        
        Subscription s;
        
        volatile boolean done;
        
        volatile long index;
        
        public SourceSubscriber(Subscriber actual, SubscriptionArbiter sa,
                Function> mapper, int bufferSize) {
            this.actual = actual;
            this.sa = sa;
            this.mapper = mapper;
            this.bufferSize = bufferSize;
            this.inner = new InnerSubscriber(actual, sa, this);
            Queue q;
            if (Pow2.isPowerOfTwo(bufferSize)) {
                q = new SpscArrayQueue(bufferSize);
            } else {
                q = new SpscExactArrayQueue(bufferSize);
            }
            this.queue = q;
        }
        @Override
        public void onSubscribe(Subscription s) {
            if (this.s != null) {
                s.cancel();
                RxJavaPlugins.onError(new IllegalStateException("Subscription already set!"));
                return;
            }
            this.s = s;
            s.request(bufferSize);
        }
        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            if (!queue.offer(t)) {
                cancel();
                actual.onError(new IllegalStateException("More values received than requested!"));
                return;
            }
            if (getAndIncrement() == 0) {
                drain();
            }
        }
        @Override
        public void onError(Throwable t) {
            if (done) {
                RxJavaPlugins.onError(t);
                return;
            }
            done = true;
            cancel();
            actual.onError(t);
        }
        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;
            if (getAndIncrement() == 0) {
                drain();
            }
        }
        
        void innerComplete() {
            if (decrementAndGet() != 0) {
                drain();
            }
            if (!done) {
                s.request(1);
            }
        }
        
        void cancel() {
            sa.cancel();
            s.cancel();
        }
        
        void drain() {
            boolean d = done;
            T o = queue.poll();
            
            if (o == null) {
                if (d) {
                    actual.onComplete();
                    return;
                }
                RxJavaPlugins.onError(new IllegalStateException("Queue is empty?!"));
                return;
            }
            Publisher p;
            try {
                p = mapper.apply(o);
            } catch (Throwable e) {
                cancel();
                actual.onError(e);
                return;
            }
            
            if (p == null) {
                cancel();
                actual.onError(new NullPointerException("The publisher returned is null"));
                return;
            }
            
            index++;
            // this is not RS but since our Subscriber doesn't hold state by itself,
            // subscribing it to each source is safe and saves allocation
            p.subscribe(inner);
        }
    }
    
    static final class InnerSubscriber implements Subscriber {
        final Subscriber actual;
        final SubscriptionArbiter sa;
        final SourceSubscriber parent;

        /*
         * FIXME this is a workaround for now, but doesn't work 
         * for async non-conforming sources.
         * Such sources require individual instances of InnerSubscriber and a
         * done field.
         */
         
        long index;
        
        public InnerSubscriber(Subscriber actual, 
                SubscriptionArbiter sa, SourceSubscriber parent) {
            this.actual = actual;
            this.sa = sa;
            this.parent = parent;
            this.index = 1;
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            if (index == parent.index) {
                sa.setSubscription(s);
            }
        }
        
        @Override
        public void onNext(U t) {
            if (index == parent.index) {
                actual.onNext(t);
                sa.produced(1L);
            }
        }
        @Override
        public void onError(Throwable t) {
            if (index == parent.index) {
                index++;
                parent.cancel();
                actual.onError(t);
            } else {
                RxJavaPlugins.onError(t);
            }
        }
        @Override
        public void onComplete() {
            if (index == parent.index) {
                index++;
                parent.innerComplete();
            }
        }
    }
}