Class VFXCellsCache<T,C extends VFXCell<T>>
VFXCell.
Cells are stored in a CellsQueue, a special extension of LinkedList.
The cache can be limited in the maximum number of cells to keep by setting its capacity, see VFXList.cacheCapacityProperty().
Cells won't be added if the capacity is reached and will be disposed immediately instead.
Aside from the typical functions of a cache (take/store), you are also allowed to 'populate' it at will, see populate().
Note however that to generate cells, the cache must know the function to produce them. The function must be the same used by the container, therefore, it's set by the container itself.
This is mostly useful to 'pre-populate' it, filling the cache before the container is even laid out, so that at init cells are already built and just need to be updated.
Beware, in order for this to work, the cells you are using must allow null items!
I often thought about optimizing the cache by using a Map instead of a Queue to store the cached cells.
However, doing so poses a variety of issues that make such an improvement not possible.
The enhancement would make the cache more efficient in theory because when a cell is de-cached it could be extracted by the needed item. Which means that if a cell was previously built for that item, the only update needed would be the index. The current implementation, polls the first available cell in the cache and updates both the item and the index.
Cons
- The populate() method could not work because the cells are produced with null items. So, these
cells would require a separate collection. This would also make controlling the cache capacity a bit harder, as well as
add and poll operations.
- The mapping [Item -> VFXCell] raises the problem of updating the cache when the items list changes. If an item is removed from the list, and a mapping is present in the cache, then it becomes invalid. Which means that the cell associated with that item would need to be disposed or moved to the other collection.
- T items come from "outside". In this new version of VirtualizedFX, to simplify the handling of changes in
the items' list, mappings of type [Item -> VFXCell] are used in the state. But to avoid memory leaks, old maps are always
cleaned, so that there is not any reference to old items. So, again, we would have to manage this aspect too for the cache.
-
Constructor Summary
ConstructorsConstructorDescriptionVFXCellsCache(CellFactory<T, C> cellFactory) VFXCellsCache(CellFactory<T, C> cellFactory, int capacity) -
Method Summary
Modifier and TypeMethodDescriptionfinal VFXCellsCache<T, C> Adds the given cells to the queue.cache(Collection<C> cells) Adds the given cells to the queue.clear()Disposes and removes all the cells from the cache.populate()Fills the cache to its limit.Removed the specified cell from the cache's queue.setCapacity(int capacity) Sets the cache's capacity.intsize()take()Removes one cell from the cache, specifically from the queue's head, so the oldest cached cell.tryTake()
-
Constructor Details
-
VFXCellsCache
-
VFXCellsCache
-
-
Method Details
-
populate
Fills the cache to its limit. Can be useful to 'pre-populate' the cache before init time, making cells already available and just in need of an update (VFXCell.updateIndex(int),VFXCell.updateItem(Object)).- Throws:
NullPointerException- ifgetCellFactory()returnsnull
-
cache
Adds the given cells to the queue. For successfully cached cells,VFXCell.onCache()will automatically be invoked. -
cache
Adds the given cells to the queue. For successfully cached cells,VFXCell.onCache()will automatically be invoked. -
take
Removes one cell from the cache, specifically from the queue's head, so the oldest cached cell. Beware this can return anullvalue, if it's not,VFXCell.onDeCache()is automatically invoked. -
tryTake
-
remove
Removed the specified cell from the cache's queue. The removed cell is also disposed. -
clear
Disposes and removes all the cells from the cache. -
size
public int size()- Returns:
- the number of cached cells
-
setCapacity
Sets the cache's capacity. -
getCellFactory
-