Skip to content

Commit

Permalink
Better comments and variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
kytpbs committed Dec 16, 2024
1 parent 07bb16e commit efe0b17
Showing 1 changed file with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BooleanSupplier;

/**
Expand Down Expand Up @@ -403,27 +402,24 @@ public RepeatCommand repeatedly() {
}

/**
* Decorates this command to run repeatedly, restarting until the command runs for the given times
* amount. The decorated command can still be canceled.
*
* <p>This is just syntactic sugar for {@code this.finallyDo(() ->
* counter[0]++).repeatedly().until(() -> counter[0] >= times)} which just means: runs the
* command, then increments the counter, repeatedly until the counter saturates.
* Decorates this command to run repeatedly, restarting until the command runs for the given
* number of times. The decorated command can still be canceled.
*
* <p>Note: This decorator works by adding this command to a composition. The command the
* decorator was called on cannot be scheduled independently or be added to a different
* composition (namely, decorators), unless it is manually cleared from the list of composed
* commands with {@link CommandScheduler#removeComposedCommand(Command)}. The command composition
* returned from this method can be further decorated without issue.
*
* @param times the count of times to run the command (inclusively).
* @param repetitions the count of times to run the command.
* @return the decorated command
*/
public ParallelRaceGroup repeatedly(int times) {
AtomicInteger counter = new AtomicInteger(0);
return this.finallyDo(counter::getAndIncrement)
.repeatedly()
.until(() -> counter.get() >= times);
public ParallelRaceGroup repeatedly(int repetitions) {
// use an array so that it stays in the heap instead of stack.
// We use an array with a size of 1 instead of `AtomicInteger` because of the performance
// overhead
int[] counter = new int[1];
return this.finallyDo(() -> counter[0]++).repeatedly().until(() -> counter[0] >= repetitions);
}

/**
Expand Down

0 comments on commit efe0b17

Please sign in to comment.