diff --git a/src/main/java/musicplayer/LibraryUtils.java b/src/main/java/musicplayer/LibraryUtils.java index 5eb1d00..0eaf0c6 100644 --- a/src/main/java/musicplayer/LibraryUtils.java +++ b/src/main/java/musicplayer/LibraryUtils.java @@ -22,24 +22,42 @@ import java.util.stream.Collectors; public final class LibraryUtils { static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)"; + static Thread updaterThread; + /** + * Add all songs contained with all paths in rootDirectory to the database. + * @param rootDirectory Folder(s) containing music files to index. + * @param callbackInterface Object to send callback data to, set to null if the application doesn't require update data. + */ public static void processSongsWithCallback(List rootDirectory, LibraryCallbackInterface callbackInterface){ - Thread thread = new Thread(() -> { + boolean callbacksEnabled = callbackInterface != null; + updaterThread = new Thread(() -> { rootDirectory.forEach(dir -> { try { Files.walk(dir) .filter(f -> f.toString().matches(musicFileExtensionRegex)).map(LibraryUtils::autoParse) .forEach(x -> x.ifPresent(i -> { Gateway.addSong(i); - callbackInterface.currentlyUpdating(i.toString()); + if(callbacksEnabled) + callbackInterface.currentlyUpdating(i.toString()); })); } catch (IOException e) { - callbackInterface.libraryUpdated(false); + if(callbacksEnabled) + callbackInterface.libraryUpdated(false); } }); - callbackInterface.libraryUpdated(true); + if(callbacksEnabled) + callbackInterface.libraryUpdated(true); }); - thread.start(); + updaterThread.start(); + } + + /** + * Halt execution of the library updater thread (if running) + */ + public static void cancelProcessing(){ + if(updaterThread.isAlive()) + updaterThread.interrupt(); } /**