ConcurrentUUIDFactory implements UUIDFactory { @Override public UUID generateRandomUuid() { final Random rnd = ThreadLocalRandom.current(); long mostSig = rnd.nextLong(); long leastSig = rnd.nextLong(); mostSig &= 0xffffffffffff0fffL; mostSig |= 0x0000000000004000L; leastSig &= 0x3fffffffffffffffL; leastSig |= 0x8000000000000000L; return new UUID(mostSig, leastSig); }  @Override UUID generateRandomUuid();  }
@Test public void testGenerateRandomUuidVersionAndVariant() throws Exception { final UUID uuid = new ConcurrentUUIDFactory().generateRandomUuid(); Assert.assertEquals(4, uuid.version()); Assert.assertEquals(2, uuid.variant()); }
@Test public void testGenerateRandomUuidNonConstant() throws Exception { final int numValues = 1000; final Set<UUID> values = new HashSet<>(); final ConcurrentUUIDFactory factory = new ConcurrentUUIDFactory(); for (int i = 0; i < numValues; ++i) { final UUID newUuid = factory.generateRandomUuid(); Assert.assertTrue("Already generated UUID with value: " + newUuid, values.add(newUuid)); } Assert.assertEquals(numValues, values.size()); }