Java Code Examples for java.util.UUID#variant()

The following examples show how to use java.util.UUID#variant() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomVersionOneStrategy.java    From lams with GNU General Public License v2.0 7 votes vote down vote up
@AllowSysOut
public static void main(String[] args) {
	CustomVersionOneStrategy strategy = new CustomVersionOneStrategy();

	for ( int i = 0; i < 1000; i++ ) {
		System.out.println( "Generation # " + i + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
		byte[] loBits = new byte[8];

		long sysTime = System.currentTimeMillis();
		short hiTime = (short) ( System.currentTimeMillis() >>> 32 );
		int loTime = (int) sysTime;
		System.arraycopy( BytesHelper.fromShort( hiTime ), 0, loBits, 0, 2 );
		System.arraycopy( BytesHelper.fromInt( loTime ), 0, loBits, 2, 4 );
		System.arraycopy( Helper.getCountBytes(), 0, loBits, 6, 2 );

		System.out.println( "    before bit setting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
		System.out.println( "       loBits[0] : " + BytesHelper.toBinaryString( loBits[0] ) );
		System.out.println( "             lsb : " + BytesHelper.toBinaryString( BytesHelper.asLong( loBits ) ) );
		System.out.println( "    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );

		loBits[0] &= 0x3f;
		loBits[0] |= ((byte)2 << (byte)6);

		System.out.println( "    after bit setting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
		System.out.println( "       loBits[0] : " + BytesHelper.toBinaryString( loBits[0] ) );
		long leastSignificantBits = BytesHelper.asLong( loBits );
		System.out.println( "             lsb : " + BytesHelper.toBinaryString( leastSignificantBits ) );
		System.out.println( "    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );


		UUID uuid = new UUID( strategy.mostSignificantBits, leastSignificantBits );
		System.out.println( "  uuid : " + uuid.toString() );
		System.out.println( "  variant : " + uuid.variant() );
		System.out.println( "  version : " + uuid.version() );
		if ( uuid.variant() != 2 ) {
			throw new RuntimeException( "bad variant" );
		}
		System.out.println( "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
	}
}
 
Example 2
Source File: IrisUUID.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static boolean isTime(UUID uuid) {
   return uuid.variant() == 2 && uuid.version() == 1;
}
 
Example 3
Source File: UuidUtil.java    From uuid-creator with MIT License 5 votes vote down vote up
private static boolean isVariant(UUID uuid, UuidVariant variant) {
	if (uuid == null) {
		throw new InvalidUuidException("Null UUID has no variant");
	}
	return (uuid.variant() == variant.getValue());
}
 
Example 4
Source File: UuidTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTimeBaseUuid() {
    final UUID uuid = UuidUtil.getTimeBasedUuid();
    //final UUID uuid2 = UuidUtil.getTimeBasedUUID(); // unused
    final long current = (System.currentTimeMillis() * 10000) + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH;
    final long time = uuid.timestamp();
    assertTrue("Incorrect time", current + 10000 - time > 0);
    final UUID[] uuids = new UUID[COUNT];
    final long start = System.nanoTime();
    for (int i=0; i < COUNT; ++i) {
        uuids[i] = UuidUtil.getTimeBasedUuid();
    }
    final long elapsed = System.nanoTime() - start;
    System.out.println("Elapsed for " + COUNT + " UUIDS = " + elapsed + " Average = " + elapsed / COUNT + " ns");
    int errors = 0;
    for (int i=0; i < COUNT; ++i) {
        for (int j=i+1; j < COUNT; ++j) {
            if (uuids[i].equals(uuids[j])) {
                ++errors;
                System.out.println("UUID " + i + " equals UUID " + j);
            }
        }
    }
    assertEquals(errors + " duplicate UUIDS", 0, errors);
    final int variant = uuid.variant();
    assertEquals("Incorrect variant. Expected 2 got " + variant, 2, variant);
    final int version = uuid.version();
    assertEquals("Incorrect version. Expected 1 got " + version, 1, version);
    final long node = uuid.node();
    assertTrue("Invalid node", node != 0);
}