Package com.technicjelle
Class UpdateChecker
- java.lang.Object
-
- com.technicjelle.UpdateChecker
-
public class UpdateChecker extends Object
Checks for updates on a GitHub repository
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheck()Checks for updates from a GitHub repository's releases
This method blocks the thread it is called fromvoidcheckAsync()Checks for updates from a GitHub repository's releases
This method does not block the thread it is called fromStringgetCurrentVersion()Gets the current version of the program.
Useful in case you want to log a custom message.
Does not actually check for updatesStringgetLatestVersion()Checks if necessary and returns the latest available versionOptional<String>getUpdateMessage()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.
I recommend that you use one of the log methods if you want to log the update message:logUpdateMessage(Logger)logUpdateMessageAsync(Logger)logUpdateMessage(Consumer)logUpdateMessageAsync(Consumer)But this function remains available in case you want to use it manually.
Example message:
New version available: v2.5 (current: v2.4)
Download it at https://github.com/TechnicJelle/UpdateCheckerJava/releases/latestStringgetUpdateUrl()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 updatesURLgetUpdateURL()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 updatesbooleanisUpdateAvailable()Checks if necessary and returns whether an update is available or notvoidlogUpdateMessage(@NotNull Consumer<String> logger)This method logs a message to the console if an update is available
Useful if you don't use Java's ownLoggerand you want to use your own.voidlogUpdateMessage(Logger logger)This method logs a message to the console if an update is availablevoidlogUpdateMessageAsync(@NotNull Consumer<String> logger)This method logs a message to the console if an update is available, asynchronously
Useful if you don't use Java's ownLoggerand you want to use your own.voidlogUpdateMessageAsync(Logger logger)This method logs a message to the console if an update is available, asynchronouslystatic @NotNull Stringtrace(@NotNull Throwable throwable)Convenience function to convert a Throwable to a String, including the stacktrace.
-
-
-
Constructor Detail
-
UpdateChecker
public UpdateChecker(@NotNull @NotNull String author, @NotNull @NotNull String repoName, @NotNull @NotNull String currentVersion, @NotNull @NotNull Consumer<Throwable> onError)Start the program with-Dtechnicjelle.updatechecker.disabledto disable the update checker- Parameters:
author- GitHub UsernamerepoName- GitHub Repository NamecurrentVersion- Current version of the program. This must be in the same format as the version tags on GitHubonError- Called when an internal error occurs. Use this to log the error using your logging system
-
-
Method Detail
-
check
public void check()
Checks for updates from a GitHub repository's releases
This method blocks the thread it is called from- See Also:
checkAsync()
-
checkAsync
public void checkAsync()
Checks for updates from a GitHub repository's releases
This method does not block the thread it is called from- See Also:
check()
-
getLatestVersion
public String getLatestVersion()
Checks if necessary and returns the latest available version- Returns:
- the latest available version
-
isUpdateAvailable
public boolean isUpdateAvailable()
Checks if necessary and returns whether an update is available or not- Returns:
trueif there is an update available orfalseotherwise.
-
getUpdateMessage
public Optional<String> getUpdateMessage()
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.
I recommend that you use one of the log methods if you want to log the update message:logUpdateMessage(Logger)logUpdateMessageAsync(Logger)logUpdateMessage(Consumer)logUpdateMessageAsync(Consumer)
Example message:
New version available: v2.5 (current: v2.4)
Download it at https://github.com/TechnicJelle/UpdateCheckerJava/releases/latest- Returns:
- An optional containing the update message or an empty optional if there is no update available
-
logUpdateMessage
public void logUpdateMessage(@NotNull Logger logger)This method logs a message to the console if an update is available- Parameters:
logger- Logger to log a potential update notification to- See Also:
logUpdateMessageAsync(Logger),logUpdateMessage(Consumer),logUpdateMessageAsync(Consumer)
-
logUpdateMessageAsync
public void logUpdateMessageAsync(@NotNull Logger logger)This method logs a message to the console if an update is available, asynchronously- Parameters:
logger- Logger to log a potential update notification to- See Also:
logUpdateMessage(Logger),logUpdateMessage(Consumer),logUpdateMessageAsync(Consumer)
-
logUpdateMessage
public void logUpdateMessage(@NotNull @NotNull Consumer<String> logger)This method logs a message to the console if an update is available
Useful if you don't use Java's ownLoggerand you want to use your own.- Parameters:
logger- Logger to log a potential update notification to- See Also:
logUpdateMessage(Logger),logUpdateMessageAsync(Logger),logUpdateMessageAsync(Consumer)
-
logUpdateMessageAsync
public void logUpdateMessageAsync(@NotNull @NotNull Consumer<String> logger)This method logs a message to the console if an update is available, asynchronously
Useful if you don't use Java's ownLoggerand you want to use your own.- Parameters:
logger- Logger to log a potential update notification to- See Also:
logUpdateMessage(Logger),logUpdateMessageAsync(Logger),logUpdateMessage(Consumer)
-
getCurrentVersion
public String getCurrentVersion()
Gets the current version of the program.
Useful in case you want to log a custom message.
Does not actually check for updates- Returns:
- The current version of the program
-
getUpdateURL
public URL getUpdateURL()
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- Returns:
- The URL to the GitHub releases page
- See Also:
getUpdateUrl()
-
getUpdateUrl
public String getUpdateUrl()
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- Returns:
- The URL (as a String) to the GitHub releases page
- See Also:
getUpdateURL()
-
trace
@Contract(pure=true) @NotNull public static @NotNull String trace(@NotNull @NotNull Throwable throwable)
Convenience function to convert a Throwable to a String, including the stacktrace. Useful for when logging it.- Parameters:
throwable- The Throwable to convert to a String- Returns:
- The converted String
-
-