新增自定义排序工具类

This commit is contained in:
wenc000 2020-01-14 23:43:16 +08:00
parent 0e8934fe21
commit 18eac06445

View File

@ -0,0 +1,178 @@
package com.cm.common.utils;
import org.springframework.security.core.parameters.P;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: SortCodeUtil
* @Description: 排序编码工具
* @Author: WangGeng
* @Date: 2020/1/14 10:21 下午
* @Version: 1.0
**/
public class SortCodeUtil {
/**
* 获取排序Code
*
* @param lastCode 上一个编码第一个null
* @param separator 分隔符 默认"-"
* @param sectionLength 片段长度 默认长度3
* @return
*/
public static String getSortCode(String lastCode, String separator, int sectionLength) {
separator = separator == null || separator.isEmpty() ? "-" : separator;
sectionLength = sectionLength <= 0 ? 3 : sectionLength;
String carryFlag = String.format("%0" + sectionLength + "d", 0);
if (lastCode == null || lastCode.isEmpty()) {
return carryFlag;
}
// 获取分段数组
String[] sectionArray = lastCode.split(separator);
boolean hasCarry = true;
String resultSortCode = "";
for (int i = sectionArray.length - 1; i >= 0; i--) {
String section = sectionArray[i];
if (hasCarry) {
String nextCode = nextCode(section, sectionLength);
if (resultSortCode.isEmpty()) {
resultSortCode = nextCode;
} else {
resultSortCode = nextCode + separator + resultSortCode;
}
if (carryFlag.equals(nextCode)) {
hasCarry = true;
continue;
}
hasCarry = false;
} else {
resultSortCode = section + separator + resultSortCode;
}
}
return resultSortCode;
}
/**
* 下一个编码编码大小为 36^sectionLength
*
* @param code 编码
* @param sectionLength 长度
* @return
*/
public static String nextCode(String code, int sectionLength) {
if (code == null || code.isEmpty()) {
return String.format("%0" + sectionLength + "d", 0);
}
code = code.toUpperCase();
char[] charArray = code.toCharArray();
boolean hasCarry = true;
String resultCode = "";
for (int i = charArray.length - 1; i >= 0; i--) {
char currentChar = charArray[i];
if (hasCarry) {
char nextChar = nextChar(currentChar);
resultCode = nextChar + resultCode;
if ('0' == nextChar) {
hasCarry = true;
continue;
}
hasCarry = false;
} else {
// 没有发生进位直接相加
resultCode = currentChar + resultCode;
}
}
return resultCode;
}
/**
* 下一个字节
*
* @param currentChar
* @return
*/
public static char nextChar(char currentChar) {
char nextChar = '0';
if ('0' == currentChar) {
nextChar = '1';
} else if ('1' == currentChar) {
nextChar = '2';
} else if ('2' == currentChar) {
nextChar = '3';
} else if ('3' == currentChar) {
nextChar = '4';
} else if ('4' == currentChar) {
nextChar = '5';
} else if ('5' == currentChar) {
nextChar = '6';
} else if ('6' == currentChar) {
nextChar = '7';
} else if ('7' == currentChar) {
nextChar = '8';
} else if ('8' == currentChar) {
nextChar = '9';
} else if ('9' == currentChar) {
nextChar = 'A';
} else if ('A' == currentChar) {
nextChar = 'B';
} else if ('B' == currentChar) {
nextChar = 'C';
} else if ('C' == currentChar) {
nextChar = 'D';
} else if ('D' == currentChar) {
nextChar = 'E';
} else if ('E' == currentChar) {
nextChar = 'F';
} else if ('F' == currentChar) {
nextChar = 'G';
} else if ('G' == currentChar) {
nextChar = 'H';
} else if ('H' == currentChar) {
nextChar = 'I';
} else if ('I' == currentChar) {
nextChar = 'J';
} else if ('J' == currentChar) {
nextChar = 'K';
} else if ('K' == currentChar) {
nextChar = 'L';
} else if ('L' == currentChar) {
nextChar = 'M';
} else if ('M' == currentChar) {
nextChar = 'N';
} else if ('N' == currentChar) {
nextChar = 'O';
} else if ('O' == currentChar) {
nextChar = 'P';
} else if ('P' == currentChar) {
nextChar = 'Q';
} else if ('Q' == currentChar) {
nextChar = 'R';
} else if ('R' == currentChar) {
nextChar = 'S';
} else if ('S' == currentChar) {
nextChar = 'T';
} else if ('T' == currentChar) {
nextChar = 'U';
} else if ('U' == currentChar) {
nextChar = 'V';
} else if ('V' == currentChar) {
nextChar = 'W';
} else if ('W' == currentChar) {
nextChar = 'X';
} else if ('X' == currentChar) {
nextChar = 'Y';
} else if ('Y' == currentChar) {
nextChar = 'Z';
}
return nextChar;
}
public static void main(String[] args) {
String str = "ZZ0-ZZZ-ZZZ-0ZZ";
System.out.println(getSortCode(str, "-", 3));
}
}