2424public final class UpdateController {
2525
2626 private String updateUrl ;
27+ private String currentVersion ;
2728
2829 private final Logger logger ;
2930 private final ObjectMapper objectMapper ;
3031
3132 /**
3233 * Initialize a new UpdateController
3334 *
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
3537 */
36- public UpdateController (final String updateUrl ) {
38+ public UpdateController (final String updateUrl , final String currentVersion ) {
3739 logger = LogManager .getLogger (UpdateController .class );
3840 logger .info ("Initializing new UpdateController object" );
3941
4042 objectMapper = new ObjectMapper ();
4143 objectMapper .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false );
4244
4345 setUpdateUrl (updateUrl );
46+ setCurrentVersion (currentVersion );
4447 }
4548
4649 /**
@@ -63,10 +66,52 @@ public Optional<PlatformUpdate> checkForUpdates(final String currentPlatform, fi
6366
6467 final List <PlatformUpdate > updates = getUpdates ();
6568
66- return updates
69+ final Optional < PlatformUpdate > update = updates
6770 .stream ()
6871 .filter (e -> e .getPlatformName ().equalsIgnoreCase (currentPlatform ) && e .isPortable () == isPortable )
6972 .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 ;
70115 }
71116
72117 /**
@@ -144,4 +189,27 @@ public void setUpdateUrl(final String updateUrl) {
144189
145190 this .updateUrl = updateUrl ;
146191 }
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+ }
147215}
0 commit comments