Last active
August 29, 2015 14:05
-
-
Save yim1990/9aa60518495f4b75278e to your computer and use it in GitHub Desktop.
Version diff Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Pattern; | |
public class Version { | |
private final static String FORMAT_STRING="%4s"; | |
private final static Pattern PATTERN=Pattern.compile(".", Pattern.LITERAL); | |
private String normalizedVersion; | |
private int length; | |
public Version(String versionArg) { | |
normalizeVersion(versionArg); | |
} | |
public String getNormalizedVersion() { | |
return normalizedVersion; | |
} | |
public int getLength() { | |
return length; | |
} | |
private void normalizeVersion(String version) { | |
String[] split = PATTERN.split(version); | |
this.length=split.length; | |
StringBuilder sb = new StringBuilder(); | |
for (String s : split) { | |
sb.append(String.format(FORMAT_STRING, s)); | |
} | |
this.normalizedVersion=sb.toString(); | |
} | |
private String appendZeros(String normalizedVersion, Integer count) { | |
while(count-- > 0) { | |
normalizedVersion+=String.format(FORMAT_STRING, 0); | |
} | |
return normalizedVersion; | |
} | |
public Boolean isAfterOrEqual(Version version) { | |
String myVersion=getNormalizedVersion(); | |
String theirVersion=version.getNormalizedVersion(); | |
int diff=Math.abs(version.getLength() - this.getLength()); | |
if(version.getLength() > this.getLength()) { | |
myVersion=appendZeros(getNormalizedVersion(), diff); | |
} else if(version.getLength() < this.getLength()) { | |
theirVersion=appendZeros(version.getNormalizedVersion(), diff); | |
} | |
return theirVersion.compareTo(myVersion) <= 0; | |
} | |
public Boolean isBeforeOrEqual(Version version) { | |
String myVersion=getNormalizedVersion(); | |
String theirVersion=version.getNormalizedVersion(); | |
int diff=Math.abs(version.getLength() - this.getLength()); | |
if(version.getLength() > this.getLength()) { | |
myVersion=appendZeros(getNormalizedVersion(), diff); | |
} else if(version.getLength() < this.getLength()) { | |
theirVersion=appendZeros(version.getNormalizedVersion(), diff); | |
} | |
return theirVersion.compareTo(myVersion) >= 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment