All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eleme.openapi.sdk.media.trace.ShorterUUID Maven / Gradle / Ivy

There is a newer version: 1.30.71
Show newest version
package eleme.openapi.sdk.media.trace;

import java.util.UUID;

/**
 * Created by huamulou on 16/1/12.
 */
public class ShorterUUID {

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

    /**
     * 以62进制(字母加数字)生成19位UUID,最短的UUID
     *
     * @return uuid
     */
    public static String uuid() {
        UUID uuid = UUID.randomUUID();
        StringBuilder sb = new StringBuilder();
        sb.append(digits(uuid.getMostSignificantBits() >> 32, 8));
        sb.append(digits(uuid.getMostSignificantBits() >> 16, 4));
        sb.append(digits(uuid.getMostSignificantBits(), 4));
        sb.append(digits(uuid.getLeastSignificantBits() >> 48, 4));
        sb.append(digits(uuid.getLeastSignificantBits(), 12));
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy