|
20 | 20 | import dev.hypera.updatelib.internal.tasks.UpdateTask;
|
21 | 21 |
|
22 | 22 | import java.util.Timer;
|
| 23 | +import java.util.function.Consumer; |
23 | 24 |
|
24 | 25 | public class UpdateLib {
|
25 | 26 |
|
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 | + } |
100 | 125 |
|
101 | 126 | }
|
0 commit comments