com.hivemq.client.internal.rx.RxFutureConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hivemq-mqtt-client Show documentation
Show all versions of hivemq-mqtt-client Show documentation
HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
/*
* Copyright 2018-present HiveMQ and the HiveMQ Community
*
* 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 com.hivemq.client.internal.rx;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
/**
* @author Silvio Giebl
*/
public final class RxFutureConverter {
public static @NotNull CompletableFuture toFuture(final @NotNull Completable completable) {
return new RxCompletableFuture(completable);
}
public static @NotNull CompletableFuture<@NotNull Optional> toFuture(final @NotNull Maybe maybe) {
return new RxMaybeFuture<>(maybe);
}
public static @NotNull CompletableFuture<@NotNull T> toFuture(final @NotNull Single single) {
return new RxSingleFuture<>(single);
}
private static abstract class RxFuture extends CompletableFuture {
volatile @Nullable Disposable disposable;
volatile boolean cancelled;
public void onSubscribe(final @NotNull Disposable d) {
disposable = d;
if (cancelled) {
d.dispose();
}
}
public void onError(final @NotNull Throwable e) {
if (!cancelled) {
completeExceptionally(e);
}
}
@Override
public boolean cancel(final boolean mayInterruptIfRunning) {
cancelled = true;
final Disposable disposable = this.disposable;
if (disposable != null) {
disposable.dispose();
}
return super.cancel(mayInterruptIfRunning);
}
}
private static class RxCompletableFuture extends RxFuture implements CompletableObserver {
RxCompletableFuture(final @NotNull Completable completable) {
completable.subscribe(this);
}
@Override
public void onComplete() {
if (!cancelled) {
complete(null);
}
}
}
private static class RxMaybeFuture extends RxFuture> implements MaybeObserver {
RxMaybeFuture(final @NotNull Maybe maybe) {
maybe.subscribe(this);
}
@Override
public void onSuccess(final @NotNull T t) {
if (!cancelled) {
complete(Optional.of(t));
}
}
@Override
public void onComplete() {
if (!cancelled) {
complete(Optional.empty());
}
}
}
private static class RxSingleFuture extends RxFuture implements SingleObserver {
RxSingleFuture(final @NotNull Single single) {
single.subscribe(this);
}
@Override
public void onSuccess(final @NotNull T t) {
if (!cancelled) {
complete(t);
}
}
}
public static @NotNull Completable toCompletable(final @NotNull CompletableFuture> future) {
return new FutureCompletable(future);
}
public static @NotNull Maybe toMaybe(final @NotNull CompletableFuture<@NotNull Optional> future) {
return new FutureMaybe<>(future);
}
public static @NotNull Single toSingle(final @NotNull CompletableFuture<@NotNull T> future) {
return new FutureSingle<>(future);
}
private static final int INITIAL = 0;
private static final int SUBSCRIBED_OR_COMPLETE = 1;
private static final int SUBSCRIBED_AND_COMPLETE_OR_CANCELLED = 2;
private static boolean checkComplete(final @NotNull AtomicInteger done) {
return !done.compareAndSet(INITIAL, SUBSCRIBED_OR_COMPLETE) &&
done.compareAndSet(SUBSCRIBED_OR_COMPLETE, SUBSCRIBED_AND_COMPLETE_OR_CANCELLED);
}
private static void dispose(final @NotNull AtomicInteger done, final @NotNull CompletableFuture> future) {
done.set(SUBSCRIBED_AND_COMPLETE_OR_CANCELLED);
future.cancel(false);
}
private static boolean isDisposed(final @NotNull AtomicInteger done) {
return done.get() == SUBSCRIBED_AND_COMPLETE_OR_CANCELLED;
}
private static class FutureCompletable extends Completable implements Disposable, BiConsumer