【fix sonar】YouBianCodeUtil文件

This commit is contained in:
梁琦涛
2023-03-06 11:36:22 +08:00
parent 20ddd56f93
commit e14fe992bc
@@ -2,6 +2,8 @@ package com.jero.common.util;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import java.util.Objects;
/** /**
* 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数) * 流水号生成规则(按默认规则递增,数字从1-99开始递增,数字到99,递增字母;位数不够增加位数)
* A001 * A001
@@ -11,11 +13,15 @@ import io.netty.util.internal.StringUtil;
*/ */
public class YouBianCodeUtil { public class YouBianCodeUtil {
private YouBianCodeUtil(){
}
// 数字位数(默认生成3位的数字) // 数字位数(默认生成3位的数字)
//代表数字位数
private static final int NUM_LENGTH = 2;
private static final int numLength = 2;//代表数字位数 public static final int ZHANWEI_LENGTH = 1 + NUM_LENGTH;
public static final int zhanweiLength = 1+numLength;
/** /**
* 根据前一个code,获取同级下一个code * 根据前一个code,获取同级下一个code
@@ -31,28 +37,28 @@ public class YouBianCodeUtil {
String num = getStrNum(1); String num = getStrNum(1);
newcode = zimu + num; newcode = zimu + num;
} else { } else {
String before_code = code.substring(0, code.length() - 1- numLength); String before_code = code.substring(0, code.length() - 1- NUM_LENGTH);
String after_code = code.substring(code.length() - 1 - numLength,code.length()); String after_code = code.substring(code.length() - 1 - NUM_LENGTH,code.length());
char after_code_zimu = after_code.substring(0, 1).charAt(0); char after_code_zimu = after_code.substring(0, 1).charAt(0);
Integer after_code_num = Integer.parseInt(after_code.substring(1)); Integer after_code_num = Integer.parseInt(after_code.substring(1));
String nextNum = ""; String nextNum = "";
char nextZimu = 'A'; char nextZimu = 'A';
// 先判断数字等于999*,则计数从1重新开始,递增 // 先判断数字等于999*,则计数从1重新开始,递增
if (after_code_num == getMaxNumByLength(numLength)) { if (after_code_num == getMaxNumByLength(NUM_LENGTH)) {
nextNum = getNextStrNum(0); nextNum = getNextStrNum(0);
} else { } else {
nextNum = getNextStrNum(after_code_num); nextNum = getNextStrNum(after_code_num);
} }
// 先判断数字等于999*,则字母从A重新开始,递增 // 先判断数字等于999*,则字母从A重新开始,递增
if(after_code_num == getMaxNumByLength(numLength)) { if(after_code_num == getMaxNumByLength(NUM_LENGTH)) {
nextZimu = getNextZiMu(after_code_zimu); nextZimu = getNextZiMu(after_code_zimu);
}else{ }else{
nextZimu = after_code_zimu; nextZimu = after_code_zimu;
} }
// 例如Z99,下一个code就是Z99A01 // 例如Z99,下一个code就是Z99A01
if ('Z' == after_code_zimu && getMaxNumByLength(numLength) == after_code_num) { if ('Z' == after_code_zimu && getMaxNumByLength(NUM_LENGTH) == after_code_num) {
newcode = code + (nextZimu + nextNum); newcode = code + (nextZimu + nextNum);
} else { } else {
newcode = before_code + (nextZimu + nextNum); newcode = before_code + (nextZimu + nextNum);
@@ -74,7 +80,7 @@ public class YouBianCodeUtil {
* @return * @return
*/ */
public static synchronized String getSubYouBianCode(String parentCode,String localCode) { public static synchronized String getSubYouBianCode(String parentCode,String localCode) {
if(localCode!=null && localCode!=""){ if(localCode!=null && Objects.equals("",localCode)){
// return parentCode + getNextYouBianCode(localCode); // return parentCode + getNextYouBianCode(localCode);
return getNextYouBianCode(localCode); return getNextYouBianCode(localCode);
@@ -104,7 +110,7 @@ public class YouBianCodeUtil {
* @return * @return
*/ */
private static String getStrNum(int num) { private static String getStrNum(int num) {
String s = String.format("%0" + numLength + "d", num); String s = String.format("%0" + NUM_LENGTH + "d", num);
return s; return s;
} }
@@ -122,7 +128,7 @@ public class YouBianCodeUtil {
/** /**
* 递增获取下个字母 * 递增获取下个字母
* *
* @param num * @param zimu
* @return * @return
*/ */
private static char getNextZiMu(char zimu) { private static char getNextZiMu(char zimu) {
@@ -150,13 +156,13 @@ public class YouBianCodeUtil {
} }
public static String[] cutYouBianCode(String code){ public static String[] cutYouBianCode(String code){
if(code==null || StringUtil.isNullOrEmpty(code)){ if(code==null || StringUtil.isNullOrEmpty(code)){
return null; return new String[0];
}else{ }else{
//获取标准长度为numLength+1,截取的数量为code.length/numLength+1 //获取标准长度为numLength+1,截取的数量为code.length/numLength+1
int c = code.length()/(numLength+1); int c = code.length()/(NUM_LENGTH+1);
String[] cutcode = new String[c]; String[] cutcode = new String[c];
for(int i =0 ; i <c;i++){ for(int i =0 ; i <c;i++){
cutcode[i] = code.substring(0,(i+1)*(numLength+1)); cutcode[i] = code.substring(0,(i+1)*(NUM_LENGTH+1));
} }
return cutcode; return cutcode;
} }