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

hu.akarnokd.rxjava2.internal.operators.OperatorBuffer 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.*;
import java.util.concurrent.atomic.AtomicBoolean;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.functions.Supplier;
import hu.akarnokd.rxjava2.internal.subscribers.EmptySubscriber;
import hu.akarnokd.rxjava2.internal.subscriptions.*;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;

public final class OperatorBuffer> implements Operator {
    final int count;
    final int skip;
    final Supplier bufferSupplier;
    
    public OperatorBuffer(int count, int skip, Supplier bufferSupplier) {
        this.count = count;
        this.skip = skip;
        this.bufferSupplier = bufferSupplier;
    }

    @Override
    public Subscriber apply(Subscriber t) {
        if (skip == count) {
            BufferExactSubscriber bes = new BufferExactSubscriber(t, count, bufferSupplier);
            if (bes.createBuffer()) {
                return bes;
            }
            return EmptySubscriber.INSTANCE;
        }
        return new BufferSkipSubscriber(t, count, skip, bufferSupplier);
    }
    
    static final class BufferExactSubscriber> implements Subscriber, Subscription {
        final Subscriber actual;
        final int count;
        final Supplier bufferSupplier;
        U buffer;
        
        int size;
        
        Subscription s;

        public BufferExactSubscriber(Subscriber actual, int count, Supplier bufferSupplier) {
            this.actual = actual;
            this.count = count;
            this.bufferSupplier = bufferSupplier;
        }
        
        boolean createBuffer() {
            U b;
            try {
                b = bufferSupplier.get();
            } catch (Throwable t) {
                buffer = null;
                if (s == null) {
                    EmptySubscription.error(t, actual);
                } else {
                    s.cancel();
                    actual.onError(t);
                }
                return false;
            }
            
            buffer = b;
            if (b == null) {
                Throwable t = new NullPointerException("Empty buffer supplied");
                if (s == null) {
                    EmptySubscription.error(t, actual);
                } else {
                    s.cancel();
                    actual.onError(t);
                }
                return false;
            }
            
            return true;
        }
        
        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            this.s = s;
            actual.onSubscribe(this);
        }
        
        @Override
        public void onNext(T t) {
            U b = buffer;
            if (b == null) {
                return;
            }
            
            b.add(t);
            
            if (++size >= count) {
                actual.onNext(b);
                
                size = 0;
                createBuffer();
            }
        }
        
        @Override
        public void onError(Throwable t) {
            buffer = null;
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            U b = buffer;
            buffer = null;
            if (b != null && !b.isEmpty()) {
                actual.onNext(b);
            }
            actual.onComplete();
        }
        
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            long m = BackpressureHelper.multiplyCap(n, count);
            s.request(m);
        }
        
        @Override
        public void cancel() {
            s.cancel();
        }
    }
    
    static final class BufferSkipSubscriber> extends AtomicBoolean implements Subscriber, Subscription {
        /** */
        private static final long serialVersionUID = -8223395059921494546L;
        final Subscriber actual;
        final int count;
        final int skip;
        final Supplier bufferSupplier;

        Subscription s;
        
        final ArrayDeque buffers;
        
        long index;

        public BufferSkipSubscriber(Subscriber actual, int count, int skip, Supplier bufferSupplier) {
            this.actual = actual;
            this.count = count;
            this.skip = skip;
            this.bufferSupplier = bufferSupplier;
            this.buffers = new ArrayDeque();
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validateSubscription(this.s, s)) {
                return;
            }
            this.s = s;
            actual.onSubscribe(this);
        }

        @Override
        public void onNext(T t) {
            if (index++ % skip == 0) {
                U b;
                
                try {
                    b = bufferSupplier.get();
                } catch (Throwable e) {
                    buffers.clear();
                    s.cancel();
                    actual.onError(e);
                    return;
                }
                
                if (b == null) {
                    buffers.clear();
                    s.cancel();
                    actual.onError(new NullPointerException());
                    return;
                }
                
                buffers.offer(b);
            }
            
            Iterator it = buffers.iterator();
            while (it.hasNext()) {
                U b = it.next();
                b.add(t);
                if (count <= b.size()) {
                    it.remove();
                    
                    actual.onNext(b);
                }
            }
        }
        
        @Override
        public void onError(Throwable t) {
            buffers.clear();
            actual.onError(t);
        }
        
        @Override
        public void onComplete() {
            while (!buffers.isEmpty()) {
                actual.onNext(buffers.poll());
            }
            actual.onComplete();
        }
        
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            // requesting the first set of buffers must happen only once
            if (!get() && compareAndSet(false, true)) {
                
                if (count < skip) {
                    // don't request the first gap after n buffers
                    long m = BackpressureHelper.multiplyCap(n, count);
                    s.request(m);
                } else {
                    // request 1 full and n - 1 skip gaps
                    long m = BackpressureHelper.multiplyCap(n - 1, skip);
                    long k = BackpressureHelper.addCap(count, m);
                    s.request(k);
                }
                
            } else {
                
                if (count < skip) {
                    // since this isn't the first, request n buffers and n gaps
                    long m = BackpressureHelper.multiplyCap(n, count + skip);
                    s.request(m);
                } else {
                    // request the remaining n * skip
                    long m = BackpressureHelper.multiplyCap(n, skip);
                    s.request(m);
                }
            }
        }
        
        @Override
        public void cancel() {
            s.cancel();
        }
    }
}