final File thisFile = entry.getKey();
final Long currentTimestamp = entry.getValue();
if (!priorFiles.containsKey(thisFile)) {
// This file did not exist last execution, so it
// must be new
eventsToPublish.add(new FileEvent(new FileDetails(
thisFile, currentTimestamp),
FileOperation.CREATED, null));
try {
// If this file was already going to be
// notified, there is no need to do it twice
notifyCreated.remove(thisFile
.getCanonicalPath());
}
catch (final IOException ignored) {
}
continue;
}
final Long previousTimestamp = priorFiles.get(thisFile);
if (!currentTimestamp.equals(previousTimestamp)) {
// Modified
eventsToPublish.add(new FileEvent(new FileDetails(
thisFile, currentTimestamp),
FileOperation.UPDATED, null));
try {
// If this file was already going to be
// notified, there is no need to do it twice
notifyChanged.remove(thisFile
.getCanonicalPath());
}
catch (final IOException ignored) {
}
}
}
// Now locate deleted files
priorFiles.keySet().removeAll(currentExecution.keySet());
for (final Entry<File, Long> entry : priorFiles.entrySet()) {
final File deletedFile = entry.getKey();
eventsToPublish.add(new FileEvent(new FileDetails(
deletedFile, entry.getValue()),
FileOperation.DELETED, null));
try {
// If this file was already going to be notified,
// there is no need to do it twice
notifyDeleted
.remove(deletedFile.getCanonicalPath());
}
catch (final IOException ignored) {
}
}
}
else {
// No data from previous execution, so it's a
// newly-monitored location
for (final Entry<File, Long> entry : currentExecution
.entrySet()) {
eventsToPublish.add(new FileEvent(new FileDetails(entry
.getKey(), entry.getValue()),
FileOperation.MONITORING_START, null));
}
}
// Record the monitored location's contents, ready for next
// execution
priorExecution.put(request, currentExecution);
// We can discard the created and deleted notifications, as they
// would have been correctly discovered in the above loop
notifyCreated.clear();
notifyDeleted.clear();
// Explicitly handle any undiscovered update notifications, as
// this indicates an identical millisecond update occurred
for (final String canonicalPath : notifyChanged) {
final File file = new File(canonicalPath);
eventsToPublish.add(new FileEvent(new FileDetails(file,
file.lastModified()), FileOperation.UPDATED, null));
}
notifyChanged.clear();
publish(eventsToPublish);