灰度控制,比較兩個版本

/**
	 * 比較version1和version2的版本大小
	 * @param version1
	 * @param version2
	 * @param splitStr
	 * @return
	 * 返回version1-比較結果-version2
	 */
	public static CompareResult compareVersion(String version1,String version2,String splitStr){
		if (StringUtils.isBlank(version1)||StringUtils.isBlank(version2)) {
			return CompareResult.ILLEGEL;
		}
		String[] version1StrArr=version1.split(splitStr);
		String[] version2StrArr=version2.split(splitStr);
		int[] version1IntArr= new int[version1StrArr.length];
		int[] version2IntArr= new int[version2StrArr.length];
		for(int i=0;i<version1StrArr.length;i++){
			try {
				if (StringUtils.isBlank(version1StrArr[i])) {
					return CompareResult.ILLEGEL;
				}
				int version1Temp= NumberUtils.toInt(version1StrArr[i]);
				version1IntArr[i]=version1Temp;
			} catch (Exception e) {
				LOG.info("VersionCompareUtil.compareVersion exception:",e);
				return CompareResult.ILLEGEL;
			}
		}
		for(int i=0;i<version2StrArr.length;i++){
			try {
				if (StringUtils.isBlank(version2StrArr[i])) {
					return CompareResult.ILLEGEL;
				}
				int version2Temp=NumberUtils.toInt(version2StrArr[i]);
				version2IntArr[i]=version2Temp;
			} catch (Exception e) {
				LOG.info("VersionCompareUtil.compareVersion exception:",e);
				return CompareResult.ILLEGEL;
			}
		}
		int lessLength=version1IntArr.length>version2IntArr.length?version2IntArr.length:version1IntArr.length;
		for(int i=0;i<lessLength;i++){
			if (version1IntArr[i]>version2IntArr[i]) {
				return CompareResult.BIGGER_THAN;
			}else if (version1IntArr[i]<version2IntArr[i]) {
				return CompareResult.LESS_THAN;
			}
		}
		int extraVersion1=0;
		int extraVersion2=0;
		for(int i=lessLength;i<version1IntArr.length;i++){
			extraVersion1+=version1IntArr[i];
		}
		for(int i=lessLength;i<version2IntArr.length;i++){
			extraVersion2+=version2IntArr[i];
		}
		int extraCompareResult=extraVersion1-extraVersion2;
		return extraCompareResult==0?CompareResult.EQUAL:(extraCompareResult>0?CompareResult.BIGGER_THAN:CompareResult.LESS_THAN);
	}
相關文章
相關標籤/搜索