Skip to content

Commit d062780

Browse files
committed
v2, to allow for multiple instances
1 parent f6c6d23 commit d062780

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ A simple Java update checker for your application.
55
Visit https://jitpack.io/#TechnicJelle/UpdateCheckerJava for details on how to install this library.
66

77
## Usage
8-
This library is very simple to use. Just call the `UpdateChecker.check()` method with your GitHub username,
9-
repository name and current version to check for updates.
8+
Simply instantiate a new `UpdateChecker` object with your GitHub username, repository name and current version.
109

11-
You can then log the update message with `UpdateChecker.logUpdateMessage()`.
10+
Then call `.check()` or `.checkAsync()` on the instance to check for updates.
1211

13-
There is also an async way to check for updates: `UpdateChecker.checkAsync()`.
12+
You can then log the update message with `logUpdateMessage()`.
1413

1514
```java
16-
UpdateChecker.check("TechnicJelle", "UpdateChecker", "v1.0");
17-
18-
UpdateChecker.logUpdateMessage(getLogger());
15+
UpdateChecker updateChecker = new UpdateChecker("TechnicJelle", "UpdateCheckerJava", "2.0");
16+
updateChecker.check();
17+
updateChecker.logUpdateMessage(getLogger());
1918
```
2019

21-
Full javadoc API reference: [technicjelle.com/UpdateCheckerJava](https://technicjelle.com/UpdateCheckerJava/com/technicjelle/UpdateChecker.html)
20+
Full javadoc API reference: [technicjelle.com/UpdateCheckerJava](https://technicjelle.com/UpdateCheckerJava/com/technicjelle/UpdateChecker.html)

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.technicjelle</groupId>
88
<artifactId>UpdateChecker</artifactId>
9-
<version>1.0</version>
9+
<version>2.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>11</maven.compiler.source>

src/main/java/com/technicjelle/UpdateChecker.java

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,40 @@
66
import java.io.IOException;
77
import java.net.HttpURLConnection;
88
import java.net.URL;
9+
import java.net.MalformedURLException;
910
import java.util.logging.Logger;
1011

1112
/**
1213
* Checks for updates on a GitHub repository
1314
*/
14-
public final class UpdateChecker {
15-
private static boolean updateAvailable = false;
16-
private static URL url = null;
17-
private static String latestVersion = null;
18-
private static String curVer = null;
15+
public class UpdateChecker {
16+
private final String repoName;
17+
private final String currentVersion;
18+
private final URL url;
19+
20+
private boolean updateAvailable = false;
21+
private String latestVersion = null;
22+
23+
/**
24+
* @param author GitHub Username
25+
* @param repoName GitHub Repository Name
26+
* @param currentVersion Current version of the program. This must be in the same format as the version tags on GitHub
27+
* @throws MalformedURLException If the URL is invalid, due to an invalid author and/or name
28+
*/
29+
public UpdateChecker(@NotNull String author, @NotNull String repoName, @NotNull String currentVersion) throws MalformedURLException {
30+
this.repoName = repoName;
31+
this.currentVersion = removePrefix(currentVersion);
32+
this.url = new URL("https://github.com/" + author + "/" + repoName + "/releases/latest");
33+
}
1934

2035
/**
2136
* Checks for updates from a GitHub repository's releases<br>
2237
* <i>This method blocks the thread it is called from</i>
2338
*
24-
* @param author GitHub Username
25-
* @param name GitHub Repository Name
26-
* @param currentVersion Current version of the program. This must be in the same format as the version tags on GitHub
2739
* @throws IOException If an IO exception occurs
28-
* @see #checkAsync(String, String, String)
40+
* @see #checkAsync()
2941
*/
30-
public static void check(@NotNull String author, @NotNull String name, @NotNull String currentVersion) throws IOException {
31-
curVer = removePrefix(currentVersion);
32-
url = new URL("https://github.com/" + author + "/" + name + "/releases/latest");
33-
42+
public void check() throws IOException {
3443
// Connect to GitHub website
3544
HttpURLConnection con;
3645
con = (HttpURLConnection) url.openConnection();
@@ -48,39 +57,35 @@ public static void check(@NotNull String author, @NotNull String name, @NotNull
4857
latestVersion = removePrefix(split[split.length - 1]);
4958

5059
// Check if the latest version is not the current version
51-
if (!latestVersion.equals(curVer)) updateAvailable = true;
60+
if (!latestVersion.equals(currentVersion)) updateAvailable = true;
5261
}
5362

5463
/**
5564
* Checks for updates from a GitHub repository's releases<br>
5665
* <i>This method does not block the thread it is called from</i>
5766
*
58-
* @param author GitHub Username
59-
* @param name GitHub Repository Name
60-
* @param currentVersion Current version of the program. This must be in the same format as the version tags on GitHub
61-
* @see #check(String, String, String)
67+
* @see #check()
6268
*/
63-
public static void checkAsync(@NotNull String author, @NotNull String name, @NotNull String currentVersion) {
64-
curVer = removePrefix(currentVersion);
69+
public void checkAsync() {
6570
new Thread(() -> {
6671
try {
67-
check(author, name, currentVersion);
72+
check();
6873
} catch (IOException e) {
6974
throw new RuntimeException(e);
7075
}
71-
}, name + "-Update-Checker").start();
76+
}, repoName + "-Update-Checker").start();
7277
}
7378

7479
/**
7580
* This method logs a message to the console if an update is available<br>
7681
*
7782
* @param logger Logger to log a potential update notification to
78-
* @throws IllegalStateException If {@link #check(String, String, String)} has not been called
83+
* @throws IllegalStateException If {@link #check()} has not been called
7984
*/
80-
public static void logUpdateMessage(Logger logger) throws IllegalStateException {
81-
if (curVer == null) throw new IllegalStateException("UpdateChecker.check() has not been called");
85+
public void logUpdateMessage(@NotNull Logger logger) throws IllegalStateException {
86+
if (latestVersion == null) throw new IllegalStateException("check() has not been called");
8287
if (updateAvailable) {
83-
logger.warning("New version available: v" + latestVersion + " (current: v" + curVer + ")");
88+
logger.warning("New version available: v" + latestVersion + " (current: v" + currentVersion + ")");
8489
logger.warning("Download it at " + url);
8590
}
8691
}
@@ -95,8 +100,4 @@ public static void logUpdateMessage(Logger logger) throws IllegalStateException
95100
private static @NotNull String removePrefix(@NotNull String version) {
96101
return version.replaceFirst("^v", "");
97102
}
98-
99-
private UpdateChecker() {
100-
throw new IllegalStateException("Utility class");
101-
}
102103
}

0 commit comments

Comments
 (0)