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.checkerframework.checker.nullness.qual.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@J2ktIncompatible
037@GwtIncompatible
038@ElementTypesAreNonnullByDefault
039public interface ListeningScheduledExecutorService
040    extends ScheduledExecutorService, ListeningExecutorService {
041
042  /** @since 15.0 (previously returned ScheduledFuture) */
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  default ListenableScheduledFuture<?> schedule(Runnable command, Duration delay) {
052    return schedule(command, toNanosSaturated(delay), TimeUnit.NANOSECONDS);
053  }
054
055  /** @since 15.0 (previously returned ScheduledFuture) */
056  @Override
057  <V extends @Nullable Object> ListenableScheduledFuture<V> schedule(
058      Callable<V> callable, long delay, TimeUnit unit);
059
060  /**
061   * Duration-based overload of {@link #schedule(Callable, long, TimeUnit)}.
062   *
063   * @since 29.0
064   */
065  default <V extends @Nullable Object> ListenableScheduledFuture<V> schedule(
066      Callable<V> callable, Duration delay) {
067    return schedule(callable, toNanosSaturated(delay), TimeUnit.NANOSECONDS);
068  }
069
070  /** @since 15.0 (previously returned ScheduledFuture) */
071  @Override
072  ListenableScheduledFuture<?> scheduleAtFixedRate(
073      Runnable command, long initialDelay, long period, TimeUnit unit);
074
075  /**
076   * Duration-based overload of {@link #scheduleAtFixedRate(Runnable, long, long, TimeUnit)}.
077   *
078   * @since 29.0
079   */
080  default ListenableScheduledFuture<?> scheduleAtFixedRate(
081      Runnable command, Duration initialDelay, Duration period) {
082    return scheduleAtFixedRate(
083        command, toNanosSaturated(initialDelay), toNanosSaturated(period), TimeUnit.NANOSECONDS);
084  }
085
086  /** @since 15.0 (previously returned ScheduledFuture) */
087  @Override
088  ListenableScheduledFuture<?> scheduleWithFixedDelay(
089      Runnable command, long initialDelay, long delay, TimeUnit unit);
090
091  /**
092   * Duration-based overload of {@link #scheduleWithFixedDelay(Runnable, long, long, TimeUnit)}.
093   *
094   * @since 29.0
095   */
096  default ListenableScheduledFuture<?> scheduleWithFixedDelay(
097      Runnable command, Duration initialDelay, Duration delay) {
098    return scheduleWithFixedDelay(
099        command, toNanosSaturated(initialDelay), toNanosSaturated(delay), TimeUnit.NANOSECONDS);
100  }
101}