Class SimpleReferenceManager<T,R extends SimpleReference<T>>

java.lang.Object
io.deephaven.util.datastructures.SimpleReferenceManager<T,R>

public final class SimpleReferenceManager<T,R extends SimpleReference<T>> extends Object
A helper for managing a list of References. It hides the internal management of expired references and provides for iteration over the valid ones
  • Constructor Details

    • SimpleReferenceManager

      public SimpleReferenceManager(@NotNull @NotNull Function<T,R> referenceFactory)
      Create a SimpleReferenceManager, with CopyOnWriteArrayList as backing structure.
      Parameters:
      referenceFactory - Factory to create references for added referents; should always make a unique reference
    • SimpleReferenceManager

      public SimpleReferenceManager(@NotNull @NotNull Function<T,R> referenceFactory, boolean concurrent)
      Create a SimpleReferenceManager, with either ArrayList or CopyOnWriteArrayList as backing structure.
      Parameters:
      referenceFactory - Factory to create references for added referents; should always make a unique reference
      concurrent - Use CopyOnWriteArrayList for internal storage if true, else ArrayList
    • SimpleReferenceManager

      public SimpleReferenceManager(@NotNull @NotNull Function<T,R> referenceFactory, @NotNull @NotNull Collection<R> referenceCollection)
      Create a SimpleReferenceManager with the specified referenceCollection as the backing data structure..
      Parameters:
      referenceFactory - Factory to create references for added referents; should always make a unique reference
      referenceCollection - The collection to use for holding references
  • Method Details

    • add

      public R add(@NotNull T item)
      Add the specified item to the list.
      Parameters:
      item - the item to add.
    • remove

      public T remove(@NotNull T item)
      Remove item from the list if present according to reference equality (==), and also any cleared references.
      Parameters:
      item - the item to remove.
      Returns:
      The item if it was removed, else null
    • removeAll

      public void removeAll(@NotNull @NotNull Collection<T> items)
      Remove items in the collection from the list, and also any cleared references.
      Parameters:
      items - the items to remove.
    • removeIf

      public boolean removeIf(@NotNull @NotNull Predicate<T> filter)
      Retrieve all encountered items that satisfy a filter, while also removing any cleared references.
      Parameters:
      filter - The filter to decide if a valid item should be removed
      Returns:
      Whether we succeeded in removing anything
    • forEach

      public void forEach(@NotNull @NotNull BiConsumer<R,T> consumer)
      Execute the provided procedure on each reference, item pair whose item is still reachable, while removing any cleared references.
      Parameters:
      consumer - The function to call with each reachable pair
    • getFirstItem

      public T getFirstItem(@NotNull @NotNull Predicate<T> filter)
      Retrieve the first valid item that satisfies a filter. Remove any encountered cleared references as a side effect.
      Parameters:
      filter - The filter to decide if a valid item should be returned
      Returns:
      The first valid item that passed the filter, or null if no such item exists
    • getFirstReference

      public R getFirstReference(@NotNull @NotNull Predicate<T> filter)
      Retrieve the first valid reference whose item satisfies a filter. Remove any encountered cleared references as a side effect.
      Parameters:
      filter - The filter to decide if a valid item should be returned
      Returns:
      The first valid item that passed the filter, or null if no such item exists
    • isEmpty

      public boolean isEmpty()
      Return true if the list is empty. Does not check for cleared references.
      Returns:
      true if the list is empty.
    • size

      public int size()
      Return the number of valid references in the list. Note that each reference is checked for validity, making this operation linear in the number of references.
      Returns:
      the number of valid references in the list
    • clear

      public void clear()
      Clear the list of references.