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

io.activej.promise.CompleteExceptionallyPromise Maven / Gradle / Ivy

Go to download

A convenient way to organize asynchronous code. Promises are a faster and more efficient version of JavaScript's Promise and Java's CompletionStage's.

There is a newer version: 6.0-rc2
Show newest version
/*
 * Copyright (C) 2020 ActiveJ LLC.
 *
 * 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.activej.promise;

import io.activej.async.callback.Callback;
import io.activej.async.function.*;
import io.activej.common.collection.Try;
import io.activej.common.function.*;
import io.activej.common.recycle.Recyclers;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;

import static io.activej.common.exception.FatalErrorHandler.handleError;
import static io.activej.reactor.Reactor.getCurrentReactor;

/**
 * Represents a {@code Promise} which is completed with an exception.
 */
@SuppressWarnings("unchecked")
public final class CompleteExceptionallyPromise implements Promise {
	private final Exception exception;

	public CompleteExceptionallyPromise(Exception e) {
		this.exception = e;
	}

	@Override
	public boolean isComplete() {
		return true;
	}

	@Override
	public boolean isResult() {
		return false;
	}

	@Override
	public boolean isException() {
		return true;
	}

	@Override
	public T getResult() {
		return null;
	}

	@Override
	public Exception getException() {
		return exception;
	}

	@Override
	public Try getTry() {
		return Try.ofException(exception);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  Promise map(FunctionEx fn) {
		return (Promise) this;
	}

	@Override
	public  Promise map(BiFunctionEx fn) {
		try {
			return Promise.of(fn.apply(null, exception));
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise map(FunctionEx fn, FunctionEx exceptionFn) {
		try {
			return Promise.of(exceptionFn.apply(exception));
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise mapException(FunctionEx exceptionFn) {
		try {
			return Promise.ofException(exceptionFn.apply(exception));
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise mapException(
		Class clazz, FunctionEx exceptionFn
	) {
		try {
			return clazz.isAssignableFrom(exception.getClass()) ? Promise.ofException(exceptionFn.apply((E) exception)) : this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise then(AsyncFunctionEx fn) {
		return (Promise) this;
	}

	@Override
	public  Promise thenCallback(CallbackFunctionEx fn) {
		return (Promise) this;
	}

	@Override
	public  Promise then(AsyncSupplierEx fn) {
		return (Promise) this;
	}

	@Override
	public  Promise thenCallback(CallbackSupplierEx fn) {
		return (Promise) this;
	}

	@Override
	public  Promise then(AsyncBiFunctionEx fn) {
		try {
			return fn.apply(null, exception);
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise thenCallback(CallbackBiFunctionEx fn) {
		try {
			return Promise.ofCallback(null, exception, fn);
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise then(AsyncFunctionEx fn, AsyncFunctionEx exceptionFn) {
		try {
			return exceptionFn.apply(exception);
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise thenCallback(CallbackFunctionEx fn, CallbackFunctionEx exceptionFn) {
		try {
			return Promise.ofCallback(null, exception, fn, exceptionFn);
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise whenComplete(BiConsumerEx fn) {
		try {
			fn.accept(null, exception);
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise whenComplete(ConsumerEx fn, ConsumerEx exceptionFn) {
		try {
			exceptionFn.accept(exception);
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise whenComplete(RunnableEx action) {
		try {
			action.run();
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise whenResult(ConsumerEx fn) {
		return this;
	}

	@Override
	public Promise whenResult(RunnableEx action) {
		return this;
	}

	@Override
	public Promise whenException(ConsumerEx fn) {
		try {
			fn.accept(exception);
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise whenException(Class clazz, ConsumerEx fn) {
		try {
			if (clazz.isAssignableFrom(exception.getClass())) {
				fn.accept((E) exception);
			}
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise whenException(RunnableEx action) {
		try {
			action.run();
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public Promise whenException(Class clazz, RunnableEx action) {
		try {
			if (clazz.isAssignableFrom(exception.getClass())) {
				action.run();
			}
			return this;
		} catch (Exception ex) {
			handleError(ex, this);
			return Promise.ofException(ex);
		}
	}

	@Override
	public  Promise combine(Promise other, BiFunctionEx fn) {
		other.whenResult(Recyclers::recycle);
		return (Promise) this;
	}

	@Override
	public Promise both(Promise other) {
		other.whenResult(Recyclers::recycle);
		return (Promise) this;
	}

	@Override
	public Promise either(Promise other) {
		return (Promise) other;
	}

	@Override
	public Promise async() {
		SettablePromise result = new SettablePromise<>();
		getCurrentReactor().post(() -> result.setException(exception));
		return result;
	}

	@Override
	public Promise> toTry() {
		return Promise.of(Try.ofException(exception));
	}

	@Override
	public Promise toVoid() {
		return (Promise) this;
	}

	@Override
	public void next(NextPromise cb) {
		cb.acceptNext(null, exception);
	}

	@Override
	public Promise subscribe(Callback cb) {
		cb.accept(null, exception);
		return this;
	}

	@Override
	public CompletableFuture toCompletableFuture() {
		CompletableFuture future = new CompletableFuture<>();
		future.completeExceptionally(exception);
		return future;
	}
}