Code example for UUID

Methods: getLeastSignificantBitsgetMostSignificantBits

0
UUID r1 = UUID.randomUUID();
            UUID r2 = UUID.randomUUID();
 
            testCompare(r1, r2, compareUUID(r1, r2));
            testCompare(r1, r1, 0);
            testCompare(r2, r2, 0);
 
            testCompare(t1, r1, -1);
            testCompare(r2, t2, 1);
        } 
    } 
 
    public static int compareUnsigned(long n1, long n2)
    { 
        if (n1 == n2)
        { 
            return 0; 
        } 
        if ((n1 < n2) ^ ((n1 < 0) != (n2 < 0)))
        { 
            return -1; 
        } 
        return 1; 
    } 
 
    public static int compareUUID(UUID u1, UUID u2)
    { 
        int c = compareUnsigned(u1.getMostSignificantBits(),
                u2.getMostSignificantBits());
        if (c != 0)
        { 
            return c;
        } 
        return compareUnsigned(u1.getLeastSignificantBits(),
                u2.getLeastSignificantBits());
    } 
 
    public String describeCompare(UUID u1, UUID u2, int c)
    { 
        String tb1 = (u1 == null) ? "null" : (u1.version() == 1) ? "time-based " : "random ";
        String tb2 = (u2 == null) ? "null" : (u2.version() == 1) ? "time-based " : "random ";
        String comp = (c < 0) ? " < " : ((c == 0) ? " = " : " > ");
        return tb1 + u1 + comp + tb2 + u2;
    } 
 
    public int sign(int i)
    { 
        if (i < 0)
        { 
            return -1; 
        } 
        if (i > 0)
        { 
            return 1; 
        } 
        return 0; 
    } 
 
    public static ByteBuffer bytebuffer(UUID uuid)
    { 
        if (uuid == null)
            return ByteBufferUtil.EMPTY_BYTE_BUFFER; 
 
        long msb = uuid.getMostSignificantBits();
        long lsb = uuid.getLeastSignificantBits();
        byte[] bytes = new byte[16];
 
        for (int i = 0; i < 8; i++)
        { 
            bytes[i] = (byte) (msb >>> 8 * (7 - i));
        } 
        for (int i = 8; i < 16; i++)
        { 
            bytes[i] = (byte) (lsb >>> 8 * (7 - i));
        } 
 
        return ByteBuffer.wrap(bytes);
    } 
 
    public void logJdkUUIDCompareToVariance(UUID u1, UUID u2, int expC)
    { 
        if ((u1 == null) || (u2 == null))
            return; 
        if (u1.version() != u2.version())
            return; 
        if (u1.version() == 1)
            return; 
        if (u1.compareTo(u2) != expC)
            logger.info("*** Note: java.util.UUID.compareTo() would have compared this differently"); 
    } 
 
    public void testCompare(UUID u1, UUID u2, int expC)
    { 
        int c = sign(uuidType.compare(bytebuffer(u1), bytebuffer(u2)));
        expC = sign(expC);
        assertEquals("Expected " + describeCompare(u1, u2, expC) + ", got " + describeCompare(u1, u2, c), expC, c);
 
        if (((u1 != null) && (u1.version() == 1)) && ((u2 != null) && (u2.version() == 1)))
            assertEquals(c, sign(TimeUUIDType.instance.compare(bytebuffer(u1), bytebuffer(u2))));
 
        logJdkUUIDCompareToVariance(u1, u2, c);
    } 
 
    @Test 
    public void testTimeEquality() 
    { 
        UUID a = UUIDGen.getTimeUUID();
        UUID b = new UUID(a.getMostSignificantBits(),
                a.getLeastSignificantBits());
 
        assertEquals(0, uuidType.compare(bytebuffer(a), bytebuffer(b)));
    } 
 
    @Test