I don't see why you think a general collection, that is in 99.9% of the cases not used to implement an MRU, should burden every call to #add with a check to see if it isn't exceeding its maximum size or to see if a maximum size has been set.

This is much better done by composition, as I don't think all methods offered by `Collection` should even be part of an `MRU` interface.

--John

On 20/09/2022 21:08, Ernie Rael wrote:
(There may be a better place to send this, let me know where)

Suggesting an option to limit the size of the collection, e.g "setMaxSize(int)", default of zero means no limit.

I put together "interface MRU<E> extends Collection<E>" some months ago, it has two implementations based on LinkedList and LinkedHashSet. The code can be seen at https://sourceforge.net/p/jvi/raelity-lib/ci/default/tree/lib/src/main/java/com/raelity/lib/

A SequencedCollection, as outlined in the JEP draft 2020/09/01, would be almost perfect to implement MRU; I've run into most of the problems/issues discussed in the JEP draft.

The MRU is a cache, as I use it; it typically has a max size for the collection. Handling this natively in the collection would be ideal; if an add operation would overflow, remove the item at the other end. Note that addAll() is used when initializing from backing store.

FYI, I use a "Supplier<Integer>" to the constructor to provide maxSize, but a property makes much more sense. I'll make that change in MRU for sanity, and get rid of the trim() method. setMaxSize can do the trim.

-ernie

Reply via email to