Skip to content

Commit

Permalink
Expose more Java 8 APIs to Android users.
Browse files Browse the repository at this point in the history
RELNOTES=Exposed some additional Java 8 APIs to Android users.
PiperOrigin-RevId: 689811921
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 25, 2024
1 parent 309365e commit 984f713
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
import static com.google.common.util.concurrent.Internal.toNanosSaturated;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.NANOSECONDS;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import com.google.j2objc.annotations.WeakOuter;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -116,6 +119,22 @@ public abstract class AbstractScheduledService implements Service {
* @since 11.0
*/
public abstract static class Scheduler {
/**
* Returns a {@link Scheduler} that schedules the task using the {@link
* ScheduledExecutorService#scheduleWithFixedDelay} method.
*
* @param initialDelay the time to delay first execution
* @param delay the delay between the termination of one execution and the commencement of the
* next
* @since NEXT (but since 28.0 in the JRE flavor)
*/
@SuppressWarnings("Java7ApiChecker")
@IgnoreJRERequirement // Users will use this only if they're already using Duration
public static Scheduler newFixedDelaySchedule(Duration initialDelay, Duration delay) {
return newFixedDelaySchedule(
toNanosSaturated(initialDelay), toNanosSaturated(delay), NANOSECONDS);
}

/**
* Returns a {@link Scheduler} that schedules the task using the {@link
* ScheduledExecutorService#scheduleWithFixedDelay} method.
Expand All @@ -140,6 +159,21 @@ public Cancellable schedule(
};
}

/**
* Returns a {@link Scheduler} that schedules the task using the {@link
* ScheduledExecutorService#scheduleAtFixedRate} method.
*
* @param initialDelay the time to delay first execution
* @param period the period between successive executions of the task
* @since NEXT (but since 28.0 in the JRE flavor)
*/
@SuppressWarnings("Java7ApiChecker")
@IgnoreJRERequirement // Users will use this only if they're already using Duration
public static Scheduler newFixedRateSchedule(Duration initialDelay, Duration period) {
return newFixedRateSchedule(
toNanosSaturated(initialDelay), toNanosSaturated(period), NANOSECONDS);
}

/**
* Returns a {@link Scheduler} that schedules the task using the {@link
* ScheduledExecutorService#scheduleAtFixedRate} method.
Expand Down Expand Up @@ -685,6 +719,16 @@ public Schedule(long delay, TimeUnit unit) {
this.delay = delay;
this.unit = checkNotNull(unit);
}

/**
* @param delay the time from now to delay execution
* @since NEXT (but since 31.1 in the JRE flavor)
*/
@SuppressWarnings("Java7ApiChecker")
@IgnoreJRERequirement // Users will use this only if they're already using Duration
public Schedule(Duration delay) {
this(toNanosSaturated(delay), NANOSECONDS);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public abstract static class Scheduler {
* @param initialDelay the time to delay first execution
* @param delay the delay between the termination of one execution and the commencement of the
* next
* @since 28.0
* @since 28.0 (but only since 33.4.0 in the Android flavor)
*/
public static Scheduler newFixedDelaySchedule(Duration initialDelay, Duration delay) {
return newFixedDelaySchedule(
Expand Down Expand Up @@ -163,7 +163,7 @@ public Cancellable schedule(
*
* @param initialDelay the time to delay first execution
* @param period the period between successive executions of the task
* @since 28.0
* @since 28.0 (but only since 33.4.0 in the Android flavor)
*/
public static Scheduler newFixedRateSchedule(Duration initialDelay, Duration period) {
return newFixedRateSchedule(
Expand Down Expand Up @@ -730,7 +730,7 @@ public Schedule(long delay, TimeUnit unit) {

/**
* @param delay the time from now to delay execution
* @since 31.1
* @since 31.1 (but only since 33.4.0 in the Android flavor)
*/
public Schedule(Duration delay) {
this(toNanosSaturated(delay), NANOSECONDS);
Expand Down

0 comments on commit 984f713

Please sign in to comment.