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

io.reactivex.rxjava3.internal.jdk8.ObservableMapOptional 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 io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.observers.BasicFuseableObserver;

/**
 * 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 ObservableMapOptional extends Observable {

    final Observable source;

    final Function> mapper;

    public ObservableMapOptional(Observable source, Function> mapper) {
        this.source = source;
        this.mapper = mapper;
    }

    @Override
    protected void subscribeActual(Observer observer) {
        source.subscribe(new MapOptionalObserver<>(observer, mapper));
    }

    static final class MapOptionalObserver extends BasicFuseableObserver {

        final Function> mapper;

        MapOptionalObserver(Observer downstream, Function> mapper) {
            super(downstream);
            this.mapper = mapper;
        }

        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }

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

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

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

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

        @Override
        public R poll() throws Throwable {
            for (;;) {
                T item = qd.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();
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy