Package net.kolotyluk.loom
Record Class Lag
java.lang.Object
java.lang.Record
net.kolotyluk.loom.Lag
- Record Components:
minimum
- Duration to waitmaximum
- Duration to wait
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 interruptBut, we don't always want to deal with the normal try-catch-InterruptedException.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal boolean
Indicates whether some other object is "equal to" this one.Compute a random Duration, unless it's definite.final int
hashCode()
Returns a hash code value for this object.maximum()
Returns the value of themaximum
record component.minimum()
Returns the value of theminimum
record component.sleep()
CallThread.sleep(Duration)
for the chosen duration.sleep
(Consumer<InterruptedException> exceptionHandler) CallThread.sleep(Duration)
for the chosen duration and handle any InterruptedException if, and only if an exceptionHandler is given.final String
toString()
Returns a string representation of this record class.
-
Constructor Details
-
Method Details
-
getDuration
Compute a random Duration, unless it's definite.- Returns:
- new 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
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
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. -
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. -
equals
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 withObjects::equals(Object,Object)
. -
minimum
Returns the value of theminimum
record component.- Returns:
- the value of the
minimum
record component
-
maximum
Returns the value of themaximum
record component.- Returns:
- the value of the
maximum
record component
-