Record Class Lag

java.lang.Object
java.lang.Record
net.kolotyluk.loom.Lag
Record Components:
minimum - Duration to wait
maximum - Duration to wait

public record Lag(Duration minimum, Duration maximum) extends Record

Induced Lag

There are many reasons we might want to induce lag into our code, such as for testing, benchmarking, experimenting, etc. For example

 var definiteLag = new Lag(Duration.ofMillis(1));
 var randomLag   = new Lag(Duration.ofMillis(1), Duration.ofMillis(10));
 . . .
 definiteLag.sleep(); // sleep for 1 millisecond
 randomLag.sleep();   // sleep randomly between 1 and 10 milliseconds
 randomLag.sleep(cause -> Throw cause); // handle interrupt
     
But, we don't always want to deal with the normal try-catch-InterruptedException.

  • Constructor Details

    • Lag

      public Lag(Duration duration)
      Construct a Lag with a definite Duration.
      Parameters:
      duration -
    • Lag

      public Lag(Duration minimum, Duration maximum)
      Creates an instance of a Lag record class.
      Parameters:
      minimum - the value for the minimum record component
      maximum - the value for the maximum record component
  • Method Details

    • getDuration

      public Duration getDuration()
      Compute a random Duration, unless it's definite.
      Returns:
      new duration.
    • sleep

      public Duration sleep()

      Call Thread.sleep(Duration) for the chosen duration.

      Note: this does not throw InterruptedException.

      Returns:
      Duration of time chosen to sleep for.
      Throws:
      IllegalArgumentException - if maximum is less than minimum
    • sleep

      public Duration sleep(Consumer<InterruptedException> exceptionHandler)

      Call Thread.sleep(Duration) for the chosen duration and handle any InterruptedException if, and only if an exceptionHandler is given.

       

      lag.sleep(cause -> Throw cause);
      Parameters:
      exceptionHandler -
      Returns:
      Duration of time chosen to sleep for.
      Throws:
      IllegalArgumentException - if maximum is less than minimum
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • minimum

      public Duration minimum()
      Returns the value of the minimum record component.
      Returns:
      the value of the minimum record component
    • maximum

      public Duration maximum()
      Returns the value of the maximum record component.
      Returns:
      the value of the maximum record component