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: 690111120
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 26, 2024
1 parent 2f04514 commit b650b9f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
49 changes: 49 additions & 0 deletions android/guava/src/com/google/common/collect/Queues.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -276,6 +277,30 @@ public static <E> SynchronousQueue<E> 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 <E> int drain(
BlockingQueue<E> q, Collection<? super E> 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.
Expand Down Expand Up @@ -323,6 +348,30 @@ public static <E> 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 <E> int drainUninterruptibly(
BlockingQueue<E> q, Collection<? super E> 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
Expand Down
4 changes: 2 additions & 2 deletions guava/src/com/google/common/collect/Queues.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public static <E> SynchronousQueue<E> 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
Expand Down Expand Up @@ -357,7 +357,7 @@ public static <E> 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
Expand Down

0 comments on commit b650b9f

Please sign in to comment.