1 /*
2  * Copyright 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
16 package software.amazon.awssdk.auth.signer.internal;
17
18 import java.util.LinkedHashMap;
19 import java.util.Map;
20 import software.amazon.awssdk.annotations.SdkInternalApi;
21
22 /**
23  * A bounded linked hash map that would remove the eldest entry when the map
24  * size exceeds a configurable maximum.
25  */

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

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

49     int getMaxSize() {
50         return maxSize;
51     }
52 }
53