001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import static com.google.common.util.concurrent.Internal.toNanosSaturated;
018import static java.util.concurrent.TimeUnit.NANOSECONDS;
019
020import com.google.common.annotations.GwtIncompatible;
021import com.google.common.annotations.J2ktIncompatible;
022import java.time.Duration;
023import java.util.concurrent.Callable;
024import java.util.concurrent.ScheduledExecutorService;
025import java.util.concurrent.TimeUnit;
026import org.jspecify.annotations.Nullable;
027
028/**
029 * A {@link ScheduledExecutorService} that returns {@link ListenableFuture} instances from its
030 * {@code ExecutorService} methods. To create an instance from an existing {@link
031 * ScheduledExecutorService}, call {@link
032 * MoreExecutors#listeningDecorator(ScheduledExecutorService)}.
033 *
034 * @author Chris Povirk
035 * @since 10.0
036 */
037@GwtIncompatible
038public interface ListeningScheduledExecutorService
039    extends ScheduledExecutorService, ListeningExecutorService {
040
041  /**
042   * @since 15.0 (previously returned ScheduledFuture)
043   */
044  @Override
045  ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
046
047  /**
048   * Duration-based overload of {@link #schedule(Runnable, long, TimeUnit)}.
049   *
050   * @since 29.0
051   */
052  @J2ktIncompatible
053  default ListenableScheduledFuture<?> schedule(Runnable command, Duration delay) {
054    return schedule(command, toNanosSaturated(delay), NANOSECONDS);
055  }
056
057  /**
058   * @since 15.0 (previously returned ScheduledFuture)
059   */
060  @Override
061  <V extends @Nullable Object> ListenableScheduledFuture<V> schedule(
062      Callable<V> callable, long delay, TimeUnit unit);
063
064  /**
065   * Duration-based overload of {@link #schedule(Callable, long, TimeUnit)}.
066   *
067   * @since 29.0
068   */
069  @J2ktIncompatible
070  default <V extends @Nullable Object> ListenableScheduledFuture<V> schedule(
071      Callable<V> callable, Duration delay) {
072    return schedule(callable, toNanosSaturated(delay), NANOSECONDS);
073  }
074
075  /**
076   * @since 15.0 (previously returned ScheduledFuture)
077   */
078  @Override
079  ListenableScheduledFuture<?> scheduleAtFixedRate(
080      Runnable command, long initialDelay, long period, TimeUnit unit);
081
082  /**
083   * Duration-based overload of {@link #scheduleAtFixedRate(Runnable, long, long, TimeUnit)}.
084   *
085   * @since 29.0
086   */
087  @J2ktIncompatible
088  default ListenableScheduledFuture<?> scheduleAtFixedRate(
089      Runnable command, Duration initialDelay, Duration period) {
090    return scheduleAtFixedRate(
091        command, toNanosSaturated(initialDelay), toNanosSaturated(period), NANOSECONDS);
092  }
093
094  /**
095   * @since 15.0 (previously returned ScheduledFuture)
096   */
097  @Override
098  ListenableScheduledFuture<?> scheduleWithFixedDelay(
099      Runnable command, long initialDelay, long delay, TimeUnit unit);
100
101  /**
102   * Duration-based overload of {@link #scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}.
103   *
104   * @since 29.0
105   */
106  @J2ktIncompatible
107  default ListenableScheduledFuture<?> scheduleWithFixedDelay(
108      Runnable command, Duration initialDelay, Duration delay) {
109    return scheduleWithFixedDelay(
110        command, toNanosSaturated(initialDelay), toNanosSaturated(delay), NANOSECONDS);
111  }
112}