diff --git a/android/guava/src/com/google/common/collect/Queues.java b/android/guava/src/com/google/common/collect/Queues.java index 8daaba1a1039..9861eeaa1752 100644 --- a/android/guava/src/com/google/common/collect/Queues.java +++ b/android/guava/src/com/google/common/collect/Queues.java @@ -21,6 +21,7 @@ import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Preconditions; import com.google.errorprone.annotations.CanIgnoreReturnValue; +import java.time.Duration; import java.util.ArrayDeque; import java.util.Collection; import java.util.Deque; @@ -276,6 +277,30 @@ public static SynchronousQueue newSynchronousQueue() { return new SynchronousQueue<>(); } + /** + * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested {@code + * numElements} elements are not available, it will wait for them up to the specified timeout. + * + * @param q the blocking queue to be drained + * @param buffer where to add the transferred elements + * @param numElements the number of elements to be waited for + * @param timeout how long to wait before giving up + * @return the number of elements transferred + * @throws InterruptedException if interrupted while waiting + * @since NEXT (but since 28.0 in the JRE flavor) + */ + @CanIgnoreReturnValue + @J2ktIncompatible + @GwtIncompatible // BlockingQueue + @SuppressWarnings("Java7ApiChecker") + @IgnoreJRERequirement // Users will use this only if they're already using Duration + public static int drain( + BlockingQueue q, Collection buffer, int numElements, Duration timeout) + throws InterruptedException { + // TODO(b/126049426): Consider using saturateToNanos(timeout) instead. + return drain(q, buffer, numElements, timeout.toNanos(), NANOSECONDS); + } + /** * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested {@code * numElements} elements are not available, it will wait for them up to the specified timeout. @@ -323,6 +348,30 @@ public static int drain( return added; } + /** + * Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, Duration)}, but with a + * different behavior in case it is interrupted while waiting. In that case, the operation will + * continue as usual, and in the end the thread's interruption status will be set (no {@code + * InterruptedException} is thrown). + * + * @param q the blocking queue to be drained + * @param buffer where to add the transferred elements + * @param numElements the number of elements to be waited for + * @param timeout how long to wait before giving up + * @return the number of elements transferred + * @since NEXT (but since 28.0 in the JRE flavor) + */ + @CanIgnoreReturnValue + @J2ktIncompatible + @GwtIncompatible // BlockingQueue + @SuppressWarnings("Java7ApiChecker") + @IgnoreJRERequirement // Users will use this only if they're already using Duration + public static int drainUninterruptibly( + BlockingQueue q, Collection buffer, int numElements, Duration timeout) { + // TODO(b/126049426): Consider using saturateToNanos(timeout) instead. + return drainUninterruptibly(q, buffer, numElements, timeout.toNanos(), NANOSECONDS); + } + /** * Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, long, TimeUnit)}, but * with a different behavior in case it is interrupted while waiting. In that case, the operation diff --git a/guava/src/com/google/common/collect/Queues.java b/guava/src/com/google/common/collect/Queues.java index 3ac6e86df3f0..7c4af7df9d50 100644 --- a/guava/src/com/google/common/collect/Queues.java +++ b/guava/src/com/google/common/collect/Queues.java @@ -287,7 +287,7 @@ public static SynchronousQueue newSynchronousQueue() { * @param timeout how long to wait before giving up * @return the number of elements transferred * @throws InterruptedException if interrupted while waiting - * @since 28.0 + * @since 28.0 (but only since 33.4.0 in the Android flavor) */ @CanIgnoreReturnValue @J2ktIncompatible @@ -357,7 +357,7 @@ public static int drain( * @param numElements the number of elements to be waited for * @param timeout how long to wait before giving up * @return the number of elements transferred - * @since 28.0 + * @since 28.0 (but only since 33.4.0 in the Android flavor) */ @CanIgnoreReturnValue @J2ktIncompatible