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