16
16
17
17
package dev .hypera .updatelib ;
18
18
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 ;
21
24
25
+ import java .util .concurrent .TimeUnit ;
22
26
import java .util .function .Consumer ;
23
27
24
- @ SuppressWarnings ( "unused" )
28
+ @ Builder
25
29
public class UpdateLibBuilder {
26
30
27
31
private final long resourceId ;
28
32
private final String currentVersion ;
29
33
30
34
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.
33
37
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.
35
45
46
+
47
+ /**
48
+ * UpdateLibBuilder Constructor - Used internally by UpdateLib.
49
+ *
50
+ * @param currentVersion Current version of the resource.
51
+ * @param resourceId Resource ID.
52
+ */
36
53
private UpdateLibBuilder (String currentVersion , long resourceId ) {
54
+ Check .notNull ("currentVersion" , currentVersion );
37
55
this .currentVersion = currentVersion ;
38
56
this .resourceId = resourceId ;
39
57
}
40
58
41
59
/**
42
- * Creates a new instance of {@link UpdateLibBuilder}.
60
+ * Create a new instance of {@link UpdateLibBuilder}
43
61
*
44
62
* @param currentVersion Current version of the resource.
45
- * @param resourceId SpigotMC Resource Id .
63
+ * @param resourceId Resource ID .
46
64
*
47
65
* @return Instance of {@link UpdateLibBuilder}
48
66
* @since 2.0.0-SNAPSHOT
@@ -52,11 +70,11 @@ public static UpdateLibBuilder create(String currentVersion, long resourceId) {
52
70
}
53
71
54
72
/**
55
- * Should UpdateLib keep checking for updates? (Time defined by checkInterval)
73
+ * Sets if UpdateLib should check for updates periodically.
56
74
*
57
- * @param enabled Repeating checks enabled .
75
+ * @param enabled If UpdateLib should check for updates periodically .
58
76
*
59
- * @see #setCheckInterval(long)
77
+ * @return {@link UpdateLibBuilder}
60
78
* @since 2.0.0-SNAPSHOT
61
79
*/
62
80
public UpdateLibBuilder setRepeatingChecksEnabled (boolean enabled ) {
@@ -65,23 +83,26 @@ public UpdateLibBuilder setRepeatingChecksEnabled(boolean enabled) {
65
83
}
66
84
67
85
/**
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.
69
87
*
70
- * @param interval Interval in milliseconds.
88
+ * @param time Time.
89
+ * @param unit TimeUnit.
71
90
*
91
+ * @return {@link UpdateLibBuilder}
72
92
* @see #setRepeatingChecksEnabled(boolean)
73
93
* @since 2.0.0-SNAPSHOT
74
94
*/
75
- public UpdateLibBuilder setCheckInterval (long interval ) {
76
- this .checkInterval = interval ;
95
+ public UpdateLibBuilder setCheckInterval (long time , TimeUnit unit ) {
96
+ this .checkInterval = unit . toMillis ( time ) ;
77
97
return this ;
78
98
}
79
99
80
100
/**
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.
82
102
*
83
103
* @param timeout Timeout in milliseconds.
84
104
*
105
+ * @return {@link UpdateLibBuilder}
85
106
* @since 2.0.0-SNAPSHOT
86
107
*/
87
108
public UpdateLibBuilder setConnectionTimeout (int timeout ) {
@@ -90,25 +111,52 @@ public UpdateLibBuilder setConnectionTimeout(int timeout) {
90
111
}
91
112
92
113
/**
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.
94
128
*
95
- * @param consumer Consumer to run after checking for an update.
129
+ * @param action Consumer to run after checking for an update.
96
130
*
131
+ * @return {@link UpdateLibBuilder}
97
132
* @since 2.1.0-SNAPSHOT
98
133
*/
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 ;
101
149
return this ;
102
150
}
103
151
104
152
/**
105
- * Builds a new instance of {@link UpdateLib}.
153
+ * Builds a new instance of {@link UpdateLib}
106
154
*
107
155
* @return Instance of {@link UpdateLib}
108
156
* @since 2.0.0-SNAPSHOT
109
157
*/
110
158
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 );
112
160
}
113
161
114
162
}
0 commit comments