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;
018
019import com.google.common.annotations.GwtIncompatible;
020import com.google.common.annotations.J2ktIncompatible;
021import java.time.Duration;
022import java.util.concurrent.Callable;
023import java.util.concurrent.ScheduledExecutorService;
024import java.util.concurrent.TimeUnit;
025import org.jspecify.annotations.Nullable;
026
027/**
028 * A {@link ScheduledExecutorService} that returns {@link ListenableFuture} instances from its
029 * {@code ExecutorService} methods. To create an instance from an existing {@link
030 * ScheduledExecutorService}, call {@link
031 * MoreExecutors#listeningDecorator(ScheduledExecutorService)}.
032 *
033 * @author Chris Povirk
034 * @since 10.0
035 */
036@GwtIncompatible
037public interface ListeningScheduledExecutorService
038    extends ScheduledExecutorService, ListeningExecutorService {
039
040  /**
041   * @since 15.0 (previously returned ScheduledFuture)
042   */
043  @Override
044  ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
045
046  /**
047   * Duration-based overload of {@link #schedule(Runnable, long, TimeUnit)}.
048   *
049   * @since 29.0
050   */
051  @J2ktIncompatible
052  default ListenableScheduledFuture<?> schedule(Runnable command, Duration delay) {
053    return schedule(command, toNanosSaturated(delay), TimeUnit.NANOSECONDS);
054  }
055
056  /**
057   * @since 15.0 (previously returned ScheduledFuture)
058   */
059  @Override
060  <V extends @Nullable Object> ListenableScheduledFuture<V> schedule(
061      Callable<V> callable, long delay, TimeUnit unit);
062
063  /**
064   * Duration-based overload of {@link #schedule(Callable, long, TimeUnit)}.
065   *
066   * @since 29.0
067   */
068  @J2ktIncompatible
069  default <V extends @Nullable Object> ListenableScheduledFuture<V> schedule(
070      Callable<V> callable, Duration delay) {
071    return schedule(callable, toNanosSaturated(delay), TimeUnit.NANOSECONDS);
072  }
073
074  /**
075   * @since 15.0 (previously returned ScheduledFuture)
076   */
077  @Override
078  ListenableScheduledFuture<?> scheduleAtFixedRate(
079      Runnable command, long initialDelay, long period, TimeUnit unit);
080
081  /**
082   * Duration-based overload of {@link #scheduleAtFixedRate(Runnable, long, long, TimeUnit)}.
083   *
084   * @since 29.0
085   */
086  @J2ktIncompatible
087  default ListenableScheduledFuture<?> scheduleAtFixedRate(
088      Runnable command, Duration initialDelay, Duration period) {
089    return scheduleAtFixedRate(
090        command, toNanosSaturated(initialDelay), toNanosSaturated(period), TimeUnit.NANOSECONDS);
091  }
092
093  /**
094   * @since 15.0 (previously returned ScheduledFuture)
095   */
096  @Override
097  ListenableScheduledFuture<?> scheduleWithFixedDelay(
098      Runnable command, long initialDelay, long delay, TimeUnit unit);
099
100  /**
101   * Duration-based overload of {@link #scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}.
102   *
103   * @since 29.0
104   */
105  @J2ktIncompatible
106  default ListenableScheduledFuture<?> scheduleWithFixedDelay(
107      Runnable command, Duration initialDelay, Duration delay) {
108    return scheduleWithFixedDelay(
109        command, toNanosSaturated(initialDelay), toNanosSaturated(delay), TimeUnit.NANOSECONDS);
110  }
111}