Skip to content

Commit

Permalink
[MGPG-81] refactor GpgVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Syquel committed Feb 26, 2021
1 parent 9808611 commit 64e1d4e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 56 deletions.
93 changes: 38 additions & 55 deletions src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,46 @@
*/
public class GpgVersion implements Comparable<GpgVersion>
{
private final String rawVersion;

private GpgVersion( String rawVersion )
{
this.rawVersion = rawVersion;
}
private static final Pattern VERSION_PATTERN = Pattern.compile( "(\\d+\\.)+(\\d+)" );

public static GpgVersion parse( String rawVersion )
private final int[] versionSegments;

private GpgVersion( int... versionSegments )
{
return new GpgVersion( rawVersion );
this.versionSegments = versionSegments;
}

@Override
public int compareTo( GpgVersion other )
public static GpgVersion parse( String rawVersion )
{
Pattern p = Pattern.compile( "(\\d+\\.)+(\\d+)" );

String[] thisSegments;
Matcher m = p.matcher( rawVersion );
if ( m.find() )
final Matcher versionMatcher = VERSION_PATTERN.matcher( rawVersion );
if ( !versionMatcher.find() )
{
thisSegments = m.group( 0 ).split( "\\." );
}
else
{
throw new IllegalArgumentException( "Can't parse version of " + this.rawVersion );
throw new IllegalArgumentException( "Can't parse version of " + rawVersion );
}

String[] otherSegments;
m = p.matcher( other.rawVersion );
if ( m.find() )
{
otherSegments = m.group( 0 ).split( "\\." );
}
else
final String[] rawVersionSegments = versionMatcher.group( 0 ).split( "\\." );

final int[] versionSegments = new int[rawVersionSegments.length];
for ( int index = 0; index < rawVersionSegments.length; index++ )
{
throw new IllegalArgumentException( "Can't parse version of " + other.rawVersion );
versionSegments[index] = Integer.parseInt( rawVersionSegments[index] );
}

return new GpgVersion( versionSegments );
}

@Override
public int compareTo( GpgVersion other )
{
final int[] thisSegments = versionSegments;
final int[] otherSegments = other.versionSegments;

int minSegments = Math.min( thisSegments.length, otherSegments.length );

for ( int index = 0; index < minSegments; index++ )
{
int thisValue = Integer.parseInt( thisSegments[index] );

int otherValue = Integer.parseInt( otherSegments[index] );

int compareValue = Integer.compare( thisValue, otherValue );
int compareValue = Integer.compare( thisSegments[index], otherSegments[index] );

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

/**
* Verify if this version is before some other version
*
* @param other the version to compare with
* @return {@code true} is this is less than {@code other}, otherwise {@code false}
*/
public boolean isBefore( String other )
{
return this.compareTo( parse( other ) ) < 0;
}

/**
* Verify if this version is at least some other version
*
Expand All @@ -123,21 +104,23 @@ public boolean isAtLeast( GpgVersion other )
return this.compareTo( other ) >= 0;
}

/**
* Verify if this version is at least some other version
*
* @param other the version to compare with
* @return {@code true} is this is greater than or equal to {@code other}, otherwise {@code false}
*/
public boolean isAtLeast( String other )
{
return this.compareTo( parse( other ) ) >= 0;
}

@Override
public String toString()
{
return rawVersion;
if ( versionSegments.length == 0 )
{
return "";
}

final StringBuilder versionStringBuilder = new StringBuilder();
versionStringBuilder.append( versionSegments[0] );

for ( int index = 1; index < versionSegments.length; index++ )
{
versionStringBuilder.append( '.' ).append( versionSegments[index] );
}

return versionStringBuilder.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void test()
GpgVersionConsumer consumer = new GpgVersionConsumer();
consumer.consumeLine( "gpg (GnuPG/MacGPG2) 2.2.10" );

assertThat( consumer.getGpgVersion().toString(), is( "gpg (GnuPG/MacGPG2) 2.2.10" ) );
assertThat( consumer.getGpgVersion().toString(), is( GpgVersion.parse( "2.2.10" ).toString() ) );
}

}

0 comments on commit 64e1d4e

Please sign in to comment.