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.timestamp; 019 020 import java.util.Hashtable; 021 import java.util.Iterator; 022 import java.util.Map.Entry; 023 import java.io.File; 024 025 public class Timestamps { 026 Hashtable stamps = new Hashtable(); 027 028 /** 029 * Set the stamp of an object to current time 030 * @see #touch(Object) 031 */ 032 public void touch(Object object) { 033 stamps.put(object,new Long(System.currentTimeMillis())); 034 } 035 036 /** 037 * Gets the time an object was last modified, or 0. 038 * 039 * @param object the object whose timestamp you request 040 * @return if object is a file, obect.lastModified(), otherwise 041 * the stored timestamp for that object, or if 0 no timestamp is 042 * stored for it. 043 * 044 * @see #touch(Object) 045 */ 046 public long getStamp(Object object) { 047 if (object instanceof File) { 048 return ((File)object).lastModified(); 049 } else { 050 Long stamp = (Long)stamps.get(object); 051 if (stamp!=null) { 052 return stamp.longValue(); 053 } else { 054 return 0; 055 } 056 } 057 } 058 059 /** 060 * Delete all timestamps 061 */ 062 public void touchAll() { 063 Long time = new Long(System.currentTimeMillis()); 064 Iterator i = stamps.entrySet().iterator(); 065 while (i.hasNext()) { 066 Entry entry = (Entry)i.next(); 067 entry.setValue(time); 068 } 069 } 070 }