Skip to content

Commit b38c638

Browse files
authored
Merge pull request #11 from Syquel/bugfix/MGPG-80
[MGPG-81] check format in constructor
2 parents 9808611 + 64e1d4e commit b38c638

File tree

2 files changed

+39
-56
lines changed

2 files changed

+39
-56
lines changed

src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java

+38-55
Original file line numberDiff line numberDiff line change
@@ -32,54 +32,46 @@
3232
*/
3333
public class GpgVersion implements Comparable<GpgVersion>
3434
{
35-
private final String rawVersion;
3635

37-
private GpgVersion( String rawVersion )
38-
{
39-
this.rawVersion = rawVersion;
40-
}
36+
private static final Pattern VERSION_PATTERN = Pattern.compile( "(\\d+\\.)+(\\d+)" );
4137

42-
public static GpgVersion parse( String rawVersion )
38+
private final int[] versionSegments;
39+
40+
private GpgVersion( int... versionSegments )
4341
{
44-
return new GpgVersion( rawVersion );
42+
this.versionSegments = versionSegments;
4543
}
4644

47-
@Override
48-
public int compareTo( GpgVersion other )
45+
public static GpgVersion parse( String rawVersion )
4946
{
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() )
5549
{
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 );
6151
}
6252

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++ )
7057
{
71-
throw new IllegalArgumentException( "Can't parse version of " + other.rawVersion );
58+
versionSegments[index] = Integer.parseInt( rawVersionSegments[index] );
7259
}
7360

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+
7470
int minSegments = Math.min( thisSegments.length, otherSegments.length );
7571

7672
for ( int index = 0; index < minSegments; index++ )
7773
{
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] );
8375

8476
if ( compareValue != 0 )
8577
{
@@ -101,17 +93,6 @@ public boolean isBefore( GpgVersion other )
10193
return this.compareTo( other ) < 0;
10294
}
10395

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-
11596
/**
11697
* Verify if this version is at least some other version
11798
*
@@ -123,21 +104,23 @@ public boolean isAtLeast( GpgVersion other )
123104
return this.compareTo( other ) >= 0;
124105
}
125106

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-
137107
@Override
138108
public String toString()
139109
{
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();
141124
}
142125

143126
}

src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void test()
3434
GpgVersionConsumer consumer = new GpgVersionConsumer();
3535
consumer.consumeLine( "gpg (GnuPG/MacGPG2) 2.2.10" );
3636

37-
assertThat( consumer.getGpgVersion().toString(), is( "gpg (GnuPG/MacGPG2) 2.2.10" ) );
37+
assertThat( consumer.getGpgVersion().toString(), is( GpgVersion.parse( "2.2.10" ).toString() ) );
3838
}
3939

4040
}

0 commit comments

Comments
 (0)