Code example for UUID

Methods: timestampversion

0
        TimeUUIDType comp = TimeUUIDType.instance;
        ByteBuffer first = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
        ByteBuffer second = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
        assert comp.compare(first, second) < 0;
        assert comp.compare(second, first) > 0;
        ByteBuffer sameAsFirst = ByteBuffer.wrap(UUIDGen.decompose(UUIDGen.getUUID(first)));
        assert comp.compare(first, sameAsFirst) == 0;
    } 
 
    @Test 
    public void testUUIDTimestamp() throws UnknownHostException 
    { 
        InetAddress addr = InetAddress.getByName("127.0.0.1");
        long now = System.currentTimeMillis();
        UUID uuid = UUIDGen.getTimeUUID();
        long tstamp = UUIDGen.getAdjustedTimestamp(uuid);
 
        // I'll be damn is the uuid timestamp is more than 10ms after now 
        assert now <= tstamp && now >= tstamp - 10 : "now = " + now + ", timestamp = " + tstamp;
    } 
 
    private void assertNonZero(BigInteger i)
    { 
        assert i.toString(2).indexOf("1") > -1;
    } 
}