From 00c2aad58c6602e3f93125cce1bc2669533b0f05 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Wed, 15 Jan 2020 10:10:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=BC=96=E7=A0=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cm/common/utils/SortCodeUtil.java | 169 ++++++++++++++++-- 1 file changed, 158 insertions(+), 11 deletions(-) diff --git a/cloud-common/src/main/java/com/cm/common/utils/SortCodeUtil.java b/cloud-common/src/main/java/com/cm/common/utils/SortCodeUtil.java index b521cd7..78b2fcb 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/SortCodeUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/SortCodeUtil.java @@ -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; + } }