code: public static <E> int drainQueue(Queue<E> queue,
                                       int elementsToDrain,
                                       Predicate<E> drainedCountFilter) {
          int drained = 0;
          boolean drainMore = true;
          for (int i = 0; i < elementsToDrain && drainMore; i++) {
              E polled = queue.poll();
              if (polled != null) {
                  if (drainedCountFilter == null || drainedCountFilter.test(polled)) {
                      drained++;
                  }
              } else {
                  drainMore = false;
              }
          }
  
          return drained;
      }
docs: Drains the provided queue by the count provided in {@code elementsToDrain}.
  You may provide a predicate which will allow some elements to be drained
  but not be counted against the returned number of drained events.
  
  @param queue              The queue to be drained
  @param elementsToDrain    The number of elements to be drained from
  the queue
  @param drainedCountFilter filter which determines if the drained element
  is counted against the returned count. The filter
  may be {@code null} in which case all elements
  match
  @param <E>                the type of the elements in the queue
  @return The number of elements drained from the queue which pass the
  given predicate