1717import static com .google .common .base .Preconditions .checkArgument ;
1818import static com .google .common .base .Preconditions .checkNotNull ;
1919import static com .google .common .util .concurrent .Futures .immediateCancelledFuture ;
20+ import static com .google .common .util .concurrent .Internal .toNanosSaturated ;
2021import static com .google .common .util .concurrent .MoreExecutors .directExecutor ;
2122import static com .google .common .util .concurrent .Platform .restoreInterruptIfIsInterruptedException ;
2223import static java .util .Objects .requireNonNull ;
24+ import static java .util .concurrent .TimeUnit .NANOSECONDS ;
2325
2426import com .google .common .annotations .GwtIncompatible ;
2527import com .google .common .annotations .J2ktIncompatible ;
2628import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2729import com .google .errorprone .annotations .concurrent .GuardedBy ;
2830import com .google .j2objc .annotations .WeakOuter ;
31+ import java .time .Duration ;
2932import java .util .concurrent .Callable ;
3033import java .util .concurrent .Executor ;
3134import java .util .concurrent .Executors ;
@@ -116,6 +119,22 @@ public abstract class AbstractScheduledService implements Service {
116119 * @since 11.0
117120 */
118121 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+
119138 /**
120139 * Returns a {@link Scheduler} that schedules the task using the {@link
121140 * ScheduledExecutorService#scheduleWithFixedDelay} method.
@@ -140,6 +159,21 @@ public Cancellable schedule(
140159 };
141160 }
142161
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+
143177 /**
144178 * Returns a {@link Scheduler} that schedules the task using the {@link
145179 * ScheduledExecutorService#scheduleAtFixedRate} method.
@@ -685,6 +719,16 @@ public Schedule(long delay, TimeUnit unit) {
685719 this .delay = delay ;
686720 this .unit = checkNotNull (unit );
687721 }
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+ }
688732 }
689733
690734 /**
0 commit comments