/** * Tries to get a read-only copy of the State and execute {@code work} on it. * * @param work Work to execute on the State * @param defaultResult Supplies a result in case the State is null * * @return The result of executing {@code work}, or * the result of querying {@code defaultResult} if the State is null * @since 2.14 */ default <Result> Result tryReadOnly( IUnitOfWork<Result, State> work, Supplier<? extends Result> defaultResult ) { // Some implementations rely on the type of {@code work} if (work instanceof CancelableUnitOfWork<?, ?>) { return readOnly(new WrappingCancelableUnitOfWork<>(defaultResult, work)); } return readOnly((state) -> { if (state == null) { return defaultResult.get(); } return work.exec(state); }); }
/** * Tries to modify the State by executing {@code work} on it. * * @param work Work that modifies the State * @param defaultResult Supplies a result in case the State is null * * @return The result of executing {@code work}, or * the result of querying {@code defaultResult} if the State is null * @since 2.14 */ default <Result> Result tryModify( IUnitOfWork<Result, State> work, Supplier<? extends Result> defaultResult ) { // Some implementations rely on the type of {@code work} if (work instanceof CancelableUnitOfWork<?, ?>) { return modify(new WrappingCancelableUnitOfWork<>(defaultResult, work)); } return modify((state) -> { if (state == null) { return defaultResult.get(); } return work.exec(state); }); }
/** * Tries to get a read-only copy of the State and execute {@code work} on it. * Cancels all cancelable readers before executing the {@link IUnitOfWork}. * For interactive jobs that need fastest possible execution. * * @param work Work to execute on the State * @param defaultResult Supplies a result in case the State is null * * @return The result of executing {@code work}, or * the result of querying {@code defaultResult} if the State is null * @since 2.14 * @see CancelableUnitOfWork */ default <Result> Result tryPriorityReadOnly( IUnitOfWork<Result, State> work, Supplier<? extends Result> defaultResult ) { // Some implementations rely on the type of {@code work} if (work instanceof CancelableUnitOfWork<?, ?>) { return priorityReadOnly(new WrappingCancelableUnitOfWork<>(defaultResult, work)); } return priorityReadOnly((state) -> { if (state == null) { return defaultResult.get(); } return work.exec(state); }); }