Package ca.cgjennings.io
Class FileChangeMonitor
- java.lang.Object
-
- ca.cgjennings.io.FileChangeMonitor
-
public class FileChangeMonitor extends java.lang.ObjectListeners that register a file with aFileChangeMonitorreceive notification when it changes.- Author:
- Chris Jennings
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFileChangeMonitor.ChangeTypeThe kind of change detected by the monitor.
-
Constructor Summary
Constructors Constructor Description FileChangeMonitor()Creates a new file change monitor which is independent of the shared instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFileChangeListener(FileChangeListener listener, java.io.File file)Begin monitoring a file for changes.voidaddFileChangeListener(FileChangeListener listener, java.io.File file, long updatePeriod)Begin monitoring a file for changes.voiddispose()Removes all listeners being monitored and ends the monitoring thread.static longgetDefaultCheckPeriod()Returns a hint describing how often files should be checked for changes by default.static FileChangeMonitorgetSharedInstance()Return a shared instance ofFileChangeMonitor.voidpurge()Remove all monitor tasks that are no longer valid because the listener was garbage collected without removing itself.voidremoveFileChangeListener(FileChangeListener listener)Stop monitoring all files that are registered with a listener.voidremoveFileChangeListener(FileChangeListener listener, java.io.File file)Stop monitoring a specific file registered with this listener.static voidsetDefaultCheckPeriod(long periodInMS)Sets a hint describing how often files should be checked for changes by default.
-
-
-
Constructor Detail
-
FileChangeMonitor
public FileChangeMonitor()
Creates a new file change monitor which is independent of the shared instance. It is generally preferable to use the shared instance.- See Also:
getSharedInstance()
-
-
Method Detail
-
getSharedInstance
public static FileChangeMonitor getSharedInstance()
Return a shared instance ofFileChangeMonitor.- Returns:
- an shared instance
-
setDefaultCheckPeriod
public static void setDefaultCheckPeriod(long periodInMS)
Sets a hint describing how often files should be checked for changes by default. Note that this class makes no guarantee about how it monitors files for changes, so this value may have no effect.- Parameters:
periodInMS- the default check period, in milliseconds
-
getDefaultCheckPeriod
public static long getDefaultCheckPeriod()
Returns a hint describing how often files should be checked for changes by default. Note that this class makes no guarantee about how it monitors files for changes, so this value may have no effect. (It will only have an effect if the class must poll the file system for changes, and even then the exact delay between checks cannot be guaranteed.)- Returns:
- periodInMS the default check period, in milliseconds
-
dispose
public void dispose()
Removes all listeners being monitored and ends the monitoring thread. Once a monitor has been disposed, it cannot have any more listeners added. If this method is called on the shared instance, it will throw anUnsupportedOperationException.- Throws:
java.lang.UnsupportedOperationException- if called ongetSharedInstance()
-
addFileChangeListener
public void addFileChangeListener(FileChangeListener listener, java.io.File file)
Begin monitoring a file for changes. Changes will cause the listener'sFileChangeListener.fileChanged(java.io.File, ca.cgjennings.io.FileChangeMonitor.ChangeType)method to be called.- Parameters:
listener- the listener to notify whenfilechangesfile- the file to monitor- Throws:
java.lang.NullPointerException- iflistenerorfileisnulljava.lang.IllegalStateException- if this monitor has beendispose()d
-
addFileChangeListener
public void addFileChangeListener(FileChangeListener listener, java.io.File file, long updatePeriod)
Begin monitoring a file for changes. Changes will cause the listener'sFileChangeListener.fileChanged(java.io.File, ca.cgjennings.io.FileChangeMonitor.ChangeType)method to be called. The value ofupdatePeriodis an approximate maximum delay, in milliseconds, before a change in file state is detected. The monitor does not guarantee thatupdatePeriodwill be honoured. Iflistenerhas already been registered to monitorfile, the existing monitor will be removed and it will be replaced by one with the newupdatePeriod. If theupdatePeriodis 0, then the monitor is not added (it is removed if it already exists).- Parameters:
listener- the listener to notify whenfilechangesfile- the file to monitorupdatePeriod- the approximate maximum delay before change notification takes place- Throws:
java.lang.NullPointerException- iflistenerorfileisnulljava.lang.IllegalArgumentException- ifupdatePeriodis negativejava.lang.IllegalStateException- if this monitor has beendispose()d of- See Also:
getDefaultCheckPeriod()
-
removeFileChangeListener
public void removeFileChangeListener(FileChangeListener listener, java.io.File file)
Stop monitoring a specific file registered with this listener. Note that the listener might be called one more time after this method is called if a file change was already being handled when the method was called.- Parameters:
listener- the listener to stop notifyingfile- the file to stop notifying the listener about
-
removeFileChangeListener
public void removeFileChangeListener(FileChangeListener listener)
Stop monitoring all files that are registered with a listener. This is more efficient than removing each file individually.- Parameters:
listener- the listener to stop monitoring
-
purge
public void purge()
Remove all monitor tasks that are no longer valid because the listener was garbage collected without removing itself. This may be done automatically from time to time, but you can force it to occur at any time by calling this method.
-
-