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

hu.akarnokd.rxjava2.internal.subscriptions.ScalarAsyncSubscription 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.subscriptions;

import java.util.concurrent.atomic.AtomicInteger;

import org.reactivestreams.*;

import hu.akarnokd.rxjava2.internal.functions.Objects;
import hu.akarnokd.rxjava2.plugins.RxJavaPlugins;

/**
 * A Subscription that coordinates the emission of a single value set asynchronously.
 * @param  the value type
 */
public final class ScalarAsyncSubscription extends AtomicInteger implements Subscription {
    /** */
    private static final long serialVersionUID = -7135031000854703151L;
    
    private T value;
    
    private final Subscriber subscriber;
    
    private static final int EMPTY = 0;
    private static final int REQUESTED = 1;
    private static final int VALUE = 2;
    
    public ScalarAsyncSubscription(Subscriber subscriber) {
        this.subscriber = subscriber;
    }
    
    @Override
    public void request(long n) {
        if (n <= 0) {
            RxJavaPlugins.onError(new IllegalArgumentException("n > 0 required but it was " + n));
            return;
        }
        for (;;) {
            int state = get();
            if ((state & REQUESTED) != 0) {
                return;
            }
            if (state == EMPTY) {
                if (compareAndSet(state, REQUESTED)) {
                    return;
                }
            } else
            if (state == VALUE) {
                if (compareAndSet(state, REQUESTED | VALUE)) {
                    subscriber.onNext(value);
                    subscriber.onComplete();
                    return;
                }
            }
        }
    }
    
    public void setValue(T v) {
        Objects.requireNonNull(v, "v is null");
        
        for (;;) {
            int state = get();
            if ((state & VALUE) != 0) {
                return;
            } else
            if (state == EMPTY) {
                value = v;
                if (compareAndSet(state, VALUE)) {
                    return;
                }
            } else
            if (state == REQUESTED) {
                if (compareAndSet(state, REQUESTED | VALUE)) {
                    subscriber.onNext(v);
                    subscriber.onComplete();
                    return;
                }
            }
        }
    }
    
    @Override
    public void cancel() {
        int state = get();
        if (state != (REQUESTED | VALUE)) {
            state = getAndSet(REQUESTED | VALUE);
            if (state != (REQUESTED | VALUE)) {
                value = null;
            }
        }
    }
    
    public boolean isComplete() {
        return get() == (REQUESTED | VALUE);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy