Class HashSetDelayedBlockingQueue<T>

java.lang.Object
com.usatiuk.utils.HashSetDelayedBlockingQueue<T>
Type Parameters:
T - the type of the objects in the queue

public class HashSetDelayedBlockingQueue<T> extends Object
Blocking queue that delays the objects for a given time, and deduplicates them.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new HashSetDelayedBlockingQueue with the specified delay.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(T el)
    Adds the object to the queue if it doesn't exist.
    Adds the object to the queue with no delay.
    Closes the queue and returns all objects in it.
    get()
    Gets the object from the queue, waiting for it if necessary.
    get(long timeout)
    Gets the object from the queue, waiting for it if necessary.
    Gets all objects from the queue that are ready to be processed.
    Gets all objects from the queue, waiting for them if necessary.
    getAllWait(int max)
    Gets all objects from the queue, waiting for them if necessary.
    getAllWait(int max, long timeout)
    Gets all objects from the queue, waiting for them if necessary.
    long
     
    boolean
    Checks if the queue has an object that is ready to be processed.
    merge(T el, Function<T,T> transformer)
    Merges the object with the old one.
    readd(T el)
    Adds the object to the queue, if it exists re-adds it with a new delay
    remove(T el)
    Removes the object from the queue.
    void
    setDelay(long delay)
    Sets the delay for the queue.
    Tries to get the object from the queue without waiting.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HashSetDelayedBlockingQueue

      public HashSetDelayedBlockingQueue(long delay)
      Creates a new HashSetDelayedBlockingQueue with the specified delay.
      Parameters:
      delay - the delay in milliseconds
  • Method Details

    • getDelay

      public long getDelay()
      Returns:
      the delay in milliseconds
    • setDelay

      public void setDelay(long delay)
      Sets the delay for the queue.
      Parameters:
      delay - the delay in milliseconds
    • add

      public boolean add(T el)
      Adds the object to the queue if it doesn't exist.
      Parameters:
      el - the object to add
      Returns:
      true if the object was added, false if it already exists
    • addNoDelay

      public T addNoDelay(T el)
      Adds the object to the queue with no delay.
      Parameters:
      el - the object to add
      Returns:
      the old object if it existed, null otherwise
    • readd

      public T readd(T el)
      Adds the object to the queue, if it exists re-adds it with a new delay
      Parameters:
      el - the object to add
      Returns:
      the old object if it existed, null otherwise
    • merge

      public T merge(T el, Function<T,T> transformer)
      Merges the object with the old one.
      Parameters:
      el - the object to merge
      transformer - the function to transform the old object
      Returns:
      the old object if it existed, null otherwise
    • remove

      public T remove(T el)
      Removes the object from the queue.
      Parameters:
      el - the object to remove
      Returns:
      the removed object, or null if it didn't exist
    • get

      public T get(long timeout) throws InterruptedException
      Gets the object from the queue, waiting for it if necessary.
      Parameters:
      timeout - the timeout in milliseconds, or -1 for no timeout
      Returns:
      the object, or null if it timed out
      Throws:
      InterruptedException - if the thread is interrupted
    • get

      public T get() throws InterruptedException
      Gets the object from the queue, waiting for it if necessary.
      Returns:
      the object
      Throws:
      InterruptedException - if the thread is interrupted
    • hasImmediate

      public boolean hasImmediate()
      Checks if the queue has an object that is ready to be processed.
      Returns:
      true if there is an object ready, false otherwise
    • tryGet

      @Nullable public T tryGet()
      Tries to get the object from the queue without waiting.
      Returns:
      the object, or null if it doesn't exist
    • getAll

      public Collection<T> getAll()
      Gets all objects from the queue that are ready to be processed.
      Returns:
      a collection of objects
    • close

      public Collection<T> close()
      Closes the queue and returns all objects in it.
      Returns:
      a collection of objects
    • getAllWait

      public Collection<T> getAllWait() throws InterruptedException
      Gets all objects from the queue, waiting for them if necessary.
      Returns:
      a collection of objects
      Throws:
      InterruptedException - if the thread is interrupted
    • getAllWait

      public Collection<T> getAllWait(int max) throws InterruptedException
      Gets all objects from the queue, waiting for them if necessary.
      Parameters:
      max - the maximum number of objects to get
      Returns:
      a collection of objects
      Throws:
      InterruptedException - if the thread is interrupted
    • getAllWait

      public Collection<T> getAllWait(int max, long timeout) throws InterruptedException
      Gets all objects from the queue, waiting for them if necessary.
      Parameters:
      max - the maximum number of objects to get
      timeout - the timeout in milliseconds, or -1 for no timeout
      Returns:
      a collection of objects
      Throws:
      InterruptedException - if the thread is interrupted