6
6
import java .io .IOException ;
7
7
import java .net .HttpURLConnection ;
8
8
import java .net .URL ;
9
+ import java .net .MalformedURLException ;
9
10
import java .util .logging .Logger ;
10
11
11
12
/**
12
13
* Checks for updates on a GitHub repository
13
14
*/
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
+ }
19
34
20
35
/**
21
36
* Checks for updates from a GitHub repository's releases<br>
22
37
* <i>This method blocks the thread it is called from</i>
23
38
*
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
27
39
* @throws IOException If an IO exception occurs
28
- * @see #checkAsync(String, String, String )
40
+ * @see #checkAsync()
29
41
*/
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 {
34
43
// Connect to GitHub website
35
44
HttpURLConnection con ;
36
45
con = (HttpURLConnection ) url .openConnection ();
@@ -48,39 +57,35 @@ public static void check(@NotNull String author, @NotNull String name, @NotNull
48
57
latestVersion = removePrefix (split [split .length - 1 ]);
49
58
50
59
// Check if the latest version is not the current version
51
- if (!latestVersion .equals (curVer )) updateAvailable = true ;
60
+ if (!latestVersion .equals (currentVersion )) updateAvailable = true ;
52
61
}
53
62
54
63
/**
55
64
* Checks for updates from a GitHub repository's releases<br>
56
65
* <i>This method does not block the thread it is called from</i>
57
66
*
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()
62
68
*/
63
- public static void checkAsync (@ NotNull String author , @ NotNull String name , @ NotNull String currentVersion ) {
64
- curVer = removePrefix (currentVersion );
69
+ public void checkAsync () {
65
70
new Thread (() -> {
66
71
try {
67
- check (author , name , currentVersion );
72
+ check ();
68
73
} catch (IOException e ) {
69
74
throw new RuntimeException (e );
70
75
}
71
- }, name + "-Update-Checker" ).start ();
76
+ }, repoName + "-Update-Checker" ).start ();
72
77
}
73
78
74
79
/**
75
80
* This method logs a message to the console if an update is available<br>
76
81
*
77
82
* @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
79
84
*/
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" );
82
87
if (updateAvailable ) {
83
- logger .warning ("New version available: v" + latestVersion + " (current: v" + curVer + ")" );
88
+ logger .warning ("New version available: v" + latestVersion + " (current: v" + currentVersion + ")" );
84
89
logger .warning ("Download it at " + url );
85
90
}
86
91
}
@@ -95,8 +100,4 @@ public static void logUpdateMessage(Logger logger) throws IllegalStateException
95
100
private static @ NotNull String removePrefix (@ NotNull String version ) {
96
101
return version .replaceFirst ("^v" , "" );
97
102
}
98
-
99
- private UpdateChecker () {
100
- throw new IllegalStateException ("Utility class" );
101
- }
102
103
}
0 commit comments