@@ -88,14 +88,15 @@ boolean isExpired() {
8888 * Constructs a new {@code LIFOCache} instance using the provided {@link Builder}.
8989 *
9090 * <p>This constructor initializes the cache with the specified capacity and default TTL,
91- * sets up internal data structures (a {@code LinkedHashMap} for cache entries and configures eviction.
91+ * sets up internal data structures (a {@code HashMap} for cache entries,
92+ * {an @code ArrayDeque}, for key storage, and configures eviction.
9293 *
9394 * @param builder the {@code Builder} object containing configuration parameters
9495 */
9596 private LIFOCache (Builder <K , V > builder ) {
9697 this .capacity = builder .capacity ;
9798 this .defaultTTL = builder .defaultTTL ;
98- this .cache = HashMap . newHashMap (builder .capacity );
99+ this .cache = new HashMap <> (builder .capacity );
99100 this .keys = new ArrayDeque <>(builder .capacity );
100101 this .lock = new ReentrantLock ();
101102 this .evictionListener = builder .evictionListener ;
@@ -123,7 +124,7 @@ public V get(K key) {
123124 try {
124125 evictionStrategy .onAccess (this );
125126
126- CacheEntry <V > entry = cache .get (key );
127+ final CacheEntry <V > entry = cache .get (key );
127128 if (entry == null || entry .isExpired ()) {
128129 if (entry != null ) {
129130 cache .remove (key );
@@ -178,7 +179,7 @@ public void put(K key, V value, long ttlMillis) {
178179 try {
179180 // If key already exists, remove it. It will later be re-inserted at top of stack
180181 keys .remove (key );
181- CacheEntry <V > oldEntry = cache .remove (key );
182+ final CacheEntry <V > oldEntry = cache .remove (key );
182183 if (oldEntry != null && !oldEntry .isExpired ()) {
183184 notifyEviction (key , oldEntry .value );
184185 }
@@ -188,8 +189,8 @@ public void put(K key, V value, long ttlMillis) {
188189
189190 // If no expired entry was removed, remove the youngest
190191 if (cache .size () >= capacity ) {
191- K youngestKey = keys .pollLast ();
192- CacheEntry <V > youngestEntry = cache .remove (youngestKey );
192+ final K youngestKey = keys .pollLast ();
193+ final CacheEntry <V > youngestEntry = cache .remove (youngestKey );
193194 notifyEviction (youngestKey , youngestEntry .value );
194195 }
195196
@@ -210,11 +211,11 @@ public void put(K key, V value, long ttlMillis) {
210211 */
211212 private int evictExpired () {
212213 int count = 0 ;
213- Iterator <K > it = keys .iterator ();
214+ final Iterator <K > it = keys .iterator ();
214215
215216 while (it .hasNext ()) {
216- K k = it .next ();
217- CacheEntry <V > entry = cache .get (k );
217+ final K k = it .next ();
218+ final CacheEntry <V > entry = cache .get (k );
218219 if (entry != null && entry .isExpired ()) {
219220 it .remove ();
220221 cache .remove (k );
@@ -238,7 +239,7 @@ public V removeKey(K key) {
238239 }
239240 lock .lock ();
240241 try {
241- CacheEntry <V > entry = cache .remove (key );
242+ final CacheEntry <V > entry = cache .remove (key );
242243 keys .remove (key );
243244
244245 // No such key in cache
@@ -361,7 +362,7 @@ public void clear() {
361362 public Set <K > getAllKeys () {
362363 lock .lock ();
363364 try {
364- Set <K > result = new HashSet <>();
365+ final Set <K > result = new HashSet <>();
365366
366367 for (Map .Entry <K , CacheEntry <V >> entry : cache .entrySet ()) {
367368 if (!entry .getValue ().isExpired ()) {
@@ -397,7 +398,7 @@ public EvictionStrategy<K, V> getEvictionStrategy() {
397398 public String toString () {
398399 lock .lock ();
399400 try {
400- Map <K , V > visible = new LinkedHashMap <>();
401+ final Map <K , V > visible = new LinkedHashMap <>();
401402 for (Map .Entry <K , CacheEntry <V >> entry : cache .entrySet ()) {
402403 if (!entry .getValue ().isExpired ()) {
403404 visible .put (entry .getKey (), entry .getValue ().value );
0 commit comments