Skip to content

Instantly share code, notes, and snippets.

@PimDeWitte
Created December 4, 2014 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PimDeWitte/e28418168cd00e6348f2 to your computer and use it in GitHub Desktop.
Save PimDeWitte/e28418168cd00e6348f2 to your computer and use it in GitHub Desktop.
package model;
import java.util.Arrays;
/**
* @author Pim de Witte(wwadewitte), Whitespell LLC
* 11/19/14
* versioning
* ${FILE_NAME}
*/
public class SemanticVersioningObject {
private int majorVersion;
private int minorVersion;
private int patchVersion;
private String version;
private String[] supportedSeparators = new String[] {
// todo(pim) this value is \\. by default, no time to do proper regex conventions atm,
"-","_"
};
public SemanticVersioningObject(String version) {
// if version is null, make object usable and always less than others but do not parse anything.
if(version == null) {
majorVersion = -1;
minorVersion = -1;
patchVersion = -1;
return;
}
this.version = version;
// default separationCharacter is .
String separationCharacter = "\\.";
// check if other separationCharacter is found
for(int i = 0; i < supportedSeparators.length; i++) {
if(version.contains(supportedSeparators[i])) {
separationCharacter = supportedSeparators[i];
}
}
//split string of all versions in individual components
String[] versions = version.split(separationCharacter);
this.majorVersion = Integer.parseInt(versions[0]);
this.minorVersion = Integer.parseInt(versions[1]);
this.patchVersion = Integer.parseInt(versions[2]);
}
// is this semantic versioning object larger than a semantic versioning object it is being compared to?
public boolean isGreaterThan(SemanticVersioningObject s) {
return this.majorVersion > s.getMajorVersion()
|| this.minorVersion > s.getMinorVersion() && this.majorVersion >= s.getMajorVersion()
|| this.patchVersion > s.getPatchVersion() && this.majorVersion >= s.getMajorVersion() && this.minorVersion >= s.getMinorVersion()
;
}
// is this semantic versioning object larger than a semantic versioning object it is being compared to?
public boolean isSmallerThan(SemanticVersioningObject s) {
return this.majorVersion < s.getMajorVersion()
|| this.minorVersion < s.getMinorVersion() && this.majorVersion <= s.getMajorVersion()
|| this.patchVersion < s.getPatchVersion() && this.majorVersion <= s.getMajorVersion() && this.minorVersion <= s.getMinorVersion()
;
}
public int getMajorVersion() {
return this.majorVersion;
}
public int getMinorVersion() {
return this.minorVersion;
}
public int getPatchVersion() {
return this.patchVersion;
}
public String toString() {
return version;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment