KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > util > MapCache


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2005, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.util;
21
22 import java.util.LinkedHashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.bcel.classfile.JavaClass;
26
27 /**
28  * Provide a HashMap that can only grow to a specified maximum capacity,
29  * with entries discarded using a LRU policy to keep the size of the HashMap
30  * within that bound.
31  *
32  * @author pugh
33  */

34 public class MapCache<K,V> extends LinkedHashMap JavaDoc<K,V> {
35     private static final long serialVersionUID = 0L;
36     int maxCapacity;
37     /**
38      * Create a new MapCache
39      * @param maxCapacity - maximum number of entries in the map
40      */

41     public MapCache(int maxCapacity) {
42         super(16, 0.75f, true);
43         this.maxCapacity = maxCapacity;
44         count = new int[maxCapacity];
45     }
46     int [] count;
47     @Override JavaDoc
48     public V get(Object JavaDoc k) {
49         if (false) {
50         int age = count.length-1;
51         for(Map.Entry JavaDoc<K,V> e : entrySet()) {
52             if (e.getKey().equals(k)) {
53                 count[age]++;
54                 if (false && age > 20 && k instanceof JavaClass) {
55                     
56                     System.out.println("Reusing value from " + age + " steps ago ");
57                     System.out.println("Class " + ((JavaClass)k).getClassName());
58                     new RuntimeException JavaDoc().printStackTrace(System.out);
59                 }
60                 break;
61             }
62             age--;
63             
64         }
65         }
66         return super.get(k);
67     }
68     @Override JavaDoc
69     protected boolean removeEldestEntry(Map.Entry JavaDoc<K,V> eldest) {
70         boolean result = size() > maxCapacity;
71         if (false && result && eldest.getKey() instanceof JavaClass)
72                 System.out.println("Dropping " + ((JavaClass)eldest.getKey()).getClassName());
73         return result;
74      }
75     
76     public String JavaDoc getStatistics() {
77         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
78         for(int c : count)
79             b.append(c).append(" ");
80         return b.toString();
81     }
82
83 }
84
Popular Tags