Skip to content

Commit 05c28c0

Browse files
committed
Fix #530. Implement #511.
1 parent 3ec272b commit 05c28c0

3 files changed

Lines changed: 77 additions & 8 deletions

File tree

src/main/java/com/cronutils/model/time/ExecutionTime.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,15 @@ default int countExecutions(ZonedDateTime startDate, ZonedDateTime endDate) {
178178
*/
179179
default List<ZonedDateTime> getExecutionDates(ZonedDateTime startDate, ZonedDateTime endDate) {
180180
if (endDate.equals(startDate) || endDate.isBefore(startDate)) {
181-
throw new IllegalArgumentException("End date should be after start date");
181+
throw new IllegalArgumentException("endDate should take place later in time than startDate");
182182
}
183-
184183
List<ZonedDateTime> executions = new ArrayList<>();
185184
ZonedDateTime nextExecutionDate = nextExecution(startDate).orElse(null);
186185

187186
if (nextExecutionDate == null) return Collections.emptyList();
188-
executions.add(nextExecutionDate);
189-
190-
while (nextExecutionDate.isBefore(endDate)) {
191-
nextExecutionDate = nextExecution(nextExecutionDate).orElse(null);
192-
193-
if (nextExecutionDate == null) break;
187+
while(nextExecutionDate != null && nextExecutionDate.isBefore(endDate)){
194188
executions.add(nextExecutionDate);
189+
nextExecutionDate = nextExecution(nextExecutionDate).orElse(null);
195190
}
196191
return executions;
197192
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cronutils.utils;
2+
3+
import com.cronutils.model.Cron;
4+
import com.cronutils.model.time.ExecutionTime;
5+
6+
import java.time.ZonedDateTime;
7+
import java.util.Comparator;
8+
9+
public class CronFrequencyComparator implements Comparator<Cron> {
10+
private final ZonedDateTime startDate;
11+
private final ZonedDateTime endDate;
12+
13+
public CronFrequencyComparator(ZonedDateTime startDate, ZonedDateTime endDate){
14+
this.startDate = startDate;
15+
this.endDate = endDate;
16+
}
17+
18+
@Override
19+
public int compare(Cron o1, Cron o2) {
20+
int executions1 = ExecutionTime.forCron(o1).countExecutions(startDate, endDate);
21+
int executions2 = ExecutionTime.forCron(o2).countExecutions(startDate, endDate);
22+
return executions1-executions2;
23+
}
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.cronutils.utils;
2+
3+
import com.cronutils.model.Cron;
4+
import com.cronutils.model.CronType;
5+
import com.cronutils.model.definition.CronDefinition;
6+
import com.cronutils.model.definition.CronDefinitionBuilder;
7+
import com.cronutils.parser.CronParser;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.time.LocalDateTime;
12+
import java.time.ZoneId;
13+
import java.time.ZonedDateTime;
14+
15+
import static org.junit.Assert.*;
16+
17+
public class CronFrequencyComparatorTest {
18+
private CronFrequencyComparator comparator;
19+
private CronParser parser;
20+
private Cron cron1;
21+
private Cron cron2;
22+
23+
@Before
24+
public void setUp() {
25+
ZonedDateTime date1 = LocalDateTime.of(2018, 11, 5, 0, 0, 0).atZone(ZoneId.of("UTC"));
26+
ZonedDateTime date2 = LocalDateTime.of(2018, 11, 11, 0, 0, 0).atZone(ZoneId.of("UTC"));
27+
comparator = new CronFrequencyComparator(date1, date2);
28+
final CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(CronType.SPRING53);
29+
parser = new CronParser(cronDefinition);
30+
31+
cron1 = parser.parse("0 0 9-17 * * MON-FRI");//on the hour nine-to-five weekdays -> 9 executions per day, five times a week -> 45 executions per week
32+
cron2 = parser.parse("0 0/30 8-10 * * *");//8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day -> six executions per day, seven times a week -> 42 executions per week
33+
}
34+
35+
@Test
36+
public void compareMoreFrequent() {
37+
assertTrue(comparator.compare(cron1, cron2)>0);
38+
}
39+
40+
@Test
41+
public void compareLessFrequent() {
42+
assertTrue(comparator.compare(cron2, cron1)<0);
43+
}
44+
45+
@Test
46+
public void compareEqualFrequent() {
47+
Cron cronx = parser.parse("0 0 9-17 * * MON-FRI");//on the hour nine-to-five weekdays -> 9 executions per day, five times a week -> 45 executions per week
48+
assertEquals(0, comparator.compare(cron1, cronx));
49+
}
50+
}

0 commit comments

Comments
 (0)