zoukankan      html  css  js  c++  java
  • 获取随机key

    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"));
        }
    }
  • 相关阅读:
    常见的单链表题目
    SpringBoot Hello
    IDEA 重置
    lombok的用法
    软件测试系列白盒测试覆盖率的问题
    软件测试系列软件测试基础
    Linux常用命令1对文件进行查看、复制、移动和分割
    软件测试系列通用测试用例写作
    Java继承特性
    Linux常用命令3如何设置IP地址?如何更改系统时间?
  • 原文地址:https://www.cnblogs.com/light-zhang/p/9779337.html
Copyright © 2011-2022 走看看