Skip to content

Commit

Permalink
Adding equals and hashcode for CronSchedule
Browse files Browse the repository at this point in the history
  • Loading branch information
0x01F4 committed Nov 13, 2024
1 parent 28ce451 commit 77459a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -134,6 +135,21 @@ public boolean isDeterministic() {
return true;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CronSchedule)) return false;
CronSchedule that = (CronSchedule) o;
return Objects.equals(this.cronStyle, that.cronStyle)
&& Objects.equals(this.zoneId, that.zoneId)
&& Objects.equals(this.pattern, that.pattern);
}

@Override
public final int hashCode() {
return Objects.hash(cronStyle, zoneId, pattern);
}

@Override
public String toString() {
return "CronSchedule pattern=" + pattern + ", cronStyle=" + cronStyle + ", zone=" + zoneId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.github.kagkarlsson.scheduler.task.schedule.CronSchedule;
Expand Down Expand Up @@ -125,7 +127,23 @@ public void should_mark_schedule_as_disabled() {
public void should_not_fail_on_cronschedule_without_next_execution_time() {
CronSchedule cron =
new CronSchedule("0 0 0 29 2 MON#1", ZoneId.systemDefault(), CronStyle.SPRING53);
Assertions.assertEquals(Schedule.NEVER, cron.getNextExecutionTime(complete(Instant.now())));
assertEquals(Schedule.NEVER, cron.getNextExecutionTime(complete(Instant.now())));
}

@Test
public void validate_cron_schedule_equals() {
assertEquals(
new CronSchedule("* * * * *", ZoneId.systemDefault(), CronStyle.UNIX),
new CronSchedule("* * * * *", ZoneId.systemDefault(), CronStyle.UNIX));
assertNotEquals(
new CronSchedule("1 * * * *", ZoneId.systemDefault(), CronStyle.UNIX),
new CronSchedule("* * * * *", ZoneId.systemDefault(), CronStyle.UNIX));
assertNotEquals(
new CronSchedule("1 * * * *", london, CronStyle.UNIX),
new CronSchedule("* * * * *", ZoneId.systemDefault(), CronStyle.UNIX));
assertNotEquals(
new CronSchedule("* * * * * *", ZoneId.systemDefault(), CronStyle.SPRING53),
new CronSchedule("* * * * * *", ZoneId.systemDefault(), CronStyle.SPRING));
}

private void assertNextExecutionTime(
Expand Down

0 comments on commit 77459a1

Please sign in to comment.