mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Include zone ID in CronTrigger's equals() and hashCode() implementations
CronTrigger carries an optional ZoneId since 5.3 that affects nextExecution; however, prior to this commit, equals() and hashCode() only considered the cron expression. This commit ensures that CronTrigger instances with the same cron expression but different time zones are no longer considered equal. Closes gh-36871 Signed-off-by: zhaomeng <zhaomeng1.vendor@sensetime.com>
This commit is contained in:
+4
-2
@@ -19,6 +19,7 @@ package org.springframework.scheduling.support;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Objects;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
@@ -145,12 +146,13 @@ public class CronTrigger implements Trigger {
|
||||
@Override
|
||||
public boolean equals(@Nullable Object other) {
|
||||
return (this == other || (other instanceof CronTrigger that &&
|
||||
this.expression.equals(that.expression)));
|
||||
this.expression.equals(that.expression) &&
|
||||
Objects.equals(this.zoneId, that.zoneId)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.expression.hashCode();
|
||||
return Objects.hash(this.expression, this.zoneId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+11
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.scheduling.support;
|
||||
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
@@ -746,6 +747,16 @@ class CronTriggerTests {
|
||||
assertThat(nextExecutionTime).isEqualTo(this.calendar.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsAndHashCodeConsiderZoneId() {
|
||||
String cron = "0 0 9 * * *";
|
||||
CronTrigger amsterdam = new CronTrigger(cron, ZoneId.of("Europe/Amsterdam"));
|
||||
CronTrigger newYork = new CronTrigger(cron, ZoneId.of("America/New_York"));
|
||||
|
||||
assertThat(amsterdam).isNotEqualTo(newYork);
|
||||
assertThat(amsterdam).doesNotHaveSameHashCodeAs(newYork);
|
||||
}
|
||||
|
||||
|
||||
private static void roundup(Calendar calendar) {
|
||||
calendar.add(Calendar.SECOND, 1);
|
||||
|
||||
Reference in New Issue
Block a user