/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn; import java.io.File; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import com.sleepycat.je.DatabaseEntry; public class DirUpdateInfoEntry implements java.io.Serializable, Comparable { /** * Comment for serialVersionUID */ private static final long serialVersionUID = 1L; public String RelPath; public long Length; public long LastMod; public long ScanPass; public boolean IsDir; public long DetectTime; /** Should only be called by bean constructor */ public DirUpdateInfoEntry() { RelPath=""; Length=0; LastMod=0; ScanPass=0; IsDir=false; } public DirUpdateInfoEntry(String relPath, File F) { RelPath=relPath; Length=0; LastMod=0; DetectTime=System.currentTimeMillis(); update(F); } public DirUpdateInfoEntry(DatabaseEntry Data) { try { ObjectInputStream IN=new ObjectInputStream(new ByteArrayInputStream(Data.getData())); //XMLDecoder Decode=new XMLDecoder(IN); DirUpdateInfoEntry E=(DirUpdateInfoEntry)IN.readObject(); RelPath=E.RelPath; Length=E.Length; LastMod=E.LastMod; ScanPass=E.ScanPass; IsDir=E.IsDir; DetectTime=E.DetectTime; } catch(java.io.IOException e) { e.printStackTrace(); } catch(java.lang.ClassNotFoundException e2) { e2.printStackTrace(); } } public DatabaseEntry getDatabaseEntry() { ByteArrayOutputStream Out=new ByteArrayOutputStream(); try { //XMLEncoder Encode=new XMLEncoder(Out); ObjectOutputStream OOS=new ObjectOutputStream(Out); OOS.writeObject(this); OOS.close(); } catch(java.io.IOException e) { e.printStackTrace(); } return new DatabaseEntry(Out.toByteArray()); } public boolean update(File F) { boolean Changes=false; if (F.isDirectory()!=IsDir) { Changes=true; IsDir=F.isDirectory(); } if (Length!=F.length()) { if (!IsDir) { Changes=true; } Length=F.length(); } if (LastMod!=F.lastModified()) { if (!IsDir) { Changes=true; } LastMod=F.lastModified(); } if (Changes) { DetectTime=System.currentTimeMillis(); } return Changes; } public Object clone() { /* TODO - less serialization for such a simple task would be leet */ DirUpdateInfoEntry N=new DirUpdateInfoEntry(getDatabaseEntry()); return N; } public boolean isDeleted(){return (Length==-2L);} public String getRelPath(){return RelPath;} public long getLength(){return Length;} public long getLastMod(){return LastMod;} public long getScanPass(){return ScanPass;} public boolean getIsDir(){return IsDir;} public long getDetectTime(){return DetectTime;} public void setRelPath(String v){RelPath=v;} public void setLength(long v){Length=v;} public void setLastMod(long v){LastMod=v;} public void setScanPass(long v){ScanPass=v;} public void setIsDir(boolean v){IsDir=v;} public void setDetectTime(long v){DetectTime=v;} public String toString() { StringBuffer SB=new StringBuffer(100); SB.append("DirUpdateInfoEntry{"); SB.append(RelPath); SB.append(",Dir="); SB.append(IsDir); SB.append(",Length="); SB.append(Length); SB.append("}"); return SB.toString(); } /** Higher is newer.*/ public int compareTo(Object O) { if (O instanceof DirUpdateInfoEntry) { DirUpdateInfoEntry E=(DirUpdateInfoEntry)O; if (getRelPath().compareTo(E.getRelPath())!=0) return getRelPath().compareTo(E.getRelPath()); if (getDetectTime() < E.getDetectTime()) return -2; if (getDetectTime() > E.getDetectTime()) return 2; return 0; } else throw new ClassCastException("DirUpdateInfoEntry can only be compared to like objects."); } }