com.hazelcast.util

## Class QueueUtil

- java.lang.Object
- - com.hazelcast.util.QueueUtil

-     public final class QueueUtil
      extends Object

  Utility class for Queues

- - ### Method Summary

    All Methods Static Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static int` | `drainQueue(Queue<?> queue)` Drains the provided queue by the size of the queue as it is known upfront draining.
    `static <E> int` | `drainQueue(Queue<E> queue, int elementsToDrain, Predicate<E> drainedCountFilter)` Drains the provided queue by the count provided in `elementsToDrain`.
    `static <E> int` | `drainQueue(Queue<E> queue, Predicate<E> drainedCountFilter)` Drains the provided queue by the size of the queue as it is known upfront draining.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### drainQueue

          public static int drainQueue(Queue<?> queue)

      Drains the provided queue by the size of the queue as it is known upfront draining.

      The rational for using this method is the same as for using `Collection.clear()`: to remove all the elements from the queue. There are two major differences to highlight:

      1. This method doesn't guarantee that the queue is empty on return if it is written concurrently.
      2. This method returns the number of drained elements, while `Collection.clear()` doesn't.

      These points makes this method more applicable than `Collection.clear()` is in conditions where the queue is written concurrently without blocking the writer threads for the time of draining the queue and the caller needs to know the number of elements removed from the queue.

      Parameters:
        `queue` - The queue to be drained
      Returns:
        The number of elements drained from the queue

    - #### drainQueue

          public static <E> int drainQueue(Queue<E> queue,
                                           Predicate<E> drainedCountFilter)

      Drains the provided queue by the size of the queue as it is known upfront draining.

      The rational for using this method is the same as for using `Collection.clear()`: to remove all the elements from the queue. There are two major differences to highlight:

      1. This method doesn't guarantee that the queue is empty on return if it is written concurrently.
      2. This method returns the number of drained elements, while `Collection.clear()` doesn't.

      These points makes this method more applicable than `Collection.clear()` is in conditions where the queue is written concurrently without blocking the writer threads for the time of draining the queue and the caller needs to know the number of elements removed from the queue.

      You may provide a predicate which will allow some elements to be drained but not be counted against the returned number of drained events.

      Type Parameters:
        `E` - the type of the elements in the queue
      Parameters:
        `queue` - The queue to be drained
        `drainedCountFilter` - filter which determines if the drained element is counted against the returned count. The filter may be `null` in which case all elements match
      Returns:
        The number of elements drained from the queue which pass the given predicate

    - #### drainQueue

          public static <E> int drainQueue(Queue<E> queue,
                                           int elementsToDrain,
                                           Predicate<E> drainedCountFilter)

      Drains the provided queue by the count provided in `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.
      Type Parameters:
        `E` - the type of the elements in the queue
      Parameters:
        `queue` - The queue to be drained
        `elementsToDrain` - The number of elements to be drained from the queue
        `drainedCountFilter` - filter which determines if the drained element is counted against the returned count. The filter may be `null` in which case all elements match
      Returns:
        The number of elements drained from the queue which pass the given predicate

Copyright © 2019. All rights reserved.