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"));
        }
    }
  • 相关阅读:
    YII中表单验证
    YII中的表单挂件
    YII数据库操作(CURD操作)
    YII中的session和cookie
    YII中面包屑制作(当前位置:网站首页 >> 会员登陆)
    ios NSFileManager 用法详解
    iOS沙盒路径的查看和使用
    iOS7改变状态栏文字颜色
    iOS常用第三方库 -转
    Linux下mysql新建账号及权限设置各种方式总结
  • 原文地址:https://www.cnblogs.com/light-zhang/p/9779337.html
Copyright © 2011-2022 走看看