package jas2.util.pluginmanager;
import java.util.*;
/**This class is used to compare version numbers.
* The only method in this class converts both the currentversion and the latest version (method arguments)
* strings into string chunks (i.e. if string = 2.0.4, then ints are 2,0,4).
* The string chunks are then converted to ints and compared one at a time. If a latest version chunk
* is > than a current version chunk an update is available and UPDATEVERSION is returned. If all chunks are
* equal then NOUPDATEVERSION is available. If a latest chunk is < current chunk or Strings can't be converted
* to ints an Exception is thrown.
*/
public class VersionNumberCompare
{
public final static int VERSIONCHECKERROR =0;
public final static int UPDATEVERSION =1;
public final static int NOUPDATEVERSION =2;
/**This method converts both the currentversion and the latest version (method arguments)
* strings into string chunks (i.e. if string = 2.0.4, then ints are 2,0,4).
* The string chunks are then converted to ints and compared one at a time. If a latest version chunk
* is > than a current version chunk an update is available and UPDATEVERSION is returned. If all chunks are
* equal then NOUPDATEVERSION is available. The boolean throw exception controls whether the VersionNumberCompare
* exception will be thrown when a latestversion chunk is less than a current version chunk.
*/
public int compareVersions(String currentversion, String latestversion, boolean throwexception) throws VersionCompareException {
Vector currentversionchunks = new Vector();
Vector latestversionchunks = new Vector();
String chunk = "";
for(int i=0;i latestversionchunks.size()){
latestversionchunks.addElement("0");
}else if(currentversionchunks.size() < latestversionchunks.size()){
currentversionchunks.addElement("0");
}
}
int latestversionnum=0;
int currentversionnum=0;
//compare chunks as ints
for(int i=0;i currentversionnum){
return UPDATEVERSION;
}else if(latestversionnum < currentversionnum & throwexception){
throw new VersionCompareException();
}
}
//if made it this far then are running current version
return NOUPDATEVERSION;
}
class VersionCompareException extends Exception{
}
}
Ads help maintain this website.