|
21 | 21 | import com.google.common.annotations.J2ktIncompatible;
|
22 | 22 | import com.google.common.base.Preconditions;
|
23 | 23 | import com.google.errorprone.annotations.CanIgnoreReturnValue;
|
| 24 | +import java.time.Duration; |
24 | 25 | import java.util.ArrayDeque;
|
25 | 26 | import java.util.Collection;
|
26 | 27 | import java.util.Deque;
|
@@ -276,6 +277,30 @@ public static <E> SynchronousQueue<E> newSynchronousQueue() {
|
276 | 277 | return new SynchronousQueue<>();
|
277 | 278 | }
|
278 | 279 |
|
| 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 | + |
279 | 304 | /**
|
280 | 305 | * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested {@code
|
281 | 306 | * numElements} elements are not available, it will wait for them up to the specified timeout.
|
@@ -323,6 +348,30 @@ public static <E> int drain(
|
323 | 348 | return added;
|
324 | 349 | }
|
325 | 350 |
|
| 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 | + |
326 | 375 | /**
|
327 | 376 | * Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, long, TimeUnit)}, but
|
328 | 377 | * with a different behavior in case it is interrupted while waiting. In that case, the operation
|
|
0 commit comments