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

hu.akarnokd.rxjava2.internal.operators.OperatorMapNotification 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.Observable.Operator;
import hu.akarnokd.rxjava2.functions.*;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;

public final class OperatorMapNotification implements Operator, T>{

    final Function> onNextMapper;
    final Function> onErrorMapper;
    final Supplier> onCompleteSupplier;

    public OperatorMapNotification(Function> onNextMapper, 
            Function> onErrorMapper, 
            Supplier> onCompleteSupplier) {
        this.onNextMapper = onNextMapper;
        this.onErrorMapper = onErrorMapper;
        this.onCompleteSupplier = onCompleteSupplier;
    }
    
    @Override
    public Subscriber apply(Subscriber> t) {
        return new MapNotificationSubscriber(t, onNextMapper, onErrorMapper, onCompleteSupplier);
    }
    
    static final class MapNotificationSubscriber
    extends AtomicLong
    implements Subscriber, Subscription {
        /** */
        private static final long serialVersionUID = 2757120512858778108L;
        
        final Subscriber> actual;
        final Function> onNextMapper;
        final Function> onErrorMapper;
        final Supplier> onCompleteSupplier;
        
        Subscription s;
        
        Publisher value;
        
        volatile boolean done;

        volatile int state;
        @SuppressWarnings("rawtypes")
        static final AtomicIntegerFieldUpdater STATE =
                AtomicIntegerFieldUpdater.newUpdater(MapNotificationSubscriber.class, "state");
        
        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 MapNotificationSubscriber(Subscriber> actual,
                Function> onNextMapper,
                Function> onErrorMapper,
                Supplier> onCompleteSupplier) {
            this.actual = actual;
            this.onNextMapper = onNextMapper;
            this.onErrorMapper = onErrorMapper;
            this.onCompleteSupplier = onCompleteSupplier;
        }
        
        @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) {
            Publisher p;
            
            try {
                p = onNextMapper.apply(t);
            } catch (Throwable e) {
                actual.onError(e);
                return;
            }
            
            if (p == null) {
                actual.onError(new NullPointerException("The onNext publisher returned is null"));
                return;
            }
            
            actual.onNext(p);
            
            long r = get();
            if (r != Long.MAX_VALUE) {
                decrementAndGet();
            }
        }
        
        @Override
        public void onError(Throwable t) {
            Publisher p;
            
            try {
                p = onErrorMapper.apply(t);
            } catch (Throwable e) {
                actual.onError(e);
                return;
            }

            if (p == null) {
                actual.onError(new NullPointerException("The onError publisher returned is null"));
                return;
            }

            tryEmit(p);
        }
        
        @Override
        public void onComplete() {
            Publisher p;
            
            try {
                p = onCompleteSupplier.get();
            } catch (Throwable e) {
                actual.onError(e);
                return;
            }

            if (p == null) {
                actual.onError(new NullPointerException("The onComplete publisher returned is null"));
                return;
            }

            tryEmit(p);
        }
        
        
        void tryEmit(Publisher p) {
            long r = get();
            if (r != 0L) {
                actual.onNext(p);
                actual.onComplete();
            } else {
                for (;;) {
                    int s = state;
                    if (s == HAS_REQUEST_NO_VALUE) {
                        if (STATE.compareAndSet(this, HAS_REQUEST_NO_VALUE, HAS_REQUEST_HAS_VALUE)) {
                            actual.onNext(p);
                            actual.onComplete();
                        }
                        return;
                    } else
                    if (s == NO_REQUEST_NO_VALUE) {
                        value = p;
                        done = true;
                        if (STATE.compareAndSet(this, NO_REQUEST_NO_VALUE, NO_REQUEST_HAS_VALUE)) {
                            return;
                        }
                    } else
                    if (s == NO_REQUEST_HAS_VALUE || s == HAS_REQUEST_HAS_VALUE) {
                        return;
                    }
                }
            }
        }
        
        @Override
        public void request(long n) {
            if (SubscriptionHelper.validateRequest(n)) {
                return;
            }
            
            BackpressureHelper.add(this, n);
            if (done) {
                for (;;) {
                    int s = state;
                    
                    if (s == HAS_REQUEST_NO_VALUE || s == HAS_REQUEST_HAS_VALUE) {
                        return;
                    } else
                    if (s == NO_REQUEST_HAS_VALUE) {
                        if (STATE.compareAndSet(this, NO_REQUEST_HAS_VALUE, HAS_REQUEST_HAS_VALUE)) {
                            Publisher p = value;
                            value = null;
                            actual.onNext(p);
                            actual.onComplete();
                        }
                        return;
                    } else
                    if (STATE.compareAndSet(this, NO_REQUEST_NO_VALUE, HAS_REQUEST_NO_VALUE)) {
                        return;
                    }
                }
            } else {
                s.request(n);
            }
        }
        
        @Override
        public void cancel() {
            STATE.lazySet(this, HAS_REQUEST_HAS_VALUE);
            s.cancel();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy