From 7e0c9de4a3a78e06c007abfbe2c403d1db77200a Mon Sep 17 00:00:00 2001 From: TechnicJelle <22576047+TechnicJelle@users.noreply.github.com> Date: Mon, 17 Jun 2024 21:29:18 +0200 Subject: [PATCH] Add extra methods for if you don't use `java.util.logging` Or want to log custom update messages --- pom.xml | 2 +- .../java/com/technicjelle/UpdateChecker.java | 49 +++++++++++++++++-- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 1b4daf4..bdeb843 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.technicjelle UpdateChecker - 2.4 + 2.5 11 diff --git a/src/main/java/com/technicjelle/UpdateChecker.java b/src/main/java/com/technicjelle/UpdateChecker.java index b213131..286ee47 100644 --- a/src/main/java/com/technicjelle/UpdateChecker.java +++ b/src/main/java/com/technicjelle/UpdateChecker.java @@ -7,6 +7,7 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.logging.Logger; @@ -24,6 +25,7 @@ public class UpdateChecker { /** * Start the program with -Dtechnicjelle.updatechecker.disabled to disable the update checker + * * @param author GitHub Username * @param repoName GitHub Repository Name * @param currentVersion Current version of the program. This must be in the same format as the version tags on GitHub @@ -101,16 +103,55 @@ public boolean isUpdateAvailable() { return !getLatestVersion().equals(currentVersion); } + /** + * Checks if necessary and returns a message if an update is available.
+ * The message will contain the latest version and a link to the GitHub releases page.
+ * Useful if you don't use Java's own {@link java.util.logging.Logger} and you want to use your own.
+ * Example:
+ * New version available: v2.5 (current: v2.4)
+ * Download it at https://github.com/TechnicJelle/UpdateCheckerJava/releases/latest
+ * + * @return An optional containing the update message or an empty optional if there is no update available + */ + public Optional getUpdateMessage() { + if (isUpdateAvailable()) { + return Optional.of("New version available: v" + getLatestVersion() + " (current: v" + currentVersion + ")\nDownload it at " + url); + } + return Optional.empty(); + } + + /** + * Gets the current version of the program.
+ * Useful in case you want to log a custom message.
+ *
+ * Does not actually check for updates + * + * @return The current version of the program + */ + public String getCurrentVersion() { + return currentVersion; + } + + /** + * Gets the URL to the GitHub releases page, + * where the latest version can be downloaded.
+ * Useful in case you want to log a custom message.
+ *
+ * Does not actually check for updates + * + * @return The URL to the GitHub releases page + */ + public String getUpdateUrl() { + return url.toString(); + } + /** * This method logs a message to the console if an update is available
* * @param logger Logger to log a potential update notification to */ public void logUpdateMessage(@NotNull Logger logger) { - if (isUpdateAvailable()) { - logger.warning("New version available: v" + getLatestVersion() + " (current: v" + currentVersion + ")"); - logger.warning("Download it at " + url); - } + getUpdateMessage().ifPresent(logger::warning); } /**