001    /*
002      Copyright (C) 2004 Laurent Martelli <laurent@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public License
015      along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
017    
018    package org.objectweb.jac.aspects.cache;
019    
020    import java.util.Hashtable;
021    import org.apache.log4j.Logger;
022    import org.objectweb.jac.aspects.timestamp.Timestamps;
023    import org.objectweb.jac.util.ObjectArray;
024    
025    public class MethodCache extends Hashtable {
026        static final Logger logger = Logger.getLogger("cache");
027    
028        public MethodCache(Timestamps stamps) {
029            this.stamps = stamps;
030        }
031    
032        Timestamps stamps;
033        public Entry getEntry(ObjectArray args, int[] ignoredArgs) {
034            Object[] argsArray = args.getArray();
035            if (ignoredArgs!=null) {
036                for (int i=0; i<ignoredArgs.length; i++) {
037                    argsArray[ignoredArgs[i]] = null;
038                }
039            }
040            Entry entry = (Entry)get(args);
041            if (entry!=null) {
042                logger.debug("  cache hit "+args.hashCode());
043                if (stamps!=null) {
044                    int argCount = argsArray.length;
045                    long entryTime = entry.time;
046                    int i;
047                    for (i=0; i<argCount; i++) {
048                        Object arg = argsArray[i];
049                        if (arg!=null) {
050                            long mtime = stamps.getStamp(arg);
051                            if (mtime > entryTime) {
052                                logger.debug(
053                                    "  outdated for arg "+i+": "+
054                                    mtime+">"+entryTime);
055                                break;
056                            }
057                        }
058                    }
059                    if (i==argCount) // we didn't break in the middle
060                        return (Entry)get(args);
061                    else
062                        return null;
063                } else {
064                    return ((Entry)get(args));
065                }
066            } else {
067                logger.debug("  cache miss "+args.hashCode());
068                return null;
069            }
070        }
071    
072        public void putEntry(ObjectArray args, Object value) {
073            put(args,new Entry(value));
074        }
075    
076        public static class Entry {
077            long time;
078            public Object value;
079            public Entry(Object value) {
080                this.value = value;
081                this.time = System.currentTimeMillis();
082            }
083        }
084    }