|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2013 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
29 | 29 | import org.springframework.util.StringUtils;
|
30 | 30 |
|
31 | 31 | /**
|
32 |
| - * Date sequence generator for a <a href="http://www.manpagez.com/man/5/crontab/">Crontab pattern</a>, |
| 32 | + * Date sequence generator for a |
| 33 | + * <a href="http://www.manpagez.com/man/5/crontab/">Crontab pattern</a>, |
33 | 34 | * allowing clients to specify a pattern that the sequence matches.
|
34 | 35 | *
|
35 | 36 | * <p>The pattern is a list of six single space-separated fields: representing
|
|
53 | 54 | */
|
54 | 55 | public class CronSequenceGenerator {
|
55 | 56 |
|
56 |
| - private final BitSet seconds = new BitSet(60); |
57 |
| - |
58 |
| - private final BitSet minutes = new BitSet(60); |
| 57 | + private final String expression; |
59 | 58 |
|
60 |
| - private final BitSet hours = new BitSet(24); |
| 59 | + private final TimeZone timeZone; |
61 | 60 |
|
62 |
| - private final BitSet daysOfWeek = new BitSet(7); |
| 61 | + private final BitSet months = new BitSet(12); |
63 | 62 |
|
64 | 63 | private final BitSet daysOfMonth = new BitSet(31);
|
65 | 64 |
|
66 |
| - private final BitSet months = new BitSet(12); |
| 65 | + private final BitSet daysOfWeek = new BitSet(7); |
67 | 66 |
|
68 |
| - private final String expression; |
| 67 | + private final BitSet hours = new BitSet(24); |
69 | 68 |
|
70 |
| - private final TimeZone timeZone; |
| 69 | + private final BitSet minutes = new BitSet(60); |
| 70 | + |
| 71 | + private final BitSet seconds = new BitSet(60); |
71 | 72 |
|
72 | 73 |
|
73 | 74 | /**
|
@@ -95,6 +96,14 @@ public CronSequenceGenerator(String expression, TimeZone timeZone) {
|
95 | 96 | }
|
96 | 97 |
|
97 | 98 |
|
| 99 | + /** |
| 100 | + * Return the cron pattern that this sequence generator has been built for. |
| 101 | + */ |
| 102 | + String getExpression() { |
| 103 | + return this.expression; |
| 104 | + } |
| 105 | + |
| 106 | + |
98 | 107 | /**
|
99 | 108 | * Get the next {@link Date} in the sequence matching the Cron pattern and
|
100 | 109 | * after the value provided. The return value will have a whole number of
|
@@ -271,9 +280,9 @@ private void parse(String expression) throws IllegalArgumentException {
|
271 | 280 | }
|
272 | 281 |
|
273 | 282 | /**
|
274 |
| - * Replace the values in the commaSeparatedList (case insensitive) with |
275 |
| - * their index in the list. |
276 |
| - * @return a new string with the values from the list replaced |
| 283 | + * Replace the values in the comma-separated list (case insensitive) |
| 284 | + * with their index in the list. |
| 285 | + * @return a new String with the values from the list replaced |
277 | 286 | */
|
278 | 287 | private String replaceOrdinals(String value, String commaSeparatedList) {
|
279 | 288 | String[] list = StringUtils.commaDelimitedListToStringArray(commaSeparatedList);
|
@@ -332,6 +341,10 @@ private void setNumberHits(BitSet bits, String value, int min, int max) {
|
332 | 341 | range[1] = max - 1;
|
333 | 342 | }
|
334 | 343 | int delta = Integer.valueOf(split[1]);
|
| 344 | + if (delta <= 0) { |
| 345 | + throw new IllegalArgumentException("Incrementer delta must be 1 or higher: '" + |
| 346 | + field + "' in expression \"" + this.expression + "\""); |
| 347 | + } |
335 | 348 | for (int i = range[0]; i <= range[1]; i += delta) {
|
336 | 349 | bits.set(i);
|
337 | 350 | }
|
@@ -369,30 +382,30 @@ private int[] getRange(String field, int min, int max) {
|
369 | 382 | return result;
|
370 | 383 | }
|
371 | 384 |
|
372 |
| - String getExpression() { |
373 |
| - return this.expression; |
374 |
| - } |
375 | 385 |
|
376 | 386 | @Override
|
377 |
| - public boolean equals(Object obj) { |
378 |
| - if (!(obj instanceof CronSequenceGenerator)) { |
| 387 | + public boolean equals(Object other) { |
| 388 | + if (this == other) { |
| 389 | + return true; |
| 390 | + } |
| 391 | + if (!(other instanceof CronSequenceGenerator)) { |
379 | 392 | return false;
|
380 | 393 | }
|
381 |
| - CronSequenceGenerator cron = (CronSequenceGenerator) obj; |
382 |
| - return cron.months.equals(this.months) && cron.daysOfMonth.equals(this.daysOfMonth) |
383 |
| - && cron.daysOfWeek.equals(this.daysOfWeek) && cron.hours.equals(this.hours) |
384 |
| - && cron.minutes.equals(this.minutes) && cron.seconds.equals(this.seconds); |
| 394 | + CronSequenceGenerator otherCron = (CronSequenceGenerator) other; |
| 395 | + return (this.months.equals(otherCron.months) && this.daysOfMonth.equals(otherCron.daysOfMonth) && |
| 396 | + this.daysOfWeek.equals(otherCron.daysOfWeek) && this.hours.equals(otherCron.hours) && |
| 397 | + this.minutes.equals(otherCron.minutes) && this.seconds.equals(otherCron.seconds)); |
385 | 398 | }
|
386 | 399 |
|
387 | 400 | @Override
|
388 | 401 | public int hashCode() {
|
389 |
| - return 37 + 17 * this.months.hashCode() + 29 * this.daysOfMonth.hashCode() + 37 * this.daysOfWeek.hashCode() |
390 |
| - + 41 * this.hours.hashCode() + 53 * this.minutes.hashCode() + 61 * this.seconds.hashCode(); |
| 402 | + return (17 * this.months.hashCode() + 29 * this.daysOfMonth.hashCode() + 37 * this.daysOfWeek.hashCode() + |
| 403 | + 41 * this.hours.hashCode() + 53 * this.minutes.hashCode() + 61 * this.seconds.hashCode()); |
391 | 404 | }
|
392 | 405 |
|
393 | 406 | @Override
|
394 | 407 | public String toString() {
|
395 |
| - return getClass().getSimpleName() + ": " + this.expression; |
| 408 | + return (getClass().getSimpleName() + ": " + this.expression); |
396 | 409 | }
|
397 | 410 |
|
398 | 411 | }
|
0 commit comments