24
24
public final class UpdateController {
25
25
26
26
private String updateUrl ;
27
+ private String currentVersion ;
27
28
28
29
private final Logger logger ;
29
30
private final ObjectMapper objectMapper ;
30
31
31
32
/**
32
33
* Initialize a new UpdateController
33
34
*
34
- * @param updateUrl The URL that can be used to check for updates
35
+ * @param updateUrl The URL that can be used to check for updates
36
+ * @param currentVersion The current application version
35
37
*/
36
- public UpdateController (final String updateUrl ) {
38
+ public UpdateController (final String updateUrl , final String currentVersion ) {
37
39
logger = LogManager .getLogger (UpdateController .class );
38
40
logger .info ("Initializing new UpdateController object" );
39
41
40
42
objectMapper = new ObjectMapper ();
41
43
objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
42
44
43
45
setUpdateUrl (updateUrl );
46
+ setCurrentVersion (currentVersion );
44
47
}
45
48
46
49
/**
@@ -63,10 +66,52 @@ public Optional<PlatformUpdate> checkForUpdates(final String currentPlatform, fi
63
66
64
67
final List <PlatformUpdate > updates = getUpdates ();
65
68
66
- return updates
69
+ final Optional < PlatformUpdate > update = updates
67
70
.stream ()
68
71
.filter (e -> e .getPlatformName ().equalsIgnoreCase (currentPlatform ) && e .isPortable () == isPortable )
69
72
.findFirst ();
73
+
74
+ if (update .isPresent ()) {
75
+ final PlatformUpdate platformUpdate = update .get ();
76
+ if (versionCompare (currentVersion , platformUpdate .getMajorVersion () + "." + platformUpdate .getMinorVersion () + "." + platformUpdate .getBuildVersion () + "." + platformUpdate .getRevisionVersion ()) < 0 ) {
77
+ return update ;
78
+ }
79
+ return Optional .empty ();
80
+ }
81
+ return Optional .empty ();
82
+ }
83
+
84
+ /**
85
+ * Compare to strings to check which version is larger
86
+ *
87
+ * @param v1 The first {@link String} object that contains a version
88
+ * @param v2 The second {@link String} object that contains a version
89
+ * @return 1 if v1 is larger than v2, -1 if v2 is larger than v1 and 0 if both are equal
90
+ */
91
+ private int versionCompare (String v1 , String v2 ) {
92
+ int vnum1 = 0 ;
93
+ int vnum2 = 0 ;
94
+
95
+ for (int i = 0 , j = 0 ; (i < v1 .length () || j < v2 .length ()); ) {
96
+ while (i < v1 .length () && v1 .charAt (i ) != '.' ) {
97
+ vnum1 = vnum1 * 10 + (v1 .charAt (i ) - '0' );
98
+ i ++;
99
+ }
100
+ while (j < v2 .length () && v2 .charAt (j ) != '.' ) {
101
+ vnum2 = vnum2 * 10 + (v2 .charAt (j ) - '0' );
102
+ j ++;
103
+ }
104
+
105
+ if (vnum1 > vnum2 )
106
+ return 1 ;
107
+ if (vnum2 > vnum1 )
108
+ return -1 ;
109
+
110
+ vnum1 = vnum2 = 0 ;
111
+ i ++;
112
+ j ++;
113
+ }
114
+ return 0 ;
70
115
}
71
116
72
117
/**
@@ -144,4 +189,27 @@ public void setUpdateUrl(final String updateUrl) {
144
189
145
190
this .updateUrl = updateUrl ;
146
191
}
192
+
193
+ /**
194
+ * Get the current version
195
+ *
196
+ * @return The current version
197
+ */
198
+ public String getCurrentVersion () {
199
+ return currentVersion ;
200
+ }
201
+
202
+ /**
203
+ * Set the current version
204
+ *
205
+ * @param currentVersion The current version
206
+ */
207
+ public void setCurrentVersion (final String currentVersion ) {
208
+ if (currentVersion == null )
209
+ throw new NullPointerException ("currentVersion cannot be null!" );
210
+ if (currentVersion .isEmpty ())
211
+ throw new IllegalArgumentException ("currentVersion cannot be empty!" );
212
+
213
+ this .currentVersion = currentVersion ;
214
+ }
147
215
}
0 commit comments