17
17
import static com .google .common .base .Preconditions .checkArgument ;
18
18
import static com .google .common .base .Preconditions .checkNotNull ;
19
19
import static com .google .common .util .concurrent .Futures .immediateCancelledFuture ;
20
+ import static com .google .common .util .concurrent .Internal .toNanosSaturated ;
20
21
import static com .google .common .util .concurrent .MoreExecutors .directExecutor ;
21
22
import static com .google .common .util .concurrent .Platform .restoreInterruptIfIsInterruptedException ;
22
23
import static java .util .Objects .requireNonNull ;
24
+ import static java .util .concurrent .TimeUnit .NANOSECONDS ;
23
25
24
26
import com .google .common .annotations .GwtIncompatible ;
25
27
import com .google .common .annotations .J2ktIncompatible ;
26
28
import com .google .errorprone .annotations .CanIgnoreReturnValue ;
27
29
import com .google .errorprone .annotations .concurrent .GuardedBy ;
28
30
import com .google .j2objc .annotations .WeakOuter ;
31
+ import java .time .Duration ;
29
32
import java .util .concurrent .Callable ;
30
33
import java .util .concurrent .Executor ;
31
34
import java .util .concurrent .Executors ;
@@ -116,6 +119,22 @@ public abstract class AbstractScheduledService implements Service {
116
119
* @since 11.0
117
120
*/
118
121
public abstract static class Scheduler {
122
+ /**
123
+ * Returns a {@link Scheduler} that schedules the task using the {@link
124
+ * ScheduledExecutorService#scheduleWithFixedDelay} method.
125
+ *
126
+ * @param initialDelay the time to delay first execution
127
+ * @param delay the delay between the termination of one execution and the commencement of the
128
+ * next
129
+ * @since NEXT (but since 28.0 in the JRE flavor)
130
+ */
131
+ @ SuppressWarnings ("Java7ApiChecker" )
132
+ @ IgnoreJRERequirement // Users will use this only if they're already using Duration
133
+ public static Scheduler newFixedDelaySchedule (Duration initialDelay , Duration delay ) {
134
+ return newFixedDelaySchedule (
135
+ toNanosSaturated (initialDelay ), toNanosSaturated (delay ), NANOSECONDS );
136
+ }
137
+
119
138
/**
120
139
* Returns a {@link Scheduler} that schedules the task using the {@link
121
140
* ScheduledExecutorService#scheduleWithFixedDelay} method.
@@ -140,6 +159,21 @@ public Cancellable schedule(
140
159
};
141
160
}
142
161
162
+ /**
163
+ * Returns a {@link Scheduler} that schedules the task using the {@link
164
+ * ScheduledExecutorService#scheduleAtFixedRate} method.
165
+ *
166
+ * @param initialDelay the time to delay first execution
167
+ * @param period the period between successive executions of the task
168
+ * @since NEXT (but since 28.0 in the JRE flavor)
169
+ */
170
+ @ SuppressWarnings ("Java7ApiChecker" )
171
+ @ IgnoreJRERequirement // Users will use this only if they're already using Duration
172
+ public static Scheduler newFixedRateSchedule (Duration initialDelay , Duration period ) {
173
+ return newFixedRateSchedule (
174
+ toNanosSaturated (initialDelay ), toNanosSaturated (period ), NANOSECONDS );
175
+ }
176
+
143
177
/**
144
178
* Returns a {@link Scheduler} that schedules the task using the {@link
145
179
* ScheduledExecutorService#scheduleAtFixedRate} method.
@@ -685,6 +719,16 @@ public Schedule(long delay, TimeUnit unit) {
685
719
this .delay = delay ;
686
720
this .unit = checkNotNull (unit );
687
721
}
722
+
723
+ /**
724
+ * @param delay the time from now to delay execution
725
+ * @since NEXT (but since 31.1 in the JRE flavor)
726
+ */
727
+ @ SuppressWarnings ("Java7ApiChecker" )
728
+ @ IgnoreJRERequirement // Users will use this only if they're already using Duration
729
+ public Schedule (Duration delay ) {
730
+ this (toNanosSaturated (delay ), NANOSECONDS );
731
+ }
688
732
}
689
733
690
734
/**
0 commit comments