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

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

/**
 * 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.concurrent.atomic.*;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.*;
import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;

public enum OperatorMaterialize implements Operator>, Object> {
    INSTANCE;
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static  Operator>, T> instance() {
        return (Operator)INSTANCE;
    }
    
    @Override
    public Subscriber apply(Subscriber>> t) {
        return new MaterializeSubscriber(t);
    }
    
    static final class MaterializeSubscriber extends AtomicLong implements Subscriber, Subscription {
        /** */
        private static final long serialVersionUID = -3740826063558713822L;
        final Subscriber>> actual;
        
        Subscription s;
        
        volatile int state;
        @SuppressWarnings("rawtypes")
        static final AtomicIntegerFieldUpdater STATE =
                AtomicIntegerFieldUpdater.newUpdater(MaterializeSubscriber.class, "state");
        Try> value;
        
        volatile boolean done;
        
        static final int NO_REQUEST_NO_VALUE = 0;
        static final int NO_REQUEST_HAS_VALUE = 1;
        static final int HAS_REQUEST_NO_VALUE = 2;
        static final int HAS_REQUEST_HAS_VALUE = 3;
        
        public MaterializeSubscriber(Subscriber>> actual) {
            this.actual = actual;
        }
        
        @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) {
            actual.onNext(Notification.next(t));
            
            if (get() != Long.MAX_VALUE) {
                decrementAndGet();
            }
        }
        
        void tryEmit(Try> v) {
            if (get() != 0L) {
                STATE.lazySet(this, HAS_REQUEST_HAS_VALUE);
                actual.onNext(v);
                actual.onComplete();
            } else {
                for (;;) {
                    int s = state;
                    if (s == HAS_REQUEST_NO_VALUE) {
                        if (STATE.compareAndSet(this, s, HAS_REQUEST_HAS_VALUE)) {
                            actual.onNext(v);
                            actual.onComplete();
                            return;
                        }
                    } else
                    if (s == NO_REQUEST_HAS_VALUE) {
                        return;
                    } else
                    if (s == HAS_REQUEST_HAS_VALUE) {
                        value = null;
                        return;
                    } else {
                        value = v;
                        done = true;
                        if (STATE.compareAndSet(this, s, NO_REQUEST_HAS_VALUE)) {
                            return;
                        }
                    }
                }
            }
        }
        
        @Override
        public void onError(Throwable t) {
            Try> v = Notification.error(t);
            
            tryEmit(v);
        }
        
        @Override
        public void onComplete() {
            Try> v = Notification.complete();
            
            tryEmit(v);
        }
        
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            BackpressureHelper.add(this, n);
            if (done) {
                for (;;) {
                    int s = state;
                    if (s == NO_REQUEST_HAS_VALUE) {
                        if (STATE.compareAndSet(this, s, HAS_REQUEST_HAS_VALUE)) {
                            Try> v = value;
                            value = null;
                            actual.onNext(v);
                            actual.onComplete();
                            return;
                        }
                    } else
                    if (s == HAS_REQUEST_NO_VALUE || s == HAS_REQUEST_HAS_VALUE) {
                        return;
                    } else
                    if (STATE.compareAndSet(this, s, HAS_REQUEST_NO_VALUE)) {
                        return;
                    }
                }
            } else {
                s.request(n);
            }
        }
        
        @Override
        public void cancel() {
            STATE.lazySet(this, HAS_REQUEST_HAS_VALUE);
            s.cancel();
        }
    }
}