| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MaxSizeMap |
|
| 1.0;1 |
| 1 | /* | |
| 2 | * Copyright 2008-2009 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.kuali.rice.core.util; | |
| 17 | ||
| 18 | import java.util.LinkedHashMap; | |
| 19 | import java.util.Map.Entry; | |
| 20 | ||
| 21 | /** | |
| 22 | * This class acts like an LRU cache, automatically purging contents when it gets above a certain size. | |
| 23 | * | |
| 24 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 25 | */ | |
| 26 | public class MaxSizeMap<K,V> extends LinkedHashMap<K,V> { | |
| 27 | ||
| 28 | private int maxSize; | |
| 29 | ||
| 30 | /** | |
| 31 | * @param maxSize | |
| 32 | */ | |
| 33 | public MaxSizeMap( int maxSize ) { | |
| 34 | 0 | this( maxSize, false ); |
| 35 | 0 | } |
| 36 | /** | |
| 37 | * @param maxSize | |
| 38 | * @param accessOrder Whether to sort in the order accessed rather than the order inserted. | |
| 39 | */ | |
| 40 | public MaxSizeMap( int maxSize, boolean accessOrder ) { | |
| 41 | 0 | super( maxSize / 2, 0.75f, accessOrder ); |
| 42 | 0 | this.maxSize = maxSize; |
| 43 | 0 | } |
| 44 | ||
| 45 | /** | |
| 46 | * @see java.util.LinkedHashMap#removeEldestEntry(java.util.Map.Entry) | |
| 47 | */ | |
| 48 | @Override | |
| 49 | protected boolean removeEldestEntry(Entry<K,V> eldest) { | |
| 50 | 0 | return size() > maxSize; |
| 51 | } | |
| 52 | ||
| 53 | } |