Compare two version numbers to a new version

Source: Internet
Author: User

/**
* Version Information
*
* @ Class Name: meichat_version
* @ Author: Kevin
* @ creation time: 2016-10-2 8:25:13
*/
public class Meichat_version extends Basicservlet {

Private static final long serialversionuid = -7537429324065779134l;

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
String CurrentVersion = "";
String model = "";
if (null! = Request.getparameter ("CurrentVersion"))
CurrentVersion = Request.getparameter ("CurrentVersion");
if (null! = Request.getparameter ("model"))
Model = Request.getparameter ("model");

Mongodbdao DAO = Mongodbdao.getmongodbdaoinstance ();
Basicdbobject query = new Basicdbobject ();
Query.put ("name", "app");
Query.put ("number", "V" + currentversion);
Query.put ("model", model);

Jsonarray jar = dao.find (query, "yl_version");
StringBuffer sb = new StringBuffer ();
SB = Getstringbbuffer (jar, CurrentVersion);
String str = getjsonappend (sb.tostring (), Msgcode.mes_0,-1);
WriteTo (response, str);
}

Private StringBuffer Getstringbbuffer (Jsonarray jar, String version1) {
String flag = "N";//no forced upgrade required
String dep = "";
String status = "0";
String time = "";
String number = "";
String verify = "";
StringBuffer sb = new StringBuffer ();
Jsonobject returnjsob = new Jsonobject ();
if (jar.size () > 0) {
Jsonobject jsob = jar.getjsonobject (0);

Long CTime = Jsob.getlong ("Time");

Mongodbdao dao = Mongodbdao.getmongodbdaoinstance ();
Jsonarray jarver = dao.find (New Basicdbobject ("Time",
New Basicdbobject ("$gte", CTime), New Basicdbobject (
"Time", 1), "Yl_version", 1000);//-1 Descending, 1 ascending
//Determine if there is a version of the forced upgrade between the current version and the latest version
for (int i = 0; i < jarver.size (); i++) {
Jsonobject jsobver = Jarver.getjsonobject (i);
Jsobver.getstring ("number"). Replace ("V", "");
if ("2". Equals (Jsobver.getstring ("status"))) {
Jsonobject maxobj = Jarver.getjsonobject (Jarver.size ()-1);
Status = "2";
dep = Get (Maxobj, "DEP");
Time = Get (Maxobj, "time");
Number = Get (Maxobj, "number");
Verify = Get (Maxobj, "verify");
Flag = "Y";
break;
}
}
//Determine if you need to upgrade
if ("N". Equals (flag)) {
Jsonobject maxobj = Jarver.getjsonobject (Jarver.size ()-1);
String Version2 = maxobj.getstring ("number"). Replace ("V", "");
//Determine if the current version is up to date
int intvalue = compareversion (Version1, version2);

if (intvalue >= 0)
Status = "0";
Else
Status = "1";
DEP = Get (Maxobj, "DEP");
Time = Get (Maxobj, "time");
Number = Get (Maxobj, "number");
Verify = Get (Maxobj, "verify");
}
}

Returnjsob.put ("DEP", DEP);
Returnjsob.put ("status", status);
Returnjsob.put ("Time", time);
Returnjsob.put ("number", number);
Returnjsob.put ("Verify", verify);

Returnjsob.put ("url", Mongoutil.getvaluebykey ("Download_url"));
Sb.append ("\" version\ ":"). Append (Returnjsob). Append (",");

return SB;
}

/**
* Compare size of two versions
*
* @param version1
* @param version2
* @return
*/
public int Compareversio N (String version1, String version2) {
if (version1.equals (Version2)) {
return 0;
}
string[] Version1array = version1.split ("\ \");
string[] Version2array = version2.split ("\ \");
int index = 0;
//Get Minimum length value
int minlen = Math.min (Version1array.length, version2array.length);
int diff = 0;
//loop to determine the size of each bit
while (Index < Minlen
&& (diff = integer.parseint (version1array[index))
- Integer.parseint (Version2array[index])) = = 0) {
index++;
}
if (diff = = 0) {
//if the number of digits is not consistent, compare the number of extra digits, and index that large will enter the for loop
for (int i = index; i < version1array.length; i++) {
if (Integer.parseint (version1array[i]) > 0) {
return 1;
}
}

for (int i = index; i < version2array.length; i++) {
if (Integer.parseint (Version2array[i]) > 0) {
return-1;
}
}
return 0;
} else {
Return diff > 0? 1:-1;
}
}

}

Compare two version numbers to a new version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.