Skip to content

Commit 63e5b35

Browse files
committed
v3.0.0-SNAPSHOT
1 parent 3dc3b6a commit 63e5b35

23 files changed

+1161
-311
lines changed

pom.xml

Lines changed: 2 additions & 9 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.1.2</version>
25+
<version>3.0.0-SNAPSHOT</version>
2626
<packaging>jar</packaging>
2727

2828
<name>UpdateLib</name>
@@ -113,7 +113,7 @@
113113
</build>
114114

115115
<dependencies>
116-
<!-- To read and use the Spigot API -->
116+
<!-- JSON-Simple -->
117117
<dependency>
118118
<groupId>com.googlecode.json-simple</groupId>
119119
<artifactId>json-simple</artifactId>
@@ -126,13 +126,6 @@
126126
</exclusion>
127127
</exclusions>
128128
</dependency>
129-
130-
<!-- To compare semver -->
131-
<dependency>
132-
<groupId>org.apache.maven</groupId>
133-
<artifactId>maven-artifact</artifactId>
134-
<version>3.6.3</version>
135-
</dependency>
136129
</dependencies>
137130

138131
</project>
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2021 Joshua Sing <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
package dev.hypera.updatelib;
18+
19+
import dev.hypera.updatelib.annotations.RunAsync;
20+
import dev.hypera.updatelib.checkers.UpdateChecker;
21+
import dev.hypera.updatelib.data.CheckData;
22+
import dev.hypera.updatelib.objects.UpdateStatus;
23+
24+
import java.util.Timer;
25+
import java.util.TimerTask;
26+
import java.util.function.Consumer;
27+
28+
public class UpdateLib {
29+
30+
private final static String VERSION = "3.0.0-SNAPSHOT"; // Current UpdateLib version.
31+
32+
private final long resourceId;
33+
private final String currentVersion;
34+
private final int connectionTimeout;
35+
36+
private final UpdateChecker updateChecker;
37+
38+
private final Consumer<UpdateStatus> completeAction;
39+
private final Consumer<Throwable> errorHandler;
40+
41+
private UpdateStatus lastStatus = null;
42+
private long lastCheck = 0L;
43+
44+
/**
45+
* UpdateLib Constructor - Used internally by UpdateLib.
46+
*
47+
* @param resourceId Resource ID.
48+
* @param currentVersion Current version of the Resource.
49+
* @param repeatingChecksEnabled If UpdateLib should check for updates periodically.
50+
* @param interval How often UpdateLib should check for updates.
51+
* @param connectionTimeout The amount of milliseconds UpdateLib should wait before timing out on requests.
52+
* @param updateChecker {@link UpdateChecker} to be used by UpdateLib to check for updates.
53+
* @param completeAction Action to run after UpdateLib has checked for an update.
54+
* @param errorHandler Consumer to run if UpdateLib encounters an exception.
55+
*/
56+
protected UpdateLib(long resourceId, String currentVersion, boolean repeatingChecksEnabled, long interval, int connectionTimeout, UpdateChecker updateChecker, Consumer<UpdateStatus> completeAction, Consumer<Throwable> errorHandler) {
57+
this.resourceId = resourceId;
58+
this.currentVersion = currentVersion;
59+
this.connectionTimeout = connectionTimeout;
60+
this.updateChecker = updateChecker;
61+
this.completeAction = completeAction;
62+
this.errorHandler = errorHandler;
63+
64+
Thread thread = new Thread(this::checkNow);
65+
thread.setName("UpdateLib-" + thread.getId());
66+
thread.start();
67+
68+
if(repeatingChecksEnabled) {
69+
new Timer().schedule(new TimerTask() {
70+
@Override
71+
public void run() {
72+
checkNow();
73+
}
74+
}, interval);
75+
}
76+
}
77+
78+
79+
/**
80+
* Start an update check. It's recommended to only run this asynchronously as it may take time to fetch data from
81+
* the API.
82+
*
83+
* @return Response - Instance of {@link UpdateStatus}
84+
* @since 2.0.0-SNAPSHOT
85+
*/
86+
@RunAsync
87+
public UpdateStatus checkNow() {
88+
try {
89+
lastStatus = updateChecker.check(new CheckData(resourceId, currentVersion, connectionTimeout));
90+
lastCheck = System.currentTimeMillis();
91+
if(null != completeAction)
92+
completeAction.accept(lastStatus);
93+
} catch (Exception ex) {
94+
if(null == errorHandler)
95+
ex.printStackTrace();
96+
else
97+
errorHandler.accept(ex);
98+
}
99+
100+
return lastStatus;
101+
}
102+
103+
/**
104+
* Get last stored {@link UpdateStatus}. If UpdateLib hasn't checked for an update or the last check failed, this
105+
* will return null.
106+
*
107+
* @return Last stored {@link UpdateStatus}
108+
* @since 3.0.0-SNAPSHOT
109+
*/
110+
public UpdateStatus getLastStatus() {
111+
return lastStatus;
112+
}
113+
114+
/**
115+
* Get the last time UpdateLib checked for updates.
116+
*
117+
* @return Last check time in milliseconds.
118+
* @since 2.0.0-SNAPSHOT
119+
*/
120+
public long getLastCheck() {
121+
return lastCheck;
122+
}
123+
124+
/**
125+
* Get the current version of UpdateLib.
126+
*
127+
* @return Current version of UpdateLib.
128+
* @since 2.1.0-SNAPSHOT
129+
*/
130+
public static String getVersion() {
131+
return VERSION;
132+
}
133+
134+
}

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

