Skip to content

Commit bfd3b3a

Browse files
committed
Eagerly initialize ZERO_NANOS constant
1 parent 0909161 commit bfd3b3a

File tree

3 files changed

+19
-30
lines changed

3 files changed

+19
-30
lines changed

spring-context/src/main/java/org/springframework/scheduling/support/BitsCronField.java

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,12 +33,9 @@
3333
*/
3434
final class BitsCronField extends CronField {
3535

36-
private static final long MASK = 0xFFFFFFFFFFFFFFFFL;
37-
38-
39-
@Nullable
40-
private static BitsCronField zeroNanos = null;
36+
public static BitsCronField ZERO_NANOS = forZeroNanos();
4137

38+
private static final long MASK = 0xFFFFFFFFFFFFFFFFL;
4239

4340
// we store at most 60 bits, for seconds and minutes, so a 64-bit long suffices
4441
private long bits;
@@ -48,16 +45,14 @@ private BitsCronField(Type type) {
4845
super(type);
4946
}
5047

48+
5149
/**
5250
* Return a {@code BitsCronField} enabled for 0 nanoseconds.
5351
*/
54-
public static BitsCronField zeroNanos() {
55-
if (zeroNanos == null) {
56-
BitsCronField field = new BitsCronField(Type.NANO);
57-
field.setBit(0);
58-
zeroNanos = field;
59-
}
60-
return zeroNanos;
52+
private static BitsCronField forZeroNanos() {
53+
BitsCronField field = new BitsCronField(Type.NANO);
54+
field.setBit(0);
55+
return field;
6156
}
6257

6358
/**
@@ -108,7 +103,6 @@ public static BitsCronField parseDaysOfWeek(String value) {
108103
return result;
109104
}
110105

111-
112106
private static BitsCronField parseDate(String value, BitsCronField.Type type) {
113107
if (value.equals("?")) {
114108
value = "*";
@@ -174,6 +168,7 @@ private static ValueRange parseRange(String value, Type type) {
174168
}
175169
}
176170

171+
177172
@Nullable
178173
@Override
179174
public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
@@ -217,7 +212,6 @@ private int nextSetBit(int fromIndex) {
217212
else {
218213
return -1;
219214
}
220-
221215
}
222216

223217
private void setBits(ValueRange range) {
@@ -247,23 +241,19 @@ private void setBit(int index) {
247241
}
248242

249243
private void clearBit(int index) {
250-
this.bits &= ~(1L << index);
244+
this.bits &= ~(1L << index);
251245
}
252246

247+
253248
@Override
254-
public int hashCode() {
255-
return Long.hashCode(this.bits);
249+
public boolean equals(Object other) {
250+
return (this == other || (other instanceof BitsCronField that &&
251+
type() == that.type() && this.bits == that.bits));
256252
}
257253

258254
@Override
259-
public boolean equals(@Nullable Object o) {
260-
if (this == o) {
261-
return true;
262-
}
263-
if (!(o instanceof BitsCronField other)) {
264-
return false;
265-
}
266-
return type() == other.type() && this.bits == other.bits;
255+
public int hashCode() {
256+
return Long.hashCode(this.bits);
267257
}
268258

269259
@Override

spring-context/src/main/java/org/springframework/scheduling/support/CronField.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
/**
3131
* Single field in a cron pattern. Created using the {@code parse*} methods,
32-
* main and only entry point is {@link #nextOrSame(Temporal)}.
32+
* the main and only entry point is {@link #nextOrSame(Temporal)}.
3333
*
3434
* <p>Supports a Quartz day-of-month/week field with an L/# expression. Follows
3535
* common cron conventions in every other respect, including 0-6 for SUN-SAT
@@ -60,7 +60,7 @@ protected CronField(Type type) {
6060
* Return a {@code CronField} enabled for 0 nanoseconds.
6161
*/
6262
public static CronField zeroNanos() {
63-
return BitsCronField.zeroNanos();
63+
return BitsCronField.ZERO_NANOS;
6464
}
6565

6666
/**
@@ -186,7 +186,6 @@ protected enum Type {
186186
MONTH(ChronoField.MONTH_OF_YEAR, ChronoUnit.YEARS, ChronoField.DAY_OF_MONTH, ChronoField.HOUR_OF_DAY, ChronoField.MINUTE_OF_HOUR, ChronoField.SECOND_OF_MINUTE, ChronoField.NANO_OF_SECOND),
187187
DAY_OF_WEEK(ChronoField.DAY_OF_WEEK, ChronoUnit.WEEKS, ChronoField.HOUR_OF_DAY, ChronoField.MINUTE_OF_HOUR, ChronoField.SECOND_OF_MINUTE, ChronoField.NANO_OF_SECOND);
188188

189-
190189
private final ChronoField field;
191190

192191
private final ChronoUnit higherOrder;

spring-context/src/main/java/org/springframework/scheduling/support/QuartzCronField.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ private static Temporal rollbackToMidnight(Temporal current, Temporal result) {
336336
}
337337
}
338338

339+
339340
@Override
340341
public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
341342
T result = adjust(temporal);
@@ -352,7 +353,6 @@ public <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal) {
352353
return result;
353354
}
354355

355-
356356
@Nullable
357357
@SuppressWarnings("unchecked")
358358
private <T extends Temporal & Comparable<? super T>> T adjust(T temporal) {

0 commit comments

Comments
 (0)