32
32
*/
33
33
public class GpgVersion implements Comparable <GpgVersion >
34
34
{
35
- private final String rawVersion ;
36
35
37
- private GpgVersion ( String rawVersion )
38
- {
39
- this .rawVersion = rawVersion ;
40
- }
36
+ private static final Pattern VERSION_PATTERN = Pattern .compile ( "(\\ d+\\ .)+(\\ d+)" );
41
37
42
- public static GpgVersion parse ( String rawVersion )
38
+ private final int [] versionSegments ;
39
+
40
+ private GpgVersion ( int ... versionSegments )
43
41
{
44
- return new GpgVersion ( rawVersion ) ;
42
+ this . versionSegments = versionSegments ;
45
43
}
46
44
47
- @ Override
48
- public int compareTo ( GpgVersion other )
45
+ public static GpgVersion parse ( String rawVersion )
49
46
{
50
- Pattern p = Pattern .compile ( "(\\ d+\\ .)+(\\ d+)" );
51
-
52
- String [] thisSegments ;
53
- Matcher m = p .matcher ( rawVersion );
54
- if ( m .find () )
47
+ final Matcher versionMatcher = VERSION_PATTERN .matcher ( rawVersion );
48
+ if ( !versionMatcher .find () )
55
49
{
56
- thisSegments = m .group ( 0 ).split ( "\\ ." );
57
- }
58
- else
59
- {
60
- throw new IllegalArgumentException ( "Can't parse version of " + this .rawVersion );
50
+ throw new IllegalArgumentException ( "Can't parse version of " + rawVersion );
61
51
}
62
52
63
- String [] otherSegments ;
64
- m = p .matcher ( other .rawVersion );
65
- if ( m .find () )
66
- {
67
- otherSegments = m .group ( 0 ).split ( "\\ ." );
68
- }
69
- else
53
+ final String [] rawVersionSegments = versionMatcher .group ( 0 ).split ( "\\ ." );
54
+
55
+ final int [] versionSegments = new int [rawVersionSegments .length ];
56
+ for ( int index = 0 ; index < rawVersionSegments .length ; index ++ )
70
57
{
71
- throw new IllegalArgumentException ( "Can't parse version of " + other . rawVersion );
58
+ versionSegments [ index ] = Integer . parseInt ( rawVersionSegments [ index ] );
72
59
}
73
60
61
+ return new GpgVersion ( versionSegments );
62
+ }
63
+
64
+ @ Override
65
+ public int compareTo ( GpgVersion other )
66
+ {
67
+ final int [] thisSegments = versionSegments ;
68
+ final int [] otherSegments = other .versionSegments ;
69
+
74
70
int minSegments = Math .min ( thisSegments .length , otherSegments .length );
75
71
76
72
for ( int index = 0 ; index < minSegments ; index ++ )
77
73
{
78
- int thisValue = Integer .parseInt ( thisSegments [index ] );
79
-
80
- int otherValue = Integer .parseInt ( otherSegments [index ] );
81
-
82
- int compareValue = Integer .compare ( thisValue , otherValue );
74
+ int compareValue = Integer .compare ( thisSegments [index ], otherSegments [index ] );
83
75
84
76
if ( compareValue != 0 )
85
77
{
@@ -101,17 +93,6 @@ public boolean isBefore( GpgVersion other )
101
93
return this .compareTo ( other ) < 0 ;
102
94
}
103
95
104
- /**
105
- * Verify if this version is before some other version
106
- *
107
- * @param other the version to compare with
108
- * @return {@code true} is this is less than {@code other}, otherwise {@code false}
109
- */
110
- public boolean isBefore ( String other )
111
- {
112
- return this .compareTo ( parse ( other ) ) < 0 ;
113
- }
114
-
115
96
/**
116
97
* Verify if this version is at least some other version
117
98
*
@@ -123,21 +104,23 @@ public boolean isAtLeast( GpgVersion other )
123
104
return this .compareTo ( other ) >= 0 ;
124
105
}
125
106
126
- /**
127
- * Verify if this version is at least some other version
128
- *
129
- * @param other the version to compare with
130
- * @return {@code true} is this is greater than or equal to {@code other}, otherwise {@code false}
131
- */
132
- public boolean isAtLeast ( String other )
133
- {
134
- return this .compareTo ( parse ( other ) ) >= 0 ;
135
- }
136
-
137
107
@ Override
138
108
public String toString ()
139
109
{
140
- return rawVersion ;
110
+ if ( versionSegments .length == 0 )
111
+ {
112
+ return "" ;
113
+ }
114
+
115
+ final StringBuilder versionStringBuilder = new StringBuilder ();
116
+ versionStringBuilder .append ( versionSegments [0 ] );
117
+
118
+ for ( int index = 1 ; index < versionSegments .length ; index ++ )
119
+ {
120
+ versionStringBuilder .append ( '.' ).append ( versionSegments [index ] );
121
+ }
122
+
123
+ return versionStringBuilder .toString ();
141
124
}
142
125
143
126
}
0 commit comments