Skip to content

Commit b650b9f

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Expose more Java 8 APIs to Android users.
RELNOTES=Exposed some additional Java 8 APIs to Android users. PiperOrigin-RevId: 690111120
1 parent 2f04514 commit b650b9f

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

android/guava/src/com/google/common/collect/Queues.java

+49
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.annotations.J2ktIncompatible;
2222
import com.google.common.base.Preconditions;
2323
import com.google.errorprone.annotations.CanIgnoreReturnValue;
24+
import java.time.Duration;
2425
import java.util.ArrayDeque;
2526
import java.util.Collection;
2627
import java.util.Deque;
@@ -276,6 +277,30 @@ public static <E> SynchronousQueue<E> newSynchronousQueue() {
276277
return new SynchronousQueue<>();
277278
}
278279

280+
/**
281+
* Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested {@code
282+
* numElements} elements are not available, it will wait for them up to the specified timeout.
283+
*
284+
* @param q the blocking queue to be drained
285+
* @param buffer where to add the transferred elements
286+
* @param numElements the number of elements to be waited for
287+
* @param timeout how long to wait before giving up
288+
* @return the number of elements transferred
289+
* @throws InterruptedException if interrupted while waiting
290+
* @since NEXT (but since 28.0 in the JRE flavor)
291+
*/
292+
@CanIgnoreReturnValue
293+
@J2ktIncompatible
294+
@GwtIncompatible // BlockingQueue
295+
@SuppressWarnings("Java7ApiChecker")
296+
@IgnoreJRERequirement // Users will use this only if they're already using Duration
297+
public static <E> int drain(
298+
BlockingQueue<E> q, Collection<? super E> buffer, int numElements, Duration timeout)
299+
throws InterruptedException {
300+
// TODO(b/126049426): Consider using saturateToNanos(timeout) instead.
301+
return drain(q, buffer, numElements, timeout.toNanos(), NANOSECONDS);
302+
}
303+
279304
/**
280305
* Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested {@code
281306
* numElements} elements are not available, it will wait for them up to the specified timeout.
@@ -323,6 +348,30 @@ public static <E> int drain(
323348
return added;
324349
}
325350

351+
/**
352+
* Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, Duration)}, but with a
353+
* different behavior in case it is interrupted while waiting. In that case, the operation will
354+
* continue as usual, and in the end the thread's interruption status will be set (no {@code
355+
* InterruptedException} is thrown).
356+
*
357+
* @param q the blocking queue to be drained
358+
* @param buffer where to add the transferred elements
359+
* @param numElements the number of elements to be waited for
360+
* @param timeout how long to wait before giving up
361+
* @return the number of elements transferred
362+
* @since NEXT (but since 28.0 in the JRE flavor)
363+
*/
364+
@CanIgnoreReturnValue
365+
@J2ktIncompatible
366+
@GwtIncompatible // BlockingQueue
367+
@SuppressWarnings("Java7ApiChecker")
368+
@IgnoreJRERequirement // Users will use this only if they're already using Duration
369+
public static <E> int drainUninterruptibly(
370+
BlockingQueue<E> q, Collection<? super E> buffer, int numElements, Duration timeout) {
371+
// TODO(b/126049426): Consider using saturateToNanos(timeout) instead.
372+
return drainUninterruptibly(q, buffer, numElements, timeout.toNanos(), NANOSECONDS);
373+
}
374+
326375
/**
327376
* Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, long, TimeUnit)}, but
328377
* with a different behavior in case it is interrupted while waiting. In that case, the operation

guava/src/com/google/common/collect/Queues.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public static <E> SynchronousQueue<E> newSynchronousQueue() {
287287
* @param timeout how long to wait before giving up
288288
* @return the number of elements transferred
289289
* @throws InterruptedException if interrupted while waiting
290-
* @since 28.0
290+
* @since 28.0 (but only since 33.4.0 in the Android flavor)
291291
*/
292292
@CanIgnoreReturnValue
293293
@J2ktIncompatible
@@ -357,7 +357,7 @@ public static <E> int drain(
357357
* @param numElements the number of elements to be waited for
358358
* @param timeout how long to wait before giving up
359359
* @return the number of elements transferred
360-
* @since 28.0
360+
* @since 28.0 (but only since 33.4.0 in the Android flavor)
361361
*/
362362
@CanIgnoreReturnValue
363363
@J2ktIncompatible

0 commit comments

Comments
 (0)