34
34
import cc .arduino .contributions .libraries .ContributedLibrary ;
35
35
36
36
import java .util .Comparator ;
37
+ import java .util .Optional ;
37
38
38
39
public class VersionComparator implements Comparator <String > {
39
40
40
- @ Override
41
- public int compare (String a , String b ) {
42
- // null is always less than any other value
43
- if (a == null && b == null )
44
- return 0 ;
45
- if (a == null )
46
- return -1 ;
47
- if (b == null )
48
- return 1 ;
49
-
50
- Version versionA = VersionHelper .valueOf (a );
51
- Version versionB = VersionHelper .valueOf (b );
52
-
53
- return versionA .compareTo (versionB );
54
- }
55
-
56
- public static boolean greaterThan (String a , String b ) {
57
- // null is always less than any other value
58
- if (a == null && b == null ) {
59
- return false ;
41
+ public static int compareTo (String a , String b ) {
42
+ Optional <Version > versionA = VersionHelper .valueOf (a );
43
+ Optional <Version > versionB = VersionHelper .valueOf (b );
44
+ if (versionA .isPresent () && versionB .isPresent ()) {
45
+ return versionA .get ().compareTo (versionB .get ());
60
46
}
61
- if (a == null ) {
62
- return false ;
47
+ if (versionA . isPresent () ) {
48
+ return 1 ;
63
49
}
64
- if (b == null ) {
65
- return true ;
50
+ if (versionB . isPresent () ) {
51
+ return - 1 ;
66
52
}
53
+ return 0 ;
54
+ }
67
55
68
- Version versionA = VersionHelper .valueOf (a );
69
- Version versionB = VersionHelper .valueOf (b );
56
+ @ Override
57
+ public int compare (String a , String b ) {
58
+ return compareTo (a , b );
59
+ }
70
60
71
- return versionA .greaterThan (versionB );
61
+ public static boolean greaterThan (String a , String b ) {
62
+ return compareTo (a , b ) > 0 ;
72
63
}
73
64
74
65
public static String max (String a , String b ) {
@@ -79,8 +70,7 @@ public static ContributedLibrary max(ContributedLibrary a, ContributedLibrary b)
79
70
return greaterThan (a , b ) ? a : b ;
80
71
}
81
72
82
- public static boolean greaterThan (ContributedLibrary a ,
83
- ContributedLibrary b ) {
73
+ public static boolean greaterThan (ContributedLibrary a , ContributedLibrary b ) {
84
74
return greaterThan (a .getParsedVersion (), b .getParsedVersion ());
85
75
}
86
76
}
0 commit comments