-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MGPG-81] check format in constructor #11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this version string come from? That is, what is it the version of? Is it ever anything that does not match this pattern? E.g. 3.45.2-beta? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see the original author of this class made the assumption that it will always be a sequence of numbers delimited by dots (although the original author wrote in the class-level comment, that this is a SemVer parser). Unfortunately I am in no position to evaluate whether GPG will continue using this versioning scheme, but it seems they have consistently used it up until now: https://files.gpg4win.org/ |
||
{ | ||
Pattern p = Pattern.compile( "(\\d+\\.)+(\\d+)" ); | ||
|
||
String[] thisSegments; | ||
Matcher m = p.matcher( rawVersion ); | ||
if ( m.find() ) | ||
final Matcher versionMatcher = VERSION_PATTERN.matcher( rawVersion ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regex might be overkill. Looks like you can just catch and convert the number format exception below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The original author implemented it in a way, that the full output of It is debatable whether that is the best way to extract the version number (personally I would have extracted the version number part already in GpgVersionParser.GpgVersionConsumer before passing it in here), but I would like to avoid introducing new bugs by breaking with the existing external contract of this class. In the end it's your call again; I could widen the scope of the refactoring to the whole GPG version parser. // EDIT |
||
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] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be a simple int comparison? e.g.
(Double check my order there) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be preferable to adhere to the contract of Integer#compareTo / Integer#compare, which is internally implemented as But your solution works of course, too. Your call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. We should adhere to the contract. The contract is more lenient than the current implementation. It just requires negative, positive, or zero at the appropriate places, not -1, 0, or +1 specifically. |
||
|
||
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(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Syquel
@elharo
How many times these patterns exist in the project?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two patterns AFAIK: One in GpgVersionParser.GpgVersionConsumer and this one here.
This one could possibly be removed and
GpgVersionParser.GpgVersionConsumer
could pass the extracted version number toGpgVersion
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be even better to use
gpgconf --query-swdb gnupg
instead ofgpg --version
, because gpgconf returns machine-readable output.although I don't know if that would work with all distributions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works on mac, too: