uuid新建 获取bean实例工具类

82 阅读2分钟

uuid新建


import .MicroUUID;
public class SnowFlakeUtils {
    public SnowFlakeUtils() {}
    public long nextId() {
        return MicroUUID.randomUUID15Long();
    }
}

MicroUUID

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package .utils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public enum MicroUUID {
    UUID15 {
        public Object getId() {
            return MicroUUID.randomUUID15();
        }
    },
    UUID15_LONG {
        public Object getId() {
            return MicroUUID.randomUUID15Long();
        }
    },
    UUID19 {
        public Object getId() {
            return MicroUUID.randomUUID19();
        }
    },
    UUID32 {
        public Object getId() {
            return MicroUUID.randomUUID32();
        }
    },
    UUID {
        public Object getId() {
            return MicroUUID.randomUUID();
        }
    };

    private static final String STR_BASE = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final char[] DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    private static final Map<Character, Integer> DIGIT_MAP = new HashMap();
    private static final int MIN_RADIX = 2;
    private static final int MAX_RADIX = DIGITS.length;

    private MicroUUID() {
    }

    public static String randomUUID() {
        return java.util.UUID.randomUUID().toString();
    }

    public static String randomUUID32() {
        return java.util.UUID.randomUUID().toString().replace("-", "");
    }

    public static String randomUUID19() {
        UUID uuid = java.util.UUID.randomUUID();
        return digits(uuid.getMostSignificantBits() >> 32, 8) + digits(uuid.getMostSignificantBits() >> 16, 4) + digits(uuid.getMostSignificantBits(), 4) + digits(uuid.getLeastSignificantBits() >> 48, 4) + digits(uuid.getLeastSignificantBits(), 12);
    }

    public static String randomUUID15() {
        return MicroUUID.UUIDMaker.generate();
    }

    public static long randomUUID15Long() {
        return toNumber(randomUUID15(), 10);
    }

    public static String randomUUIDBase64() {
        UUID uuid = java.util.UUID.randomUUID();
        byte[] byUuid = new byte[16];
        long least = uuid.getLeastSignificantBits();
        long most = uuid.getMostSignificantBits();
        long2bytes(most, byUuid, 0);
        long2bytes(least, byUuid, 8);
        return Base64.getEncoder().encodeToString(byUuid);
    }

    private static void long2bytes(long value, byte[] bytes, int offset) {
        for(int i = 7; i > -1; --i) {
            bytes[offset++] = (byte)((int)(value >> 8 * i & 255L));
        }

    }

    private static long toNumber(String s, int radix) {
        if (s == null) {
            throw new NumberFormatException("null");
        } else if (radix < 2) {
            throw new NumberFormatException("radix " + radix + " less than Numbers.MIN_RADIX");
        } else if (radix > MAX_RADIX) {
            throw new NumberFormatException("radix " + radix + " greater than Numbers.MAX_RADIX");
        } else {
            boolean negative = false;
            Integer i = 0;
            Integer len = s.length();
            long result = 0L;
            long limit = -9223372036854775807L;
            if (len <= 0) {
                throw forInputString(s);
            } else {
                char firstChar = s.charAt(0);
                if (firstChar < '0') {
                    if (firstChar == '-') {
                        negative = true;
                        limit = Long.MIN_VALUE;
                    } else if (firstChar != '+') {
                        throw forInputString(s);
                    }

                    if (len == 1) {
                        throw forInputString(s);
                    }

                    i = i + 1;
                }

                Integer digit;
                for(long multmin = limit / (long)radix; i < len; result -= (long)digit) {
                    Map var10000 = DIGIT_MAP;
                    Integer var13 = i;
                    i = i + 1;
                    digit = (Integer)var10000.get(s.charAt(var13));
                    if (digit == null || digit < 0 || result < multmin) {
                        throw forInputString(s);
                    }

                    result *= (long)radix;
                    if (result < limit + (long)digit) {
                        throw forInputString(s);
                    }
                }

                return negative ? result : -result;
            }
        }
    }

    private static String toString(long num, int radix) {
        if (radix < 2 || radix > MAX_RADIX) {
            radix = 10;
        }

        if (radix == 10) {
            return Long.toString(num);
        } else {
            int size = true;
            int charPos = 64;
            char[] buf = new char[65];
            boolean negative = num < 0L;
            if (!negative) {
                num = -num;
            }

            while(num <= (long)(-radix)) {
                buf[charPos--] = DIGITS[(int)(-(num % (long)radix))];
                num /= (long)radix;
            }

            buf[charPos] = DIGITS[(int)(-num)];
            if (negative) {
                --charPos;
                buf[charPos] = '-';
            }

            return new String(buf, charPos, 65 - charPos);
        }
    }

    private static String digits(long val, int digits) {
        long hi = 1L << digits * 4;
        return toString(hi | val & hi - 1L, MAX_RADIX).substring(1);
    }

    private static NumberFormatException forInputString(String s) {
        return new NumberFormatException("For input string: " + s);
    }

    public abstract Object getId();

    static {
        for(int i = 0; i < DIGITS.length; ++i) {
            DIGIT_MAP.put(DIGITS[i], i);
        }

    }

    private static class UUIDMaker {
        private static final String STR = "0123456789abcdefghijklmnopqrstuvwxyz";
        private static final int PIX_LEN = "0123456789abcdefghijklmnopqrstuvwxyz".length();
        private static volatile int pixOne = 0;
        private static volatile int pixTwo = 0;
        private static volatile int pixThree = 0;
        private static volatile int pixFour = 0;

        private UUIDMaker() {
        }

        private static synchronized String generate() {
            String hexString = Long.toHexString(System.currentTimeMillis());
            ++pixFour;
            if (pixFour == PIX_LEN) {
                pixFour = 0;
                ++pixThree;
                if (pixThree == PIX_LEN) {
                    pixThree = 0;
                    ++pixTwo;
                    if (pixTwo == PIX_LEN) {
                        pixTwo = 0;
                        ++pixOne;
                        if (pixOne == PIX_LEN) {
                            pixOne = 0;
                        }
                    }
                }
            }

            return hexString + "0123456789abcdefghijklmnopqrstuvwxyz".charAt(pixOne) + "0123456789abcdefghijklmnopqrstuvwxyz".charAt(pixTwo) + "0123456789abcdefghijklmnopqrstuvwxyz".charAt(pixThree) + "0123456789abcdefghijklmnopqrstuvwxyz".charAt(pixFour);
        }
    }
}

获取bean实例工具类

import javax.annotation.Nonnull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;

public final class SpringContextUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public SpringContextUtils() {
    }

    public static ApplicationContext getContext() {
        return applicationContext;
    }

    public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;
    }

    public static class SpringContextAutoConfiguration {
        public SpringContextAutoConfiguration() {
        }

        @Bean
        public SpringContextUtils springContextUtils() {
            return new SpringContextUtils();
        }
    }
}