Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016 the original author or authors.
*
* 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 net.jodah.failsafe;
import net.jodah.failsafe.function.*;
import net.jodah.failsafe.internal.util.Assert;
import net.jodah.failsafe.util.concurrent.Scheduler;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
/**
* Utilities for creating and applying functions.
*
* @author Jonathan Halterman
*/
final class Functions {
private static final CompletableFuture NULL_FUTURE = CompletableFuture.completedFuture(null);
interface SettableSupplier extends Supplier {
void set(T value);
}
/**
* Returns a Supplier that pre-executes the {@code execution}, applies the {@code supplier}, records the result and
* returns the result. This implementation also handles Thread interrupts.
*/
static Supplier get(CheckedSupplier supplier, AbstractExecution execution) {
return () -> {
ExecutionResult result;
Throwable throwable = null;
try {
execution.preExecute();
result = ExecutionResult.success(supplier.get());
} catch (Throwable t) {
throwable = t;
result = ExecutionResult.failure(t);
} finally {
// Guard against race with Timeout interruption
synchronized (execution) {
execution.canInterrupt = false;
if (execution.interrupted)
// Clear interrupt flag if interruption was intended
Thread.interrupted();
else if (throwable instanceof InterruptedException)
// Set interrupt flag if interrupt occurred but was not intentional
Thread.currentThread().interrupt();
}
}
execution.record(result);
return result;
};
}
/**
* Returns a Supplier that pre-executes the {@code execution}, applies the {@code supplier}, records the result and
* returns a promise containing the result.
*/
static Supplier> getPromise(ContextualSupplier supplier,
AbstractExecution execution) {
Assert.notNull(supplier, "supplier");
return () -> {
ExecutionResult result;
try {
execution.preExecute();
result = ExecutionResult.success(supplier.get(execution));
} catch (Throwable t) {
result = ExecutionResult.failure(t);
}
execution.record(result);
return CompletableFuture.completedFuture(result);
};
}
/**
* Returns a Supplier that asynchronously applies the {@code supplier} on the first call, synchronously on subsequent
* calls, and returns a promise containing the result.
*/
@SuppressWarnings("unchecked")
static Supplier> getPromiseAsync(
Supplier> supplier, Scheduler scheduler, FailsafeFuture