|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.scheduling.support; |
| 18 | + |
| 19 | +import java.time.DateTimeException; |
| 20 | +import java.time.temporal.Temporal; |
| 21 | +import java.time.temporal.ValueRange; |
| 22 | +import java.util.BitSet; |
| 23 | + |
| 24 | +import org.springframework.lang.Nullable; |
| 25 | +import org.springframework.util.Assert; |
| 26 | +import org.springframework.util.StringUtils; |
| 27 | + |
| 28 | +/** |
| 29 | + * Efficient {@link BitSet}-based extension of {@link CronField}. |
| 30 | + * Created using the {@code parse*} methods. |
| 31 | + * |
| 32 | + * @author Arjen Poutsma |
| 33 | + * @since 5.3 |
| 34 | + */ |
| 35 | +final class BitsCronField extends CronField { |
| 36 | + |
| 37 | + private static final BitsCronField ZERO_NANOS; |
| 38 | + |
| 39 | + |
| 40 | + static { |
| 41 | + ZERO_NANOS = new BitsCronField(Type.NANO); |
| 42 | + ZERO_NANOS.bits.set(0); |
| 43 | + } |
| 44 | + |
| 45 | + private final BitSet bits; |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + private BitsCronField(Type type) { |
| 50 | + super(type); |
| 51 | + this.bits = new BitSet((int) type.range().getMaximum()); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Return a {@code BitsCronField} enabled for 0 nano seconds. |
| 56 | + */ |
| 57 | + public static BitsCronField zeroNanos() { |
| 58 | + return BitsCronField.ZERO_NANOS; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Parse the given value into a seconds {@code BitsCronField}, the first entry of a cron expression. |
| 63 | + */ |
| 64 | + public static BitsCronField parseSeconds(String value) { |
| 65 | + return parseField(value, Type.SECOND); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Parse the given value into a minutes {@code BitsCronField}, the second entry of a cron expression. |
| 70 | + */ |
| 71 | + public static BitsCronField parseMinutes(String value) { |
| 72 | + return BitsCronField.parseField(value, Type.MINUTE); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parse the given value into a hours {@code BitsCronField}, the third entry of a cron expression. |
| 77 | + */ |
| 78 | + public static BitsCronField parseHours(String value) { |
| 79 | + return BitsCronField.parseField(value, Type.HOUR); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Parse the given value into a days of months {@code BitsCronField}, the fourth entry of a cron expression. |
| 84 | + */ |
| 85 | + public static BitsCronField parseDaysOfMonth(String value) { |
| 86 | + return parseDate(value, Type.DAY_OF_MONTH); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Parse the given value into a month {@code BitsCronField}, the fifth entry of a cron expression. |
| 91 | + */ |
| 92 | + public static BitsCronField parseMonth(String value) { |
| 93 | + return BitsCronField.parseField(value, Type.MONTH); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Parse the given value into a days of week {@code BitsCronField}, the sixth entry of a cron expression. |
| 98 | + */ |
| 99 | + public static BitsCronField parseDaysOfWeek(String value) { |
| 100 | + BitsCronField result = parseDate(value, Type.DAY_OF_WEEK); |
| 101 | + BitSet bits = result.bits; |
| 102 | + if (bits.get(0)) { |
| 103 | + // cron supports 0 for Sunday; we use 7 like java.time |
| 104 | + bits.set(7); |
| 105 | + bits.clear(0); |
| 106 | + } |
| 107 | + return result; |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + private static BitsCronField parseDate(String value, BitsCronField.Type type) { |
| 112 | + if (value.indexOf('?') != -1) { |
| 113 | + value = "*"; |
| 114 | + } |
| 115 | + return BitsCronField.parseField(value, type); |
| 116 | + } |
| 117 | + |
| 118 | + private static BitsCronField parseField(String value, Type type) { |
| 119 | + Assert.hasLength(value, "Value must not be empty"); |
| 120 | + Assert.notNull(type, "Type must not be null"); |
| 121 | + try { |
| 122 | + BitsCronField result = new BitsCronField(type); |
| 123 | + String[] fields = StringUtils.delimitedListToStringArray(value, ","); |
| 124 | + for (String field : fields) { |
| 125 | + int slashPos = field.indexOf('/'); |
| 126 | + if (slashPos == -1) { |
| 127 | + ValueRange range = parseRange(field, type); |
| 128 | + result.setBits(range); |
| 129 | + } |
| 130 | + else { |
| 131 | + String rangeStr = value.substring(0, slashPos); |
| 132 | + String deltaStr = value.substring(slashPos + 1); |
| 133 | + ValueRange range = parseRange(rangeStr, type); |
| 134 | + if (rangeStr.indexOf('-') == -1) { |
| 135 | + range = ValueRange.of(range.getMinimum(), type.range().getMaximum()); |
| 136 | + } |
| 137 | + int delta = Integer.parseInt(deltaStr); |
| 138 | + if (delta <= 0) { |
| 139 | + throw new IllegalArgumentException("Incrementer delta must be 1 or higher"); |
| 140 | + } |
| 141 | + result.setBits(range, delta); |
| 142 | + } |
| 143 | + } |
| 144 | + return result; |
| 145 | + } |
| 146 | + catch (DateTimeException | IllegalArgumentException ex) { |
| 147 | + String msg = ex.getMessage() + " '" + value + "'"; |
| 148 | + throw new IllegalArgumentException(msg, ex); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private static ValueRange parseRange(String value, Type type) { |
| 153 | + if (value.indexOf('*') != -1) { |
| 154 | + return type.range(); |
| 155 | + } |
| 156 | + else { |
| 157 | + int hyphenPos = value.indexOf('-'); |
| 158 | + if (hyphenPos == -1) { |
| 159 | + int result = type.checkValidValue(Integer.parseInt(value)); |
| 160 | + return ValueRange.of(result, result); |
| 161 | + } |
| 162 | + else { |
| 163 | + int min = Integer.parseInt(value.substring(0, hyphenPos)); |
| 164 | + int max = Integer.parseInt(value.substring(hyphenPos + 1)); |
| 165 | + min = type.checkValidValue(min); |
| 166 | + max = type.checkValidValue(max); |
| 167 | + return ValueRange.of(min, max); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @Nullable |
| 173 | + @Override |
| 174 | + public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) { |
| 175 | + int current = type().get(temporal); |
| 176 | + int next = this.bits.nextSetBit(current); |
| 177 | + if (next == -1) { |
| 178 | + temporal = type().rollForward(temporal); |
| 179 | + next = this.bits.nextSetBit(0); |
| 180 | + } |
| 181 | + if (next == current) { |
| 182 | + return temporal; |
| 183 | + } |
| 184 | + else { |
| 185 | + int count = 0; |
| 186 | + current = type().get(temporal); |
| 187 | + while (current != next && count++ < CronExpression.MAX_ATTEMPTS) { |
| 188 | + temporal = type().elapseUntil(temporal, next); |
| 189 | + current = type().get(temporal); |
| 190 | + } |
| 191 | + if (count >= CronExpression.MAX_ATTEMPTS) { |
| 192 | + return null; |
| 193 | + } |
| 194 | + return type().reset(temporal); |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + BitSet bits() { |
| 199 | + return this.bits; |
| 200 | + } |
| 201 | + |
| 202 | + private void setBits(ValueRange range) { |
| 203 | + this.bits.set((int) range.getMinimum(), (int) range.getMaximum() + 1); |
| 204 | + } |
| 205 | + |
| 206 | + private void setBits(ValueRange range, int delta) { |
| 207 | + for (int i = (int) range.getMinimum(); i <= range.getMaximum(); i += delta) { |
| 208 | + this.bits.set(i); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public int hashCode() { |
| 214 | + return this.bits.hashCode(); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public boolean equals(Object o) { |
| 219 | + if (this == o) { |
| 220 | + return true; |
| 221 | + } |
| 222 | + if (!(o instanceof BitsCronField)) { |
| 223 | + return false; |
| 224 | + } |
| 225 | + BitsCronField other = (BitsCronField) o; |
| 226 | + return type() == other.type() && |
| 227 | + this.bits.equals(other.bits); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public String toString() { |
| 232 | + return type() + " " + this.bits; |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments