Package io.deephaven.util.datastructures
Class SimpleReferenceManager<T,R extends SimpleReference<T>>
java.lang.Object
io.deephaven.util.datastructures.SimpleReferenceManager<T,R>
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 Summary
ConstructorDescriptionSimpleReferenceManager
(@NotNull Function<T, R> referenceFactory) Create a SimpleReferenceManager, withCopyOnWriteArrayList
as backing structure.SimpleReferenceManager
(@NotNull Function<T, R> referenceFactory, boolean concurrent) Create a SimpleReferenceManager, with eitherArrayList
orCopyOnWriteArrayList
as backing structure.SimpleReferenceManager
(@NotNull Function<T, R> referenceFactory, @NotNull Collection<R> referenceCollection) Create a SimpleReferenceManager with the specifiedreferenceCollection
as the backing data structure.. -
Method Summary
Modifier and TypeMethodDescriptionAdd the specified item to the list.void
clear()
Clear the list of references.void
forEach
(@NotNull BiConsumer<R, T> consumer) Execute the provided procedure on each reference, item pair whose item is still reachable, while removing any cleared references.getFirstItem
(@NotNull Predicate<T> filter) Retrieve the first valid item that satisfies a filter.getFirstReference
(@NotNull Predicate<T> filter) Retrieve the first valid reference whose item satisfies a filter.boolean
isEmpty()
Return true if the list is empty.Remove item from the list if present according to reference equality (==
), and also any cleared references.void
removeAll
(@NotNull Collection<T> items) Remove items in the collection from the list, and also any cleared references.boolean
Retrieve all encountered items that satisfy a filter, while also removing any cleared references.int
size()
Return the number of valid references in the list.
-
Constructor Details
-
SimpleReferenceManager
Create a SimpleReferenceManager, withCopyOnWriteArrayList
as backing structure.- Parameters:
referenceFactory
- Factory to create references for added referents; should always make a unique reference
-
SimpleReferenceManager
Create a SimpleReferenceManager, with eitherArrayList
orCopyOnWriteArrayList
as backing structure.- Parameters:
referenceFactory
- Factory to create references for added referents; should always make a unique referenceconcurrent
- 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 specifiedreferenceCollection
as the backing data structure..- Parameters:
referenceFactory
- Factory to create references for added referents; should always make a unique referencereferenceCollection
- The collection to use for holding references
-
-
Method Details
-
add
Add the specified item to the list.- Parameters:
item
- the item to add.
-
remove
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
Remove items in the collection from the list, and also any cleared references.- Parameters:
items
- the items to remove.
-
removeIf
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
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
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
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.
-