These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 UUID class.
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; }
{ 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)))
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()); } }
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; } }
* @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; }
return new VersionedValue(String.valueOf(load)); } public VersionedValue schema(UUID newVersion) { return new VersionedValue(newVersion.toString()); } public VersionedValue leaving(Collection<Token> tokens) { return new VersionedValue(versionString(VersionedValue.STATUS_LEAVING, makeTokenString(tokens))); } public VersionedValue left(Collection<Token> tokens, long expireTime) { return new VersionedValue(versionString(VersionedValue.STATUS_LEFT, makeTokenString(tokens), Long.toString(expireTime))); }
{ if (o1.remaining() == 0) { return o2.remaining() == 0 ? 0 : -1; } if (o2.remaining() == 0) { return 1; } int res = compareTimestampBytes(o1, o2); if (res != 0) return res; return o1.compareTo(o2); } private static int compareTimestampBytes(ByteBuffer o1, ByteBuffer o2) { int o1Pos = o1.position(); int o2Pos = o2.position();
/** * @see UUID#UUID(long, long) */ public void test_ConstructorJJ() { UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); assertEquals(2, uuid.variant()); assertEquals(1, uuid.version()); assertEquals(0x1d07decf81d4faeL, uuid.timestamp()); assertEquals(130742845922168750L, uuid.timestamp()); assertEquals(0x2765, uuid.clockSequence()); assertEquals(0xA0C91E6BF6L, uuid.node()); } /** * @see UUID#getLeastSignificantBits() */ public void test_getLeastSignificantBits() { UUID uuid = new UUID(0, 0); assertEquals(0, uuid.getLeastSignificantBits());
/** * @see UUID#UUID(long, long) */ public void test_ConstructorJJ() { UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); assertEquals(2, uuid.variant()); assertEquals(1, uuid.version()); assertEquals(0x1d07decf81d4faeL, uuid.timestamp()); assertEquals(130742845922168750L, uuid.timestamp()); assertEquals(0x2765, uuid.clockSequence()); assertEquals(0xA0C91E6BF6L, uuid.node()); } /** * @see UUID#getLeastSignificantBits() */ public void test_getLeastSignificantBits() { UUID uuid = new UUID(0, 0); assertEquals(0, uuid.getLeastSignificantBits());