From 64e1d4eecc9ff04979518dd15f8d33a3ec18538e Mon Sep 17 00:00:00 2001 From: Frederik Boster Date: Fri, 26 Feb 2021 14:10:59 +0100 Subject: [PATCH] [MGPG-81] refactor GpgVersion --- .../apache/maven/plugins/gpg/GpgVersion.java | 93 ++++++++----------- .../plugins/gpg/GpgVersionConsumerTest.java | 2 +- 2 files changed, 39 insertions(+), 56 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java b/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java index 35b33e0..a6dc14b 100644 --- a/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java +++ b/src/main/java/org/apache/maven/plugins/gpg/GpgVersion.java @@ -32,54 +32,46 @@ */ public class GpgVersion implements Comparable { - 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 ) { @@ -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 * @@ -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(); } } diff --git a/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java b/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java index 264a9f2..15f30cd 100644 --- a/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java +++ b/src/test/java/org/apache/maven/plugins/gpg/GpgVersionConsumerTest.java @@ -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() ) ); } }