如何在Java 1.4中创建UUID或GUID

时间:2011-07-27 01:22:25

标签: java uuid

我需要在java 1.4中创建一个UUID或GUID。我使用时遇到classnotfound异常:java.util.UUID。

下面链接有类似的问题,但没有一个用Java 1.4的生成器回答:

我也在网上找了几节课:

您建议我在java 1.4中使用什么来创建UUID或GUID?

5 个答案:

答案 0 :(得分:6)

我认为没有机会说服客户端下载不受支持的Java版本?如果答案是否定的话,那么你唯一的办法是使用/修改网络上的一个开源实现。你在问题中提到了其中两个,你可能想看的另一个是JUG

哦,是的,您对java.util.UUID的引用失败了,因为它仅在Java 5及更高版本中可用。

祝你好运!

答案 1 :(得分:3)

自1.5以来,java.util.UUID被添加到JDK。

对于简单的轻量级实现,请查看此博客文章:http://lzkyo.iteye.com/blog/453120

答案 2 :(得分:1)

Apache Commons ID(沙箱项目,因此您必须从源代码构建,但我已经使用它并且它可以工作):project pagesvn repo

你也可以查看试试this project,但我还没有使用它。

答案 3 :(得分:0)

使用SAP ISA中的TechKey功能。这就是它的用途。它还可以与ABAP表以及任何Portal和SAP J2EE表兼容。

答案 4 :(得分:0)

不幸的是,UUID类从1.5开始可用。我刚刚实现了该实用程序类,该实用程序类将UUID创建为 String 。随意使用和共享。我希望它可以帮助其他人!

from nltk.corpus import wordnet
nltk.download('wordnet')    
w1 = wordnet.synset("price" '.n.01')  #wordnet.lemmas(i2)[0]
w2 = wordnet.synset("amount" + '.n.01') 
print(w1.wup_similarity(w2))

这是输出:


package your.package.name;

import java.security.SecureRandom;
import java.util.Random;

/**
 * Utility class that creates random-based UUIDs as Strings.
 * 
 */
public abstract class RandomUuidStringCreator {

    private static final int RANDOM_VERSION = 4;

    /**
     * Returns a random-based UUID as String.
     * 
     * It uses a thread local {@link SecureRandom}.
     * 
     * @return a random-based UUID string
     */
    public static String getRandomUuid() {
        return getRandomUuid(SecureRandomLazyHolder.SECURE_RANDOM);
    }

    /**
     * Returns a random-based UUID String.
     * 
     * It uses any instance of {@link Random}.
     * 
     * @return a random-based UUID string
     */
    public static String getRandomUuid(Random random) {

        long msb = 0;
        long lsb = 0;

        // (3) set all bit randomly
        if (random instanceof SecureRandom) {
            // Faster for instances of SecureRandom
            final byte[] bytes = new byte[16];
            random.nextBytes(bytes);
            msb = toNumber(bytes, 0, 8); // first 8 bytes for MSB
            lsb = toNumber(bytes, 8, 16); // last 8 bytes for LSB
        } else {
            msb = random.nextLong(); // first 8 bytes for MSB
            lsb = random.nextLong(); // last 8 bytes for LSB
        }

        // Apply version and variant bits (required for RFC-4122 compliance)
        msb = (msb & 0xffffffffffff0fffL) | (RANDOM_VERSION & 0x0f) << 12; // apply version bits
        lsb = (lsb & 0x3fffffffffffffffL) | 0x8000000000000000L; // apply variant bits

        // Convert MSB and LSB to hexadecimal
        String msbHex = zerofill(Long.toHexString(msb), 16);
        String lsbHex = zerofill(Long.toHexString(lsb), 16);

        // Return the UUID
        return format(msbHex + lsbHex);
    }

    private static long toNumber(final byte[] bytes, final int start, final int length) {
        long result = 0;
        for (int i = start; i < length; i++) {
            result = (result << 8) | (bytes[i] & 0xff);
        }
        return result;
    }

    private static String zerofill(String string, int length) {
        return new String(lpad(string.toCharArray(), length, '0'));
    }

    private static char[] lpad(char[] chars, int length, char fill) {

        int delta = 0;
        int limit = 0;

        if (length > chars.length) {
            delta = length - chars.length;
            limit = length;
        } else {
            delta = 0;
            limit = chars.length;
        }

        char[] output = new char[chars.length + delta];
        for (int i = 0; i < limit; i++) {
            if (i < delta) {
                output[i] = fill;
            } else {
                output[i] = chars[i - delta];
            }
        }
        return output;
    }

    private static String format(String string) {
        char[] input = string.toCharArray();
        char[] output = new char[36];

        System.arraycopy(input, 0, output, 0, 8);
        System.arraycopy(input, 8, output, 9, 4);
        System.arraycopy(input, 12, output, 14, 4);
        System.arraycopy(input, 16, output, 19, 4);
        System.arraycopy(input, 20, output, 24, 12);

        output[8] = '-';
        output[13] = '-';
        output[18] = '-';
        output[23] = '-';

        return new String(output);
    }

    // Holds lazy secure random
    private static class SecureRandomLazyHolder {
        static final Random SECURE_RANDOM = new SecureRandom();
    }

    /**
     * For tests!
     */
    public static void main(String[] args) {

        System.out.println("// Using thread local `java.security.SecureRandom` (DEFAULT)");
        System.out.println("RandomUuidCreator.getRandomUuid()");
        System.out.println();
        for (int i = 0; i < 5; i++) {
            System.out.println(RandomUuidStringCreator.getRandomUuid());
        }

        System.out.println();
        System.out.println("// Using `java.util.Random` (FASTER)");
        System.out.println("RandomUuidCreator.getRandomUuid(new Random())");
        System.out.println();
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            System.out.println(RandomUuidStringCreator.getRandomUuid(random));
        }
    }
}