c88439c32629e99bd7e98f6a28c37418ab4f7a03
[appc.git] / appc-core / appc-common-bundle / java / org / onap / appc / cache / impl / LRUCache.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.cache.impl;
25
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29 import org.onap.appc.cache.CacheStrategy;
30
31 /**
32  * LRU cache implements CacheStategy<K, V>
33  * @param <K> Key
34  * @param <V> Value
35  */
36 public class LRUCache<K,V> implements CacheStrategy<K,V> {
37
38     private Map<K,V> map;
39
40     LRUCache(final Integer capacity){
41         map = new LinkedHashMap<K,V>(capacity, 0.75F, true) {
42             @Override
43             protected boolean removeEldestEntry(Map.Entry<K, V> eldest){
44                 return size() > capacity;
45             }
46         };
47     }
48
49     @Override
50     public V getObject(K key) {
51         return map.get(key);
52     }
53
54     @Override
55     public void putObject(K key, V value) {
56         map.put(key,value);
57     }
58 }