Skip to content

Commit

Permalink
Add convenience factory methods to JobSchedule (#134)
Browse files Browse the repository at this point in the history
* Add convenience factory methods to JobSchedule

* Add ofIntervalDelay factory methods to JobSchedule; one accepts a
  io.dropwizard.Duration and the other accepts a java.time.Duration

Closes #133
  • Loading branch information
sleberknight authored May 6, 2021
1 parent 8da59e2 commit d1d6bd8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.kiwiproject.dropwizard.util.config;

import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;

import io.dropwizard.util.Duration;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -20,13 +22,15 @@
@AllArgsConstructor(access = AccessLevel.PACKAGE)
public class JobSchedule {

static final Duration DEFAULT_INITIAL_DELAY = Duration.seconds(30);

/**
* Initial delay before the job will run. This should be passed to the ScheduledExecutor's initialDelay. Defaults
* to 30 seconds.
*/
@NotNull
@Builder.Default
private Duration initialDelay = Duration.seconds(30);
private Duration initialDelay = DEFAULT_INITIAL_DELAY;

/**
* Delay between job runs. If using a fixedDelay, this will be the time between the end of one job run and the
Expand All @@ -36,4 +40,39 @@ public class JobSchedule {
@NotNull
private Duration intervalDelay;

/**
* Create a new {@link JobSchedule} instance with the given interval delay and the default initial delay.
* <p>
* This is useful when programmatically creating JobSchedule objects.
*
* @param intervalDelay the interval delay as a Dropwizard {@link Duration}
* @return a new instance
*/
public static JobSchedule ofIntervalDelay(Duration intervalDelay) {
checkIntervalDelayNotNull(intervalDelay);

return JobSchedule.builder()
.intervalDelay(intervalDelay)
.build();
}

/**
* Create a new {@link JobSchedule} instance with the given interval delay and the default initial delay.
* <p>
* This is useful when programmatically creating JobSchedule objects.
*
* @param intervalDelay the interval delay as a JDK {@link java.time.Duration}
* @return a new instance
*/
public static JobSchedule ofIntervalDelay(java.time.Duration intervalDelay) {
checkIntervalDelayNotNull(intervalDelay);

return JobSchedule.builder()
.intervalDelay(Duration.milliseconds(intervalDelay.toMillis()))
.build();
}

private static void checkIntervalDelayNotNull(Object intervalDelay) {
checkArgumentNotNull(intervalDelay, "intervalDelay must not be null");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.kiwiproject.dropwizard.util.config;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import io.dropwizard.util.Duration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

@DisplayName("JobSchedule")
class JobScheduleTest {

@Nested
class OfIntervalDelay {

public static final int SECONDS = 45;

@Test
void shouldNotPermitNullDropwizardDuration() {
assertThatIllegalArgumentException().isThrownBy(() ->
JobSchedule.ofIntervalDelay((Duration) null))
.withMessage("intervalDelay must not be null");
}

@Test
void shouldCreateInstanceWithDropwizardDuration() {
var jobSchedule = JobSchedule.ofIntervalDelay(Duration.seconds(SECONDS));

assertThat(jobSchedule.getInitialDelay()).isEqualTo(JobSchedule.DEFAULT_INITIAL_DELAY);
assertThat(jobSchedule.getIntervalDelay().toSeconds()).isEqualTo(SECONDS);
}

@Test
void shouldNotPermitNullJavaTimeDuration() {
assertThatIllegalArgumentException().isThrownBy(() ->
JobSchedule.ofIntervalDelay((java.time.Duration) null))
.withMessage("intervalDelay must not be null");
}

@Test
void shouldCreateInstanceWithJavaTimeDuration() {
var jobSchedule = JobSchedule.ofIntervalDelay(java.time.Duration.ofSeconds(SECONDS));

assertThat(jobSchedule.getInitialDelay()).isEqualTo(JobSchedule.DEFAULT_INITIAL_DELAY);
assertThat(jobSchedule.getIntervalDelay().toSeconds()).isEqualTo(SECONDS);
}
}
}

0 comments on commit d1d6bd8

Please sign in to comment.