From d2e4500af3d39a18cca44df690b5687c001c0476 Mon Sep 17 00:00:00 2001 From: Asger Gitz-Johansen Date: Thu, 18 Apr 2024 07:26:14 +0200 Subject: [PATCH] docs: add javadocs to download utility --- .../java/dk/gtz/graphedit/util/Download.java | 69 ++++++++++++++++++- .../view/PluginManagementPanelController.java | 3 +- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/dk/gtz/graphedit/util/Download.java b/core/src/main/java/dk/gtz/graphedit/util/Download.java index 81170e2c..78c4b972 100644 --- a/core/src/main/java/dk/gtz/graphedit/util/Download.java +++ b/core/src/main/java/dk/gtz/graphedit/util/Download.java @@ -9,13 +9,34 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * Helper class for downloading files and getting the progress of the download. + */ public class Download implements Runnable { private static final Logger logger = LoggerFactory.getLogger(Download.class); + /** + * The state that the download can be in. + */ public static enum State { + /** + * The download is currently in progress. + */ DOWNLOADING, + /** + * The download is paused. Call {@link Download#resume} to resume. + */ PAUSED, + /** + * The download is complete. Call {@link Download#downloadedFile} to get the downloaded file. + */ COMPLETE, + /** + * The download was cancelled. Discard the {@link Download} instance and start again. + */ CANCELLED, + /** + * An error occurred during the download. Check the logs for more information. + */ ERROR } private static final int MAX_BUFFER_SIZE = 1024; @@ -26,6 +47,10 @@ public static enum State { private Runnable onStateChange; private Optional downloadedFilepath; + /** + * Create a new download instance. + * @param url The URL to download from. + */ public Download(URL url) { this.url = url; this.onStateChange = () -> {}; @@ -34,37 +59,67 @@ public Download(URL url) { downloaded = 0; } + /** + * Set the callback that will be called when the state of the download changes. + * This includes when some progress has been made. + * @param onStateChange The callback to call. + */ public void setOnStateChanged(Runnable onStateChange) { this.onStateChange = onStateChange; } + /** + * Get the URL that is being downloaded from. + * @return The URL. + */ public String getUrl() { return url.toString(); } + /** + * Get the size of the file being downloaded. + * @return The size in bytes. + */ public int getSize() { return size; } + /** + * Get the current percentage [0.0 - 100.0] of the download. + * @return The percentage of completion. + */ public float getProgress() { return ((float) downloaded / size) * 100; } + /** + * Get the current state of the download. + * @return The state of the download. + */ public State getStatus() { return status; } + /** + * Pause the download. Call {@link Download#resume} to resume. + */ public void pause() { status = State.PAUSED; stateChanged(); } + /** + * Resume the download if it was paused. + */ public void resume() { status = State.DOWNLOADING; stateChanged(); download(); } + /** + * Cancel the download. Discard the this instance and start again. + */ public void cancel() { status = State.CANCELLED; stateChanged(); @@ -76,16 +131,28 @@ private void error(String message) { stateChanged(); } + /** + * Start the download. + */ public void download() { status = State.DOWNLOADING; new Thread(this).start(); } - public String getFileName(URL url) { + /** + * Get the file name from a URL. + * @param url The URL to get the file name from. + * @return The file name. + */ + public static String getFileName(URL url) { var fileName = url.getFile(); return fileName.substring(fileName.lastIndexOf('/') + 1); } + /** + * Get the downloaded file path. + * @return The downloaded file path. + */ public Optional downloadedFile() { return downloadedFilepath; } diff --git a/std/src/main/java/dk/gtz/graphedit/plugins/view/PluginManagementPanelController.java b/std/src/main/java/dk/gtz/graphedit/plugins/view/PluginManagementPanelController.java index e582833c..f2ecfe86 100644 --- a/std/src/main/java/dk/gtz/graphedit/plugins/view/PluginManagementPanelController.java +++ b/std/src/main/java/dk/gtz/graphedit/plugins/view/PluginManagementPanelController.java @@ -6,7 +6,6 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; @@ -168,7 +167,7 @@ private void downloadPlugin(Button button, PluginDbEntry entry) { button.setText("download paused"); break; case COMPLETE: - var filepath = String.join(File.separator, EditorActions.getConfigDir(), "plugins", d.getFileName(url)); + var filepath = String.join(File.separator, EditorActions.getConfigDir(), "plugins", Download.getFileName(url)); Files.copy(Path.of(d.downloadedFile().get()), Path.of(filepath), StandardCopyOption.REPLACE_EXISTING); case CANCELLED: default: