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

io.reactivex.rxjava3.internal.jdk8.FlowableMapOptional Maven / Gradle / Ivy

There is a newer version: 3.1.9
Show newest version
/*
 * Copyright (c) 2016-present, RxJava Contributors.
 *
 * 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 io.reactivex.rxjava3.internal.jdk8;

import java.util.*;

import org.reactivestreams.Subscriber;

import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.subscribers.*;
import io.reactivex.rxjava3.operators.ConditionalSubscriber;

/**
 * Map the upstream values into an Optional and emit its value if any.
 * @param  the upstream element type
 * @param  the output element type
 * @since 3.0.0
 */
public final class FlowableMapOptional extends Flowable {

    final Flowable source;

    final Function> mapper;

    public FlowableMapOptional(Flowable source, Function> mapper) {
        this.source = source;
        this.mapper = mapper;
    }

    @Override
    protected void subscribeActual(Subscriber s) {
        if (s instanceof ConditionalSubscriber) {
            source.subscribe(new MapOptionalConditionalSubscriber<>((ConditionalSubscriber)s, mapper));
        } else {
            source.subscribe(new MapOptionalSubscriber<>(s, mapper));
        }
    }

    static final class MapOptionalSubscriber extends BasicFuseableSubscriber
    implements ConditionalSubscriber {

        final Function> mapper;

        MapOptionalSubscriber(Subscriber downstream, Function> mapper) {
            super(downstream);
            this.mapper = mapper;
        }

        @Override
        public void onNext(T t) {
            if (!tryOnNext(t)) {
                upstream.request(1);
            }
        }

        @Override
        public boolean tryOnNext(T t) {
            if (done) {
                return true;
            }

            if (sourceMode != NONE) {
                downstream.onNext(null);
                return true;
            }

            Optional result;
            try {
                result = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional");
            } catch (Throwable ex) {
                fail(ex);
                return true;
            }

            if (result.isPresent()) {
                downstream.onNext(result.get());
                return true;
            }
            return false;
        }

        @Override
        public int requestFusion(int mode) {
            return transitiveBoundaryFusion(mode);
        }

        @Override
        public R poll() throws Throwable {
            for (;;) {
                T item = qs.poll();
                if (item == null) {
                    return null;
                }
                Optional result = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null Optional");
                if (result.isPresent()) {
                    return result.get();
                }
                if (sourceMode == ASYNC) {
                    qs.request(1);
                }
            }
        }
    }

    static final class MapOptionalConditionalSubscriber extends BasicFuseableConditionalSubscriber {

        final Function> mapper;

        MapOptionalConditionalSubscriber(ConditionalSubscriber downstream, Function> mapper) {
            super(downstream);
            this.mapper = mapper;
        }

        @Override
        public void onNext(T t) {
            if (!tryOnNext(t)) {
                upstream.request(1);
            }
        }

        @Override
        public boolean tryOnNext(T t) {
            if (done) {
                return true;
            }

            if (sourceMode != NONE) {
                downstream.onNext(null);
                return true;
            }

            Optional result;
            try {
                result = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional");
            } catch (Throwable ex) {
                fail(ex);
                return true;
            }

            if (result.isPresent()) {
                return downstream.tryOnNext(result.get());
            }
            return false;
        }

        @Override
        public int requestFusion(int mode) {
            return transitiveBoundaryFusion(mode);
        }

        @Override
        public R poll() throws Throwable {
            for (;;) {
                T item = qs.poll();
                if (item == null) {
                    return null;
                }
                Optional result = Objects.requireNonNull(mapper.apply(item), "The mapper returned a null Optional");
                if (result.isPresent()) {
                    return result.get();
                }
                if (sourceMode == ASYNC) {
                    qs.request(1);
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy