Best code examples for Java 8 UUID class (java.util.UUID)

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 UUID class.
You can now enable Codota on your own code to easily search and navigate your Java codebase.

Java 8 UUID examples from Open Source projects
This code example shows how to use the following methods: timestamp, version
        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;
    } 

8
CodeRank
This code example shows how to use the following methods: getLeastSignificantBits, getMostSignificantBits
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)))

8
CodeRank
This code example shows how to use the following methods: getLeastSignificantBits, getMostSignificantBits
        super(UUID.class);
    } 
 
    @Override 
    public void writeObject(ObjectOutput output, UUID uuid) throws IOException {
        output.writeLong(uuid.getMostSignificantBits());
        output.writeLong(uuid.getLeastSignificantBits());
    } 
 
    @Override 
    public UUID readObject(ObjectInput input) throws IOException {
        return new UUID(input.readLong(), input.readLong());
    } 
} 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: toString
		    String tmDevice, tmSerial, tmPhone, androidId;
		    tmDevice = "" + tm.getDeviceId();
		    tmSerial = "" + tm.getSimSerialNumber();
		    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
	 
		    UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
		    deviceId = deviceUuid.toString();
		     
		    System.err.println(deviceId);
		} 
	    return deviceId;
	} 
	 
} 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: timestamp, version
     * @return the number of milliseconds since the unix epoch 
     * @throws IllegalArgumentException if the UUID is not version 1 
     */ 
    public static long getAdjustedTimestamp(UUID uuid)
    { 
        if (uuid.version() != 1)
            throw new IllegalArgumentException("incompatible with uuid version: "+uuid.version());
        return (uuid.timestamp() / 10000) + START_EPOCH;
    } 
 
    private static long makeClockSeqAndNode() 
    { 
        long clock = new Random(System.currentTimeMillis()).nextLong();
 
        long lsb = 0;
        lsb |= 0x8000000000000000L;                 // variant (2 bits)
        lsb |= (clock & 0x0000000000003FFFL) << 48; // clock sequence (14 bits)
        lsb |= makeNode();                          // 6 bytes
        return lsb;
    } 
Full Snippet Info
7
CodeRank
Related Java 8 UUID Questions & Answers:
See Code Examples for Java 8 UUID Methods: