1
- // Copyright (c) 2019, 2021 , Oracle and/or its affiliates.
1
+ // Copyright (c) 2019, 2024 , Oracle and/or its affiliates.
2
2
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3
3
4
4
package com .oracle .weblogic .imagetool .util ;
@@ -303,6 +303,27 @@ public static void deleteFilesRecursively(String pathDir) throws IOException {
303
303
logger .exiting ();
304
304
}
305
305
306
+ /**
307
+ * Compares two version strings. A null-friendly comparator that considers null to be less than non-null.
308
+ * Any qualifiers are treated as older than the same version without
309
+ * a qualifier. If both versions have qualifiers and are otherwise equal, they are compared using
310
+ * String.compareTo() to determine the result.
311
+ *
312
+ * @param thisVersion - first version
313
+ * @param otherVersion - second version
314
+ * @return returns 0 if the versions are equal, greater than zero if thisVersion is newer,
315
+ * and less than zero if thisVersion is older.
316
+ */
317
+ public static int compareVersionsNullsFirst (String thisVersion , String otherVersion ) {
318
+ if (isEmptyString (thisVersion )) {
319
+ return (isEmptyString (otherVersion )) ? 0 : -1 ;
320
+ } else if (isEmptyString (otherVersion )) {
321
+ return 1 ;
322
+ } else {
323
+ return compareVersions (thisVersion , otherVersion );
324
+ }
325
+ }
326
+
306
327
/**
307
328
* Compares two version strings. Any qualifiers are treated as older than the same version without
308
329
* a qualifier. If both versions have qualifiers and are otherwise equal, they are compared using
@@ -314,8 +335,6 @@ public static void deleteFilesRecursively(String pathDir) throws IOException {
314
335
* and less than zero if thisVersion is older.
315
336
*/
316
337
public static int compareVersions (String thisVersion , String otherVersion ) {
317
- int result = 0 ;
318
-
319
338
if (isEmptyString (thisVersion ) || isEmptyString (otherVersion )) {
320
339
throw new IllegalArgumentException ("cannot compare null strings" );
321
340
}
@@ -330,61 +349,60 @@ public static int compareVersions(String thisVersion, String otherVersion) {
330
349
331
350
int fieldsToCompare = Math .min (thisVersionElements .length , otherVersionElements .length );
332
351
352
+ int result = 0 ;
333
353
int idx ;
334
354
for (idx = 0 ; idx < fieldsToCompare ; idx ++) {
335
355
int thisVersionNumber = Integer .parseInt (thisVersionElements [idx ]);
336
356
int otherVersionNumber = Integer .parseInt (otherVersionElements [idx ]);
337
357
338
- if (thisVersionNumber > otherVersionNumber ) {
339
- result = 1 ;
340
- break ;
341
- } else if (thisVersionNumber < otherVersionNumber ) {
342
- result = -1 ;
343
- break ;
358
+ result = Integer .compare (thisVersionNumber , otherVersionNumber );
359
+ if (result != 0 ) {
360
+ return result ;
344
361
}
345
362
}
346
363
347
364
// Version fields compared so far are equal so check to see if one version number
348
365
// has more fields than the other.
349
366
//
350
- if (result == 0 && thisVersionElements .length != otherVersionElements .length ) {
367
+ if (thisVersionElements .length != otherVersionElements .length ) {
351
368
if (thisVersionElements .length > otherVersionElements .length ) {
352
369
result = 1 ;
353
370
} else {
354
371
result = -1 ;
355
372
}
356
373
}
357
374
375
+ if (result != 0 ) {
376
+ return result ;
377
+ }
378
+
358
379
// Finally, look to see if one or both versions have a qualifier if they are otherwise the same.
359
380
//
360
- if ( result == 0 ) {
361
- int useCase = 0 ;
362
- if ( thisVersion . indexOf ( '-' ) != - 1 ) {
363
- useCase += 1 ;
364
- }
365
- if ( otherVersion . indexOf ( '-' ) != - 1 ) {
366
- useCase += 2 ;
367
- }
368
- switch ( useCase ) {
369
- case 0 :
370
- break ;
381
+ int useCase = 0 ;
382
+ if ( thisVersion . indexOf ( '-' ) != - 1 ) {
383
+ useCase += 1 ;
384
+ }
385
+ if ( otherVersion . indexOf ( '-' ) != - 1 ) {
386
+ useCase += 2 ;
387
+ }
388
+ switch ( useCase ) {
389
+ case 1 :
390
+ result = - 1 ;
391
+ break ;
371
392
372
- case 1 :
373
- result = - 1 ;
374
- break ;
393
+ case 2 :
394
+ result = 1 ;
395
+ break ;
375
396
376
- case 2 :
377
- result = 1 ;
378
- break ;
397
+ case 3 :
398
+ String thisQualifier = thisVersion .substring (thisVersion .indexOf ('-' ));
399
+ String otherQualifier = otherVersion .substring (otherVersion .indexOf ('-' ));
400
+ result = thisQualifier .compareTo (otherQualifier );
401
+ break ;
379
402
380
- case 3 :
381
- String thisQualifier = thisVersion .substring (thisVersion .indexOf ('-' ));
382
- String otherQualifier = otherVersion .substring (otherVersion .indexOf ('-' ));
383
- result = thisQualifier .compareTo (otherQualifier );
384
- break ;
385
- default :
386
- break ;
387
- }
403
+ case 0 :
404
+ default :
405
+ break ;
388
406
}
389
407
return result ;
390
408
}
0 commit comments