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