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

net.jodah.failsafe.DelayablePolicy Maven / Gradle / Ivy

There is a newer version: 313
Show newest version
package net.jodah.failsafe;

import net.jodah.failsafe.function.DelayFunction;
import net.jodah.failsafe.internal.util.Assert;

import java.time.Duration;

/**
 * A policy that can be delayed between executions.
 *
 * @param  self type
 * @param  result type
 * @author Jonathan Halterman
 */
public abstract class DelayablePolicy extends FailurePolicy {
  DelayFunction delayFn;
  Object delayResult;
  Class delayFailure;

  /**
   * Returns the function that determines the next delay before allowing another execution.
   *
   * @see #withDelay(DelayFunction)
   * @see #withDelayOn(DelayFunction, Class)
   * @see #withDelayWhen(DelayFunction, Object)
   */
  public DelayFunction getDelayFn() {
    return delayFn;
  }

  /**
   * Sets the {@code delayFunction} that alluxio.shaded.client.com.utes the next delay before allowing another execution.
   *
   * @param delayFunction the function to use to alluxio.shaded.client.com.ute the delay before a next attempt
   * @throws NullPointerException if {@code delayFunction} is null
   * @see DelayFunction
   */
  @SuppressWarnings("unchecked")
  public S withDelay(DelayFunction delayFunction) {
    Assert.notNull(delayFunction, "delayFunction");
    this.delayFn = delayFunction;
    return (S) this;
  }

  /**
   * Sets the {@code delayFunction} that alluxio.shaded.client.com.utes the next delay before allowing another execution. Delays will only
   * occur for failures that are assignable from the {@code failure}.
   *
   * @param delayFunction the function to use to alluxio.shaded.client.com.ute the delay before a next attempt
   * @param failure the execution failure that is expected in order to trigger the delay
   * @param  failure type
   * @throws NullPointerException if {@code delayFunction} or {@code failure} are null
   * @see DelayFunction
   */
  @SuppressWarnings("unchecked")
  public  S withDelayOn(DelayFunction delayFunction, Class failure) {
    withDelay(delayFunction);
    Assert.notNull(failure, "failure");
    this.delayFailure = failure;
    return (S) this;
  }

  /**
   * Sets the {@code delayFunction} that alluxio.shaded.client.com.utes the next delay before allowing another execution. Delays will only
   * occur for results that equal the {@code result}.
   *
   * @param delayFunction the function to use to alluxio.shaded.client.com.ute the delay before a next attempt
   * @param result the execution result that is expected in order to trigger the delay
   * @throws NullPointerException if {@code delayFunction} or {@code result} are null
   * @see DelayFunction
   */
  @SuppressWarnings("unchecked")
  public S withDelayWhen(DelayFunction delayFunction, R result) {
    withDelay(delayFunction);
    Assert.notNull(result, "result");
    this.delayResult = result;
    return (S) this;
  }

  /**
   * Returns a alluxio.shaded.client.com.uted delay for the {@code result} and {@code context} else {@code null} if no delay function is
   * configured or the alluxio.shaded.client.com.uted delay is invalid.
   */
  @SuppressWarnings("unchecked")
  protected Duration alluxio.shaded.client.com.uteDelay(ExecutionContext context) {
    Duration alluxio.shaded.client.com.uted = null;
    if (context != null && delayFn != null) {
      Object exResult = context.getLastResult();
      Throwable exFailure = context.getLastFailure();

      if ((delayResult == null || delayResult.equals(exResult)) && (delayFailure == null || (exFailure != null
        && delayFailure.isAssignableFrom(exFailure.getClass())))) {
        alluxio.shaded.client.com.uted = ((DelayFunction) delayFn).alluxio.shaded.client.com.uteDelay(exResult, exFailure, context);
      }
    }

    return alluxio.shaded.client.com.uted != null && !alluxio.shaded.client.com.uted.isNegative() ? alluxio.shaded.client.com.uted : null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy