Skip to content

Commit

Permalink
docs: add javadocs to download utility
Browse files Browse the repository at this point in the history
  • Loading branch information
sillydan1 committed Apr 18, 2024
1 parent 84b9c07 commit d2e4500
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
69 changes: 68 additions & 1 deletion core/src/main/java/dk/gtz/graphedit/util/Download.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +47,10 @@ public static enum State {
private Runnable onStateChange;
private Optional<String> downloadedFilepath;

/**
* Create a new download instance.
* @param url The URL to download from.
*/
public Download(URL url) {
this.url = url;
this.onStateChange = () -> {};
Expand All @@ -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();
Expand All @@ -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<String> downloadedFile() {
return downloadedFilepath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d2e4500

Please sign in to comment.