Improved FileWatch class comment and merged in the interface.

This commit is contained in:
Ziver Koc 2018-11-03 20:07:33 +01:00
parent 62923e7f60
commit 9215c528f0
3 changed files with 23 additions and 47 deletions

View file

@ -1,42 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Ziver Koc
*
* 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.
*/
package zutil.io.file;
import java.io.File;
/**
* Interface for the FileWatcher class
*
* @author Ziver
*/
public interface FileChangeListener{
/**
* This method is called when there is a change in a file
*
* @param file The file that has changed
*/
void fileChangedEvent(File file);
}

View file

@ -25,20 +25,24 @@
package zutil.io.file;
import zutil.io.MultiPrintStream;
import zutil.log.LogUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Logger;
/**
* This class calls a given listener
* when a file is changed
* This class checks a file periodically for changes to the
* last modified date and calls the registered listeners.
*
* @author Ziver
*
*/
public class FileWatcher extends TimerTask{
private static Logger logger = LogUtil.getLogger();
private FileChangeListener listener;
private long lastChanged;
private File file;
@ -82,8 +86,23 @@ public class FileWatcher extends TimerTask{
listener.fileChangedEvent(file);
}
else{
MultiPrintStream.out.println("File Changed: "+file);
logger.fine("File was modified ("+file+") but no listeners was registered.");
}
}
}
/**
* Interface for the FileWatcher class
*
* @author Ziver
*/
public interface FileChangeListener{
/**
* This method is called when there is a change in a file
*
* @param file The file that has changed
*/
void fileChangedEvent(File file);
}
}

View file

@ -26,10 +26,9 @@ package zutil.io.file;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
public class FileChangedTest implements FileChangeListener{
public class FileChangedTest implements FileWatcher.FileChangeListener {
public static void main(String[] args) throws FileNotFoundException{
FileWatcher watcher = new FileWatcher(FileUtil.find("test.txt"));
watcher.setListener(new FileChangedTest());