001 /** 002 * 003 * Copyright 2004 Protique Ltd 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 **/ 018 package org.activemq.util; 019 020 import java.util.LinkedHashMap; 021 import java.util.Map; 022 023 /** 024 * Represnts an LRUCache of a fixed maximum size which by default will 025 * remove items based on access order but can be used to use insertion order 026 * 027 * @version $Revision: 1.1.1.1 $ 028 */ 029 public class LRUCache extends LinkedHashMap { 030 private static final long serialVersionUID = -5754338187296859149L; 031 032 protected static final int DEFAULT_INITIAL_CAPACITY = 1000; 033 protected static final float DEFAULT_LOAD_FACTOR = (float) 0.75; 034 035 private int maxSize; 036 037 public LRUCache(int initialCapacity, float loadFactor, boolean accessOrder, int maxSize) { 038 super(initialCapacity, loadFactor, accessOrder); 039 this.maxSize = maxSize; 040 } 041 042 public LRUCache(int maxSize) { 043 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, true, maxSize); 044 } 045 046 public LRUCache(int maxSize, boolean accessOrder) { 047 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, accessOrder, maxSize); 048 } 049 050 protected boolean removeEldestEntry(Map.Entry eldest) { 051 return size() > maxSize; 052 } 053 }