001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.server.reactive.reactor; 018 019import org.apache.juneau.rest.server.reactive.*; 020import org.reactivestreams.*; 021 022import io.reactivex.rxjava3.core.*; 023 024/** 025 * {@link ReactiveStreamsAdapter} for <a class="doclink" href="https://github.com/ReactiveX/RxJava">RxJava 3</a> 026 * return types. 027 * 028 * <ul> 029 * <li>{@link Single Single<T>} → single value (via {@link Single#toCompletionStage()}). 030 * <li>{@link Maybe Maybe<T>} → single value (via {@link Maybe#toCompletionStage(Object)} with a 031 * {@code null} default, so an empty {@code Maybe} yields a {@code null} body). 032 * <li>{@link Completable} → single {@code null} value (via {@link Completable#toCompletionStage(Object)}). 033 * <li>{@link Flowable Flowable<T>} → stream (via {@link org.reactivestreams.FlowAdapters#toFlowPublisher(Publisher)}). 034 * <li>{@link Observable Observable<T>} → stream (converted to a {@link Flowable} with 035 * {@link BackpressureStrategy#BUFFER} first, since {@code Observable} has no native backpressure). 036 * </ul> 037 * 038 * @since 10.0.0 039 */ 040public class RxJavaReactiveAdapter implements ReactiveStreamsAdapter { 041 042 @Override /* Overridden from ReactiveStreamsAdapter */ 043 public boolean canAdapt(Object value) { 044 return value instanceof Single 045 || value instanceof Maybe 046 || value instanceof Completable 047 || value instanceof Flowable 048 || value instanceof Observable; 049 } 050 051 @Override /* Overridden from ReactiveStreamsAdapter */ 052 public Adaptation adapt(Object value) { 053 if (value instanceof Single<?> value2) 054 return Adaptation.single(value2.toCompletionStage()); 055 if (value instanceof Maybe<?> value2) 056 return Adaptation.single(value2.toCompletionStage(null)); 057 if (value instanceof Completable value2) 058 return Adaptation.single(value2.toCompletionStage(null)); 059 if (value instanceof Flowable<?> value2) 060 return Adaptation.stream(FlowAdapters.toFlowPublisher(value2)); 061 var o = (Observable<?>) value; 062 return Adaptation.stream(FlowAdapters.toFlowPublisher(o.toFlowable(BackpressureStrategy.BUFFER))); 063 } 064}