/*
- * jSite - FileScanner.java - Copyright © 2006–2014 David Roden
+ * jSite - FileScanner.java - Copyright © 2006–2019 David Roden
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
* files as an event.
*
* @see Project#getLocalPath()
- * @see FileScannerListener#fileScannerFinished(FileScanner)
+ * @see FileScannerListener#fileScannerFinished(boolean, java.util.Collection)
* @author David ‘Bombe’ Roden <bombe@freenetproject.org>
*/
public class FileScanner implements Runnable {
private final static Logger logger = Logger.getLogger(FileScanner.class.getName());
/** The list of listeners. */
- private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
+ private final FileScannerListener fileScannerListener;
/** The project to scan. */
private final Project project;
* @param project
* The project whose files to scan
*/
- public FileScanner(Project project) {
+ public FileScanner(Project project, FileScannerListener fileScannerListener) {
this.project = project;
- }
-
- /**
- * Adds the given listener to the list of listeners.
- *
- * @param fileScannerListener
- * The listener to add
- */
- public void addFileScannerListener(FileScannerListener fileScannerListener) {
- fileScannerListeners.add(fileScannerListener);
- }
-
- /**
- * Removes the given listener from the list of listeners.
- *
- * @param fileScannerListener
- * The listener to remove
- */
- public void removeFileScannerListener(FileScannerListener fileScannerListener) {
- fileScannerListeners.remove(fileScannerListener);
- }
-
- /**
- * Notifies all listeners that the file scan finished.
- */
- protected void fireFileScannerFinished() {
- for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
- fileScannerListener.fileScannerFinished(this);
- }
+ this.fileScannerListener = Objects.requireNonNull(fileScannerListener);
}
/**
* Scans all available files in the project’s local path and emits an event
* when finished.
*
- * @see FileScannerListener#fileScannerFinished(FileScanner)
+ * @see FileScannerListener#fileScannerFinished(boolean, java.util.Collection)
*/
@Override
public void run() {
} catch (IOException ioe1) {
error = true;
}
- fireFileScannerFinished();
+ fileScannerListener.fileScannerFinished(error, files);
}
/**
return hexString.toString();
}
- /**
- * Container for a scanned file, consisting of the name of the file and its
- * hash.
- *
- * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
- */
- public static class ScannedFile implements Comparable<ScannedFile> {
-
- /** The name of the file. */
- private final String filename;
-
- /** The hash of the file. */
- private final String hash;
-
- /**
- * Creates a new scanned file.
- *
- * @param filename
- * The name of the file
- * @param hash
- * The hash of the file
- */
- public ScannedFile(String filename, String hash) {
- this.filename = filename;
- this.hash = hash;
- }
-
- //
- // ACCESSORS
- //
-
- /**
- * Returns the name of the file.
- *
- * @return The name of the file
- */
- public String getFilename() {
- return filename;
- }
-
- /**
- * Returns the hash of the file.
- *
- * @return The hash of the file
- */
- public String getHash() {
- return hash;
- }
-
- //
- // OBJECT METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- return filename.hashCode();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(Object obj) {
- return filename.equals(obj);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- return filename;
- }
-
- //
- // COMPARABLE METHODS
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int compareTo(ScannedFile scannedFile) {
- return filename.compareTo(scannedFile.filename);
- }
-
- }
-
}