From eb3e3fa72b8561d094b8c824a88ee76930066774 Mon Sep 17 00:00:00 2001 From: luochuan Date: Sat, 27 May 2023 16:10:10 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E7=9F=AD=E9=93=BE=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alis/rpc/common/util/HashShortUrl.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java diff --git a/alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java b/alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java new file mode 100644 index 0000000..0e6aed9 --- /dev/null +++ b/alis-rpc/alis-rpc-common/src/main/java/org/alis/rpc/common/util/HashShortUrl.java @@ -0,0 +1,43 @@ +package org.alis.rpc.common.util; + +import cn.hutool.core.lang.hash.MurmurHash; + +import java.util.stream.IntStream; + +/** + * @author lc + * @date 2023/5/27 15:06 + */ +public class HashShortUrl{ + + private static final String[] CHARS = new String[62]; + + static { + IntStream.range(0, 10).forEach(i -> CHARS[i] = String.valueOf(i)); + for (int i = 1; i <= 26; i++) { + CHARS[9 + i] = String.valueOf((char) (i + 64)); + } + for (int i = 1; i <= 26; i++) { + CHARS[35 + i] = String.valueOf((char) (i + 96)); + } + + } + + + public String generator(String old) { + int i = MurmurHash.hash32(old); + if (i < 0) { + i = Math.abs(i); + } + int num = Integer.MAX_VALUE - i; + StringBuilder sb = new StringBuilder(); + while (num > 0) { + int x = num % CHARS.length; + sb.append(CHARS[x]); + num /= CHARS.length; + } + return sb.reverse().toString(); + + + } +}