Skip to content

Commit d8bbe46

Browse files
authored
Merge pull request #2 from HyperaOfficial/2.1.0-SNAPSHOT
2.1.0-SNAPSHOT
2 parents 181a57b + 2daa506 commit d8bbe46

File tree

7 files changed

+274
-173
lines changed

7 files changed

+274
-173
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea/
22
*.iml
33
target/
4-
dependency-reduced-pom.xml
4+
dependency-reduced-pom.xml
5+
pom.xml.versionsBackup

pom.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<groupId>dev.hypera</groupId>
2424
<artifactId>UpdateLib</artifactId>
25-
<version>2.0.0-SNAPSHOT</version>
25+
<version>2.1.0-SNAPSHOT</version>
2626
<packaging>jar</packaging>
2727

2828
<name>UpdateLib</name>
@@ -101,9 +101,13 @@
101101
<groupId>org.apache.maven.plugins</groupId>
102102
<artifactId>maven-deploy-plugin</artifactId>
103103
<version>2.8.2</version>
104-
<configuration>
105-
<goals>deploy</goals>
106-
</configuration>
104+
<executions>
105+
<execution>
106+
<goals>
107+
<goal>deploy</goal>
108+
</goals>
109+
</execution>
110+
</executions>
107111
</plugin>
108112
</plugins>
109113
</build>

src/main/java/dev/hypera/updatelib/UpdateLibBuilder.java

Lines changed: 79 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,94 @@
1717
package dev.hypera.updatelib;
1818

1919
import dev.hypera.updatelib.internal.UpdateLib;
20+
import dev.hypera.updatelib.internal.UpdateResponse;
2021

22+
import java.util.function.Consumer;
23+
24+
@SuppressWarnings("unused")
2125
public class UpdateLibBuilder {
2226

23-
private final long resourceId;
24-
private final String currentVersion;
27+
private final long resourceId;
28+
private final String currentVersion;
29+
30+
private boolean repeatingChecksEnabled = true;
31+
private long checkInterval = 2 * (60 * (60 * 1000)); // 2 Hours
32+
private int connectionTimeout = 5000; // 5 Seconds
33+
34+
private Consumer<UpdateResponse> consumer = null;
2535

26-
private boolean repeatingChecksEnabled = true;
27-
private long checkInterval = 2 * (60 * (60 * 1000)); // 2 Hours
28-
private int connectionTimeout = 5000; // 5 Seconds
36+
private UpdateLibBuilder(String currentVersion, long resourceId) {
37+
this.currentVersion = currentVersion;
38+
this.resourceId = resourceId;
39+
}
2940

30-
private UpdateLibBuilder(String currentVersion, long resourceId) {
31-
this.currentVersion = currentVersion;
32-
this.resourceId = resourceId;
33-
}
41+
/**
42+
* Creates a new instance of {@link UpdateLibBuilder}.
43+
*
44+
* @param currentVersion Current version of the resource.
45+
* @param resourceId SpigotMC Resource Id.
46+
*
47+
* @return Instance of {@link UpdateLibBuilder}
48+
* @since 2.0.0-SNAPSHOT
49+
*/
50+
public static UpdateLibBuilder create(String currentVersion, long resourceId) {
51+
return new UpdateLibBuilder(currentVersion, resourceId);
52+
}
3453

35-
/**
36-
* Creates a new instance of {@link UpdateLibBuilder}.
37-
*
38-
* @param currentVersion Current version of the resource.
39-
* @param resourceId SpigotMC Resource Id.
40-
* @return Instance of {@link UpdateLibBuilder}
41-
*/
42-
public static UpdateLibBuilder create(String currentVersion, long resourceId) {
43-
return new UpdateLibBuilder(currentVersion, resourceId);
44-
}
54+
/**
55+
* Should UpdateLib keep checking for updates? (Time defined by checkInterval)
56+
*
57+
* @param enabled Repeating checks enabled.
58+
*
59+
* @see #setCheckInterval(long)
60+
* @since 2.0.0-SNAPSHOT
61+
*/
62+
public void setRepeatingChecksEnabled(boolean enabled) {
63+
this.repeatingChecksEnabled = enabled;
64+
}
4565

46-
/**
47-
* Should UpdateLib keep checking for updates? (Time defined by checkInterval)
48-
*
49-
* @param enabled Repeating checks enabled.
50-
* @see #setCheckInterval(long)
51-
*/
52-
public void setRepeatingChecksEnabled(boolean enabled) {
53-
this.repeatingChecksEnabled = enabled;
54-
}
66+
/**
67+
* How often should UpdateLib check for updates? (Only works if repeatingChecksEnabled is true)
68+
*
69+
* @param interval Interval in milliseconds.
70+
*
71+
* @see #setRepeatingChecksEnabled(boolean)
72+
* @since 2.0.0-SNAPSHOT
73+
*/
74+
public void setCheckInterval(long interval) {
75+
this.checkInterval = interval;
76+
}
5577

56-
/**
57-
* How often should UpdateLib check for updates? (Only works if repeatingChecksEnabled is true)
58-
*
59-
* @param interval Interval in milliseconds.
60-
* @see #setRepeatingChecksEnabled(boolean)
61-
*/
62-
public void setCheckInterval(long interval) {
63-
this.checkInterval = interval;
64-
}
78+
/**
79+
* After how many milliseconds should we timeout the request to SpigotMC's API?
80+
*
81+
* @param timeout Timeout in milliseconds.
82+
*
83+
* @since 2.0.0-SNAPSHOT
84+
*/
85+
public void setConnectionTimeout(int timeout) {
86+
this.connectionTimeout = timeout;
87+
}
6588

66-
/**
67-
* After how many milliseconds should we timeout the request to SpigotMC's API?
68-
*
69-
* @param timeout Timeout in milliseconds.
70-
*/
71-
public void setConnectionTimeout(int timeout) {
72-
this.connectionTimeout = timeout;
73-
}
89+
/**
90+
* Sets a consumer to run after UpdateLib has checked for an update.
91+
*
92+
* @param consumer Consumer to run after checking for an update.
93+
*
94+
* @since 2.1.0-SNAPSHOT
95+
*/
96+
public void setConsumer(Consumer<UpdateResponse> consumer) {
97+
this.consumer = consumer;
98+
}
7499

75-
/**
76-
* Builds a new instance of {@link UpdateLib}.
77-
*
78-
* @return Instance of {@link UpdateLib}
79-
*/
80-
public UpdateLib build() {
81-
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout);
82-
}
100+
/**
101+
* Builds a new instance of {@link UpdateLib}.
102+
*
103+
* @return Instance of {@link UpdateLib}
104+
* @since 2.0.0-SNAPSHOT
105+
*/
106+
public UpdateLib build() {
107+
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout, consumer);
108+
}
83109