Lines changed: 70 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,51 @@
1616

1717
package dev.hypera.updatelib;
1818

19-
import dev.hypera.updatelib.internal.UpdateLib;
20-
import dev.hypera.updatelib.internal.UpdateResponse;
19+
import dev.hypera.updatelib.annotations.Builder;
20+
import dev.hypera.updatelib.checkers.UpdateChecker;
21+
import dev.hypera.updatelib.checkers.impl.SpigotUpdateChecker;
22+
import dev.hypera.updatelib.objects.UpdateStatus;
23+
import dev.hypera.updatelib.utils.Check;
2124

25+
import java.util.concurrent.TimeUnit;
2226
import java.util.function.Consumer;
2327

24-
@SuppressWarnings("unused")
28+
@Builder
2529
public class UpdateLibBuilder {
2630

2731
private final long resourceId;
2832
private final String currentVersion;
2933

3034
private boolean repeatingChecksEnabled = true;
31-
private long checkInterval = 2 * (60 * (60 * 1000)); // 2 Hours
32-
private int connectionTimeout = 5000; // 5 Seconds
35+
private long checkInterval = 2 * (60 * (60 * 1000)); // Defaults to 2 hours.
36+
private int connectionTimeout = 10000; // Defaults to 10 seconds.
3337

34-
private Consumer<UpdateResponse> consumer = null;
38+
/**
39+
* Defaults to using {@link SpigotUpdateChecker}.
40+
*/
41+
private UpdateChecker updateChecker = new SpigotUpdateChecker();
42+
43+
private Consumer<UpdateStatus> completeAction = null;
44+
private Consumer<Throwable> errorHandler = null; // If error handler is not provided, stack traces will be printed by UpdateLib.
3545

46+
47+
/**
48+
* UpdateLibBuilder Constructor - Used internally by UpdateLib.
49+
*
50+
* @param currentVersion Current version of the resource.
51+
* @param resourceId Resource ID.
52+
*/
3653
private UpdateLibBuilder(String currentVersion, long resourceId) {
54+
Check.notNull("currentVersion", currentVersion);
3755
this.currentVersion = currentVersion;
3856
this.resourceId = resourceId;
3957
}
4058

4159
/**
42-
* Creates a new instance of {@link UpdateLibBuilder}.
60+
* Create a new instance of {@link UpdateLibBuilder}
4361
*
4462
* @param currentVersion Current version of the resource.
45-
* @param resourceId SpigotMC Resource Id.
63+
* @param resourceId Resource ID.
4664
*
4765
* @return Instance of {@link UpdateLibBuilder}
4866
* @since 2.0.0-SNAPSHOT
@@ -52,11 +70,11 @@ public static UpdateLibBuilder create(String currentVersion, long resourceId) {
5270
}
5371

5472
/**
55-
* Should UpdateLib keep checking for updates? (Time defined by checkInterval)
73+
* Sets if UpdateLib should check for updates periodically.
5674
*
57-
* @param enabled Repeating checks enabled.
75+
* @param enabled If UpdateLib should check for updates periodically.
5876
*
59-
* @see #setCheckInterval(long)
77+
* @return {@link UpdateLibBuilder}
6078
* @since 2.0.0-SNAPSHOT
6179
*/
6280
public UpdateLibBuilder setRepeatingChecksEnabled(boolean enabled) {
@@ -65,23 +83,26 @@ public UpdateLibBuilder setRepeatingChecksEnabled(boolean enabled) {
6583
}
6684

6785
/**
68-
* How often should UpdateLib check for updates? (Only works if repeatingChecksEnabled is true)
86+
* Sets how often UpdateLib should check for updates? This is only needed if 'repeatingChecksEnabled' is true.
6987
*
70-
* @param interval Interval in milliseconds.
88+
* @param time Time.
89+
* @param unit TimeUnit.
7190
*
91+
* @return {@link UpdateLibBuilder}
7292
* @see #setRepeatingChecksEnabled(boolean)
7393
* @since 2.0.0-SNAPSHOT
7494
*/
75-
public UpdateLibBuilder setCheckInterval(long interval) {
76-
this.checkInterval = interval;
95+
public UpdateLibBuilder setCheckInterval(long time, TimeUnit unit) {
96+
this.checkInterval = unit.toMillis(time);
7797
return this;
7898
}
7999

80100
/**
81-
* After how many milliseconds should we timeout the request to SpigotMC's API?
101+
* Sets the amount of milliseconds UpdateLib should wait before timing out on requests.
82102
*
83103
* @param timeout Timeout in milliseconds.
84104
*
105+
* @return {@link UpdateLibBuilder}
85106
* @since 2.0.0-SNAPSHOT
86107
*/
87108
public UpdateLibBuilder setConnectionTimeout(int timeout) {
@@ -90,25 +111,52 @@ public UpdateLibBuilder setConnectionTimeout(int timeout) {
90111
}
91112

92113
/**
93-
* Sets a consumer to run after UpdateLib has checked for an update.
114+
* Sets the {@link UpdateChecker} to be used by UpdateLib to check for updates.
115+
*
116+
* @param updateChecker Instance of an {@link UpdateChecker}
117+
*
118+
* @return {@link UpdateLibBuilder}
119+
* @since 3.0.0-SNAPSHOT
120+
*/
121+
public UpdateLibBuilder setUpdateChecker(UpdateChecker updateChecker) {
122+
this.updateChecker = updateChecker;
123+
return this;
124+
}
125+
126+
/**
127+
* Sets an action to run after UpdateLib has checked for an update.
94128
*
95-
* @param consumer Consumer to run after checking for an update.
129+
* @param action Consumer to run after checking for an update.
96130
*
131+
* @return {@link UpdateLibBuilder}
97132
* @since 2.1.0-SNAPSHOT
98133
*/
99-
public UpdateLibBuilder setConsumer(Consumer<UpdateResponse> consumer) {
100-
this.consumer = consumer;
134+
public UpdateLibBuilder setCompleteAction(Consumer<UpdateStatus> action) {
135+
this.completeAction = action;
136+
return this;
137+
}
138+
139+
/**
140+
* Sets an consumer to run if UpdateLib encounters an exception.
141+
*
142+
* @param handler Error handler.
143+
*
144+
* @return {@link UpdateLibBuilder}
145+
* @since 3.0.0-SNAPSHOT
146+
*/
147+
public UpdateLibBuilder setErrorHandler(Consumer<Throwable> handler) {
148+
this.errorHandler = handler;
101149
return this;
102150
}
103151

104152
/**
105-
* Builds a new instance of {@link UpdateLib}.
153+
* Builds a new instance of {@link UpdateLib}
106154
*
107155
* @return Instance of {@link UpdateLib}
108156
* @since 2.0.0-SNAPSHOT
109157
*/
110158
public UpdateLib build() {
111-
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout, consumer);
159+
return new UpdateLib(resourceId, currentVersion, repeatingChecksEnabled, checkInterval, connectionTimeout, updateChecker, completeAction, errorHandler);
112160
}
113161

114162
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2021 Joshua Sing <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
package dev.hypera.updatelib.annotations;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Annotation for marking a class as a Builder.
26+
*/
27+
@Target(ElementType.TYPE)
28+
@Retention(RetentionPolicy.RUNTIME)
29+
public @interface Builder {
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2021 Joshua Sing <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
package dev.hypera.updatelib.annotations;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Annotation for marking a class as for use by UpdateLib only.
26+
*/
27+
@Target(ElementType.TYPE)
28+
@Retention(RetentionPolicy.RUNTIME)
29+
public @interface Internal {
30+
31+
}

0 commit comments

Comments
 (0)