Package com.illumon.util.datastructures
Class SimpleReferenceManager<T,R extends com.fishlib.base.reference.SimpleReference<T>>
java.lang.Object
com.illumon.util.datastructures.SimpleReferenceManager<T,R>
public final class SimpleReferenceManager<T,R extends com.fishlib.base.reference.SimpleReference<T>> extends Object
A helper for manging a list of References. It hides the internal management
of expired references and provides for iteration over the valid ones
-
Constructor Summary
Constructors Constructor Description SimpleReferenceManager(Function<T,R> referenceFactory)
Create a SimpleReferenceManager, withCopyOnWriteArrayList
as backing structure.SimpleReferenceManager(Function<T,R> referenceFactory, boolean concurrent)
Create a SimpleReferenceManager, with eitherArrayList
orCopyOnWriteArrayList
as backing structure. -
Method Summary
Modifier and Type Method Description R
add(T item)
Add the specified item to the list.void
clear()
Clear the list of references.void
forEach(BiConsumer<R,T> consumer)
Execute the provided procedure on each reference, item pair whose item is still reachable, while removing any cleared references.T
getFirstItem(Predicate<T> filter)
Retrieve the first valid item that satisfies a filter.R
getFirstReference(Predicate<T> filter)
Retrieve the first valid reference whose item satisfies a filter.boolean
isEmpty()
Return true if the list is empty.void
remove(T item)
Remove item from the list if present according to reference equality (==
), and also any cleared references.void
removeAll(Collection<T> items)
Remove items in the collection from the list, and also any cleared references.void
removeIf(Predicate<T> filter)
Retrieve all encountered items that satisfy a filter, while also removing any cleared references.
-
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
-
-
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.
-
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
-
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.
-
clear
public void clear()Clear the list of references.
-