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"));
        }
    }
  • 相关阅读:
    ReactiveCocoa 谈谈RACMulticastConnection
    ReactiveCocoa 谈谈concat
    Swift 一些环境配置
    hpple 简单使用
    Swift 学习手记1,pod 的 类库使用
    [转]SQL语句:Group By总结
    Jquery VailDate初探
    C#RSA加密解密详解
    电子印章制作管理系统 -升级版本
    tensorflow 实现的第一个目标检测模型
  • 原文地址:https://www.cnblogs.com/light-zhang/p/9779337.html
Copyright © 2011-2022 走看看