1 /*
2  * Copyright 2014-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15 package com.amazonaws.internal;
16
17 import java.util.LinkedHashMap;
18 import java.util.Map;
19
20 /**
21  * A bounded linked hash map that would remove the eldest entry when the map
22  * size exceeds a configurable maximum.
23  */

24 final class BoundedLinkedHashMap<K,V> extends LinkedHashMap<K,V> {
25     private static final long serialVersionUID = 1L;
26     private final int maxSize;
27     BoundedLinkedHashMap(int maxSize) {
28         this.maxSize = maxSize;
29     }
30
31     /**
32      * {@inheritDoc}
33      * 
34      * Returns true if the size of this map exceeds the maximum.
35      */

36     @Override
37     protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
38         return size() > maxSize;
39     }
40
41     /**
42      * Returns the maximum size of this map beyond which the eldest entry
43      * will get removed.
44      */

45     int getMaxSize() {
46         return maxSize;
47     }
48 }