新增获取上一个编码方法

This commit is contained in:
wenc000 2020-01-15 10:10:38 +08:00
parent 18eac06445
commit 00c2aad58c

View File

@ -1,7 +1,5 @@
package com.cm.common.utils;
import org.springframework.security.core.parameters.P;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
@ -17,13 +15,13 @@ public class SortCodeUtil {
/**
* 获取排序Code
*
* @param lastCode 上一个编码第一个null
* @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;
public static String getNextSortCode(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()) {
@ -55,7 +53,7 @@ public class SortCodeUtil {
}
/**
* 下一个编码编码大小为 36^sectionLength
* 下一个编码编码大小为 36^sectionLength最小 0*sectionLength
*
* @param code 编码
* @param sectionLength 长度
@ -88,12 +86,12 @@ public class SortCodeUtil {
}
/**
* 下一个字
* 下一个字
*
* @param currentChar
* @return
*/
public static char nextChar(char currentChar) {
private static char nextChar(char currentChar) {
char nextChar = '0';
if ('0' == currentChar) {
nextChar = '1';
@ -169,10 +167,159 @@ public class SortCodeUtil {
return nextChar;
}
public static void main(String[] args) {
String str = "ZZ0-ZZZ-ZZZ-0ZZ";
System.out.println(getSortCode(str, "-", 3));
/**
* 获取排序Code
*
* @param nextCode 下一个编码获取最后一个值为null即可
* @param separator 分隔符 默认"-"
* @param sectionLength 片段长度 默认长度3
* @return
*/
public static String getPrevSortCode(String nextCode, String separator, int sectionLength) {
separator = ((separator == null) || separator.isEmpty()) ? "-" : separator;
sectionLength = sectionLength <= 0 ? 3 : sectionLength;
String borrowFlag = String.format("%" + sectionLength + "s", "Z").replaceAll("\\s", "Z");
if (nextCode == null || nextCode.isEmpty()) {
return borrowFlag;
}
// 获取分段数组
String[] sectionArray = nextCode.split(separator);
boolean hasBorrow = true;
String resultSortCode = "";
for (int i = sectionArray.length - 1; i >= 0; i--) {
String section = sectionArray[i];
if (hasBorrow) {
String prevCode = prevCode(section, sectionLength);
if (resultSortCode.isEmpty()) {
resultSortCode = prevCode;
} else {
resultSortCode = prevCode + separator + resultSortCode;
}
if (borrowFlag.equals(prevCode)) {
hasBorrow = true;
continue;
}
hasBorrow = false;
} else {
resultSortCode = section + separator + resultSortCode;
}
}
return resultSortCode;
}
/**
* 上一个编码编码大小为 36^sectionLength最大 Z*sectionLength
*
* @param code 编码
* @param sectionLength 长度
* @return
*/
public static String prevCode(String code, int sectionLength) {
if (code == null || code.isEmpty()) {
return String.format("%" + sectionLength + "s", "Z").replaceAll("\\s", "Z");
}
code = code.toUpperCase();
char[] charArray = code.toCharArray();
boolean hasBorrow = true;
String resultCode = "";
for (int i = charArray.length - 1; i >= 0; i--) {
char currentChar = charArray[i];
if (hasBorrow) {
char prevChar = prevChar(currentChar);
resultCode = prevChar + resultCode;
if ('Z' == prevChar) {
hasBorrow = true;
continue;
}
hasBorrow = false;
} else {
// 没有发生借位直接
resultCode = currentChar + resultCode;
}
}
return resultCode;
}
/**
* 上一个字符
*
* @param currentChar
* @return
*/
private static char prevChar(char currentChar) {
char prevChar = 'Z';
if ('1' == currentChar) {
prevChar = '0';
} else if ('2' == currentChar) {
prevChar = '1';
} else if ('3' == currentChar) {
prevChar = '2';
} else if ('4' == currentChar) {
prevChar = '3';
} else if ('5' == currentChar) {
prevChar = '4';
} else if ('6' == currentChar) {
prevChar = '5';
} else if ('7' == currentChar) {
prevChar = '6';
} else if ('8' == currentChar) {
prevChar = '7';
} else if ('9' == currentChar) {
prevChar = '8';
} else if ('A' == currentChar) {
prevChar = '9';
} else if ('B' == currentChar) {
prevChar = 'A';
} else if ('C' == currentChar) {
prevChar = 'B';
} else if ('D' == currentChar) {
prevChar = 'C';
} else if ('E' == currentChar) {
prevChar = 'D';
} else if ('F' == currentChar) {
prevChar = 'E';
} else if ('G' == currentChar) {
prevChar = 'F';
} else if ('H' == currentChar) {
prevChar = 'G';
} else if ('I' == currentChar) {
prevChar = 'H';
} else if ('J' == currentChar) {
prevChar = 'I';
} else if ('K' == currentChar) {
prevChar = 'J';
} else if ('L' == currentChar) {
prevChar = 'K';
} else if ('M' == currentChar) {
prevChar = 'L';
} else if ('N' == currentChar) {
prevChar = 'M';
} else if ('O' == currentChar) {
prevChar = 'N';
} else if ('P' == currentChar) {
prevChar = 'O';
} else if ('Q' == currentChar) {
prevChar = 'P';
} else if ('R' == currentChar) {
prevChar = 'Q';
} else if ('S' == currentChar) {
prevChar = 'R';
} else if ('T' == currentChar) {
prevChar = 'S';
} else if ('U' == currentChar) {
prevChar = 'T';
} else if ('V' == currentChar) {
prevChar = 'U';
} else if ('W' == currentChar) {
prevChar = 'V';
} else if ('X' == currentChar) {
prevChar = 'W';
} else if ('Y' == currentChar) {
prevChar = 'X';
} else if ('Z' == currentChar) {
prevChar = 'Y';
}
return prevChar;
}
}