1 /* 2 * Copyright 2004-2009 the Seasar Foundation and the Others. 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 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 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, 13 * either express or implied. See the License for the specific language 14 * governing permissions and limitations under the License. 15 */ 16 package org.seasar.cubby.internal.util; 17 18 import java.util.LinkedHashMap; 19 import java.util.Map; 20 21 /** 22 * LRU (Least Recently Used) アルゴリズムによって最近最も使われなかったエントリを削除することによって、一定のサイズを保つ 23 * {@link Map} です。 24 * <p> 25 * 最近最も使われなかったもの 26 * </p> 27 * 28 * @param <K> 29 * キーの型 30 * @param <V> 31 * 値の型 32 * @author baba 33 */ 34 public class LruHashMap<K, V> extends LinkedHashMap<K, V> { 35 36 /** シリアルバージョン UID。 */ 37 private static final long serialVersionUID = 1L; 38 39 /** デフォルトの初期容量です。 */ 40 protected static final int DEFAULT_INITIAL_CAPACITY = 16; 41 42 /** デフォルトのロードファクタです。 */ 43 protected static final float DEFAULT_LOAD_FACTOR = 0.75f; 44 45 /** 46 * 上限サイズです。 47 */ 48 protected int limitSize; 49 50 /** 51 * {@link LruHashMap} を作成します。 52 * 53 * @param limitSize 54 * 上限サイズ 55 */ 56 public LruHashMap(final int limitSize) { 57 this(limitSize, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); 58 } 59 60 /** 61 * {@link LruHashMap}を作成します。 62 * 63 * @param limitSize 64 * @param initialCapacity 65 * @param loadFactor 66 */ 67 public LruHashMap(final int limitSize, final int initialCapacity, 68 final float loadFactor) { 69 super(initialCapacity, loadFactor, true); 70 this.limitSize = limitSize; 71 } 72 73 /** 74 * 上限サイズを返します。 75 * 76 * @return 上限サイズ 77 */ 78 public int getLimitSize() { 79 return limitSize; 80 } 81 82 /** 83 * {@inheritDoc} 84 */ 85 @Override 86 protected boolean removeEldestEntry(final Map.Entry<K, V> entry) { 87 return this.size() > limitSize; 88 } 89 90 }