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

hu.akarnokd.rxjava2.internal.operators.OperatorBufferExactBoundary 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.Collection;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.disposables.Disposable;
import hu.akarnokd.rxjava2.functions.Supplier;
import hu.akarnokd.rxjava2.internal.queue.MpscLinkedQueue;
import hu.akarnokd.rxjava2.internal.subscribers.*;
import hu.akarnokd.rxjava2.internal.subscriptions.*;
import hu.akarnokd.rxjava2.internal.util.QueueDrainHelper;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;

public final class OperatorBufferExactBoundary, B> implements Operator {
    final Publisher boundary;
    final Supplier bufferSupplier;
    
    public OperatorBufferExactBoundary(Publisher boundary, Supplier bufferSupplier) {
        this.boundary = boundary;
        this.bufferSupplier = bufferSupplier;
    }

    @Override
    public Subscriber apply(Subscriber t) {
        return new BufferExactBondarySubscriber(new SerializedSubscriber(t), bufferSupplier, boundary);
    }
    
    static final class BufferExactBondarySubscriber, B>
    extends QueueDrainSubscriber implements Subscriber, Subscription, Disposable {
        /** */
        final Supplier bufferSupplier;
        final Publisher boundary;
        
        Subscription s;
        
        Disposable other;
        
        U buffer;
        
        public BufferExactBondarySubscriber(Subscriber actual, Supplier bufferSupplier,
                Publisher boundary) {
            super(actual, new MpscLinkedQueue());
            this.bufferSupplier = bufferSupplier;
            this.boundary = boundary;
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            this.s = s;
            
            U b;
            
            try {
                b = bufferSupplier.get();
            } catch (Throwable e) {
                cancelled = true;
                s.cancel();
                EmptySubscription.error(e, actual);
                return;
            }
            
            if (b == null) {
                cancelled = true;
                s.cancel();
                EmptySubscription.error(new NullPointerException("The buffer supplied is null"), actual);
                return;
            }
            buffer = b;
            
            BufferBoundarySubscriber bs = new BufferBoundarySubscriber(this);
            other = bs;
            
            actual.onSubscribe(this);
            
            if (!cancelled) {
                s.request(Long.MAX_VALUE);
                
                boundary.subscribe(bs);
            }
        }
        
        @Override
        public void onNext(T t) {
            synchronized (this) {
                U b = buffer;
                if (b == null) {
                    return;
                }
                b.add(t);
            }
        }
        
        @Override
        public void onError(Throwable t) {
            cancel();
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            U b;
            synchronized (this) {
                b = buffer;
                if (b == null) {
                    return;
                }
                buffer = null;
            }
            queue.offer(b);
            done = true;
            if (enter()) {
                QueueDrainHelper.drainMaxLoop(queue, actual, false, this, this);
            }
        }
        
        @Override
        public void request(long n) {
            requested(n);
        }
        
        @Override
        public void cancel() {
            if (!cancelled) {
                cancelled = true;
                other.dispose();
                s.cancel();
                
                if (enter()) {
                    queue.clear();
                }
            }
        }
        
        void next() {
            
            U next;
            
            try {
                next = bufferSupplier.get();
            } catch (Throwable e) {
                cancel();
                actual.onError(e);
                return;
            }
            
            if (next == null) {
                cancel();
                actual.onError(new NullPointerException("The buffer supplied is null"));
                return;
            }
            
            U b;
            synchronized (this) {
                b = buffer;
                if (b == null) {
                    return;
                }
                buffer = next;
            }
            
            fastpathEmitMax(b, false, this);
        }
        
        @Override
        public void dispose() {
            cancel();
        }
        
        @Override
        public boolean accept(Subscriber a, U v) {
            actual.onNext(v);
            return true;
        }
        
    }
    
    static final class BufferBoundarySubscriber, B> extends DisposableSubscriber {
        final BufferExactBondarySubscriber parent;
        
        public BufferBoundarySubscriber(BufferExactBondarySubscriber parent) {
            this.parent = parent;
        }

        @Override
        public void onNext(B t) {
            parent.next();
        }
        
        @Override
        public void onError(Throwable t) {
            parent.onError(t);
        }
        
        @Override
        public void onComplete() {
            parent.onComplete();
        }
    }
}