KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > cache > MethodCache


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

17
18 package org.objectweb.jac.aspects.cache;
19
20 import java.util.Hashtable JavaDoc;
21 import org.apache.log4j.Logger;
22 import org.objectweb.jac.aspects.timestamp.Timestamps;
23 import org.objectweb.jac.util.ObjectArray;
24
25 public class MethodCache extends Hashtable JavaDoc {
26     static final Logger logger = Logger.getLogger("cache");
27
28     public MethodCache(Timestamps stamps) {
29         this.stamps = stamps;
30     }
31
32     Timestamps stamps;
33     public Entry getEntry(ObjectArray args, int[] ignoredArgs) {
34         Object JavaDoc[] argsArray = args.getArray();
35         if (ignoredArgs!=null) {
36             for (int i=0; i<ignoredArgs.length; i++) {
37                 argsArray[ignoredArgs[i]] = null;
38             }
39         }
40         Entry entry = (Entry)get(args);
41         if (entry!=null) {
42             logger.debug(" cache hit "+args.hashCode());
43             if (stamps!=null) {
44                 int argCount = argsArray.length;
45                 long entryTime = entry.time;
46                 int i;
47                 for (i=0; i<argCount; i++) {
48                     Object JavaDoc arg = argsArray[i];
49                     if (arg!=null) {
50                         long mtime = stamps.getStamp(arg);
51                         if (mtime > entryTime) {
52                             logger.debug(
53                                 " outdated for arg "+i+"("+arg+"): "+
54                                 mtime+">"+entryTime);
55                             break;
56                         }
57                     }
58                 }
59                 if (i==argCount) // we didn't break in the middle
60
return (Entry)get(args);
61                 else
62                     return null;
63             } else {
64                 return ((Entry)get(args));
65             }
66         } else {
67             logger.debug(" cache miss "+args.hashCode());
68             return null;
69         }
70     }
71
72     public void putEntry(ObjectArray args, Object JavaDoc value) {
73         put(args,new Entry(value));
74     }
75
76     public static class Entry {
77         long time;
78         public Object JavaDoc value;
79         public Entry(Object JavaDoc value) {
80             this.value = value;
81             this.time = System.currentTimeMillis();
82         }
83     }
84 }
85
Popular Tags