import java.util.Set; import io.netty.util.internal.ConcurrentSet; /** * 获取key * * @project common-utils * @fileName GetKey.java * @Description * @author light-zhang * @date 2018-10-12 17:10:33 * @version 1.0.0 */ public final class GetKey { private static final String baseDigits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"; private static final int BASE = baseDigits.length(); private static final char[] digitsChar = baseDigits.toCharArray(); private static final int FAST_SIZE = 'z'; private static final int[] digitsIndex = new int[FAST_SIZE + 1]; static { for (int i = 0; i < FAST_SIZE; i++) { digitsIndex[i] = -1; } for (int i = 0; i < BASE; i++) { digitsIndex[digitsChar[i]] = i; } } /** * 参数解码 * * @param paramter * @return */ public static long decode(String paramter) { long result = 0L; synchronized (paramter) { long multiplier = 1; for (int pos = paramter.length() - 1; pos >= 0; pos--) { result += getIndex(paramter, pos) * multiplier; multiplier *= BASE; } } return result; } /** * 传入系统纳秒,防止重复 * * @param number * @return */ public static String encode(Long number) { final StringBuilder builder = new StringBuilder(); synchronized (number) { if (number < 0) new IllegalArgumentException("Number(Base64) must be positive: ".concat(number.toString())); if (number == 0) return "0"; while (number != 0) { builder.append(digitsChar[(int) (number % BASE)]); number /= BASE; } } return builder.toString(); } private static int getIndex(String paramter, int pmindex) { char c = paramter.charAt(pmindex); if (c > FAST_SIZE) { new IllegalArgumentException("Unknow character for Base64: ".concat(paramter)); } int index = digitsIndex[c]; if (index == -1) { new IllegalArgumentException("Unknow character for Base64: ".concat(paramter)); } return index; } public static void main(String[] args) { Set<String> data = new ConcurrentSet<String>(); for (int i = 0; i < 20000000; i++) { // System.out.println(encode(System.nanoTime())); data.add(encode(System.nanoTime())); // System.out.println(decode(UUID.randomUUID().toString())); } System.out.println(data.size()); // System.out.println(decode("6c7P6dzh")); } }