84110
}

src/main/java/dev/hypera/updatelib/internal/UpdateLib.java

Lines changed: 99 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,107 @@
2020
import dev.hypera.updatelib.internal.tasks.UpdateTask;
2121

2222
import java.util.Timer;
23+
import java.util.function.Consumer;
2324

2425
public class UpdateLib {
2526

26-
private final long resourceId;
27-
private final String currentVersion;
28-
private final int connectionTimeout;
29-
30-
private long lastCheck = 0L;
31-
private UpdateResponse lastResponse = null;
32-
33-
public UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long checkInterval, int connectionTimeout) {
34-
this.resourceId = resourceId;
35-
this.currentVersion = currentVersion;
36-
this.connectionTimeout = connectionTimeout;
37-
38-
Thread thread = new Thread(() -> {
39-
try {
40-
checkNow();
41-
} catch (Exception ex) {
42-
ex.printStackTrace();
43-
}
44-
});
45-
46-
thread.setName("UpdateLib-" + thread.getId());
47-
thread.start();
48-
49-
if(repeatingChecksEnabled) {
50-
new Timer().schedule(new UpdateTask(resourceId, currentVersion, connectionTimeout, this), checkInterval);
51-
}
52-
}
53-
54-
/**
55-
* Checks for a update now.
56-
*
57-
* @return Response (instance of {@link UpdateResponse}).
58-
* @throws Exception Any errors that occurred while checking.
59-
*/
60-
public UpdateResponse checkNow() throws Exception {
61-
lastCheck = System.currentTimeMillis();
62-
lastResponse = new UpdateChecker().check(resourceId, currentVersion, connectionTimeout);
63-
return lastResponse;
64-
}
65-
66-
/**
67-
* Get last {@link UpdateResponse} stored - If UpdateLib hasn't checked for updates yet, this will return null.
68-
*
69-
* @return Last {@link UpdateResponse} stored.
70-
*/
71-
public UpdateResponse getLastResponse() {
72-
return lastResponse;
73-
}
74-
75-
/**
76-
* Get the last time UpdateLib checked for an update.
77-
* @return Last time in milliseconds.
78-
*/
79-
public long getLastCheckTime() {
80-
return lastCheck;
81-
}
82-
83-
/**
84-
* Set last check time. - Used internally by UpdateLib.
85-
*
86-
* @param lastCheck Last check time in milliseconds.
87-
*/
88-
public void setLastCheck(long lastCheck) {
89-
this.lastCheck = lastCheck;
90-
}
91-
92-
/**
93-
* Set last response. - Used internally by UpdateLib.
94-
*
95-
* @param lastResponse Last response.
96-
*/
97-
public void setLastResponse(UpdateResponse lastResponse) {
98-
this.lastResponse = lastResponse;
99-
}
27+
private final static String version = "2.1.0-SNAPSHOT";
28+
29+
private final long resourceId;
30+
private final String currentVersion;
31+
private final int connectionTimeout;
32+
private final Consumer<UpdateResponse> consumer;
33+
34+
private long lastCheck = 0L;
35+
private UpdateResponse lastResponse = null;
36+
37+
public UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long checkInterval, int connectionTimeout, Consumer<UpdateResponse> consumer) {
38+
this.resourceId = resourceId;
39+
this.currentVersion = currentVersion;
40+
this.connectionTimeout = connectionTimeout;
41+
this.consumer = consumer;
42+
43+
Thread thread = new Thread(() -> {
44+
try {
45+
checkNow();
46+
} catch (Exception ex) {
47+
ex.printStackTrace();
48+
}
49+
});
50+
51+
thread.setName("UpdateLib-" + thread.getId());
52+
thread.start();
53+
54+
if(repeatingChecksEnabled) {
55+
new Timer().schedule(new UpdateTask(resourceId, currentVersion, connectionTimeout, consumer, this), checkInterval);
56+
}
57+
}
58+
59+
/**
60+
* Checks for a update now.
61+
*
62+
* @return Response (instance of {@link UpdateResponse}).
63+
* @throws Exception Any errors that occurred while checking.
64+
* @since 2.0.0-SNAPSHOT
65+
*/
66+
public UpdateResponse checkNow() throws Exception {
67+
lastCheck = System.currentTimeMillis();
68+
lastResponse = new UpdateChecker().check(resourceId, currentVersion, connectionTimeout);
69+
if(null != consumer)
70+
consumer.accept(getLastResponse());
71+
return lastResponse;
72+
}
73+
74+
/**
75+
* Get last {@link UpdateResponse} stored - If UpdateLib hasn't checked for updates yet, this will return null.
76+
*
77+
* @return Last {@link UpdateResponse} stored.
78+
* @since 2.0.0-SNAPSHOT
79+
*/
80+
public UpdateResponse getLastResponse() {
81+
return lastResponse;
82+
}
83+
84+
/**
85+
* Set last response. - Used internally by UpdateLib.
86+
*
87+
* @param lastResponse Last response.
88+
*
89+
* @since 2.0.0-SNAPSHOT
90+
*/
91+
public void setLastResponse(UpdateResponse lastResponse) {
92+
this.lastResponse = lastResponse;
93+
}
94+
95+
/**
96+
* Get the last time UpdateLib checked for an update.
97+
*
98+
* @return Last time in milliseconds.
99+
* @since 2.0.0-SNAPSHOT
100+
*/
101+
public long getLastCheckTime() {
102+
return lastCheck;
103+
}
104+
105+
/**
106+
* Set last check time. - Used internally by UpdateLib.
107+
*
108+
* @param lastCheck Last check time in milliseconds.
109+
*
110+
* @since 2.0.0-SNAPSHOT
111+
*/
112+
public void setLastCheck(long lastCheck) {
113+
this.lastCheck = lastCheck;
114+
}
115+
116+
/**
117+
* Get UpdateLib version.
118+
*
119+
* @return Version.
120+
* @since 2.1.0-SNAPSHOT
121+
*/
122+
public static String getVersion() {
123+
return version;
124+
}
100125

101126
}

0 commit comments

Comments
 (0)