Example usage for java.util UUID clockSequence

List of usage examples for java.util UUID clockSequence

Introduction

In this page you can find the example usage for java.util UUID clockSequence.

Prototype

public int clockSequence() 

Source Link

Document

The clock sequence value associated with this UUID.

Usage

From source file:Main.java

public static void main(String[] args) {
    // creating UUID      
    UUID x = UUID.randomUUID();

    // getting clock sequence value
    System.out.println("Clock sequence value: " + x.clockSequence());
}

From source file:org.olegz.uuid.TimeBasedUUIDGeneratorTests.java

/**
 * Tests that UUID which was generated based on the same timestamp will still be unique
 * based on the clockSequence. Theoretically there is a possibility of non-unique ID,
 * but only if more then 16383 ids were generated at the same time by the same machine.
 *//*from  w w  w .j a  va 2 s .co m*/
@Test
public void testUniqueness() {
    long timestamp = System.currentTimeMillis();
    UUID id = TimeBasedUUIDGenerator.generateIdFromTimestamp(timestamp);
    UUID newId = null;
    for (int i = 0; i < 16383; i++) { // max, due to the amount of bits allocated for clockSequence
        newId = TimeBasedUUIDGenerator.generateIdFromTimestamp(timestamp);
        // tests, that newly created UUID's clockSequence is always greater then the previous one.
        Assert.assertTrue(newId.clockSequence() > id.clockSequence());
        Assert.assertEquals(newId.timestamp(), id.timestamp());
        id = newId;
    }
}