@Test public void testNewRandomId() { assertThat(IdGenerator.newRandomId(null)).isBetween(IdGenerator.MIN, IdGenerator.MAX); Set<Long> ids = new HashSet<>(); ids.add(1L); assertThat(IdGenerator.newRandomId(ids)).isBetween(IdGenerator.MIN, IdGenerator.MAX); }
public static long newRandomId(@Nullable Set<Long> existingIds) { while (true) { long candidateId = ThreadLocalRandom.current().nextLong(); if (MIN <= candidateId && candidateId <= MAX) { if (existingIds == null) { return candidateId; } if (!existingIds.contains(candidateId)) { return candidateId; } } } }
IdGenerator { public static long newRandomId(@Nullable Set<Long> existingIds) { while (true) { long candidateId = ThreadLocalRandom.current().nextLong(); if (MIN <= candidateId && candidateId <= MAX) { if (existingIds == null) { return candidateId; } if (!existingIds.contains(candidateId)) { return candidateId; } } } } }
IdGenerator { public static long newRandomId(@Nullable Set<Long> existingIds) { while (true) { long candidateId = ThreadLocalRandom.current().nextLong(); if (MIN <= candidateId && candidateId <= MAX) { if (existingIds == null) { return candidateId; } if (!existingIds.contains(candidateId)) { return candidateId; } } } }  }
IdGenerator { public static long newRandomId(@Nullable Set<Long> existingIds) { while (true) { long candidateId = ThreadLocalRandom.current().nextLong(); if (MIN <= candidateId && candidateId <= MAX) { if (existingIds == null) { return candidateId; } if (!existingIds.contains(candidateId)) { return candidateId; } } } }  static long newRandomId(@Nullable Set<Long> existingIds); static long newLinearId(AtomicLong longValue); }
IdGenerator { public static long newRandomId(@Nullable Set<Long> existingIds) { while (true) { long candidateId = ThreadLocalRandom.current().nextLong(); if (MIN <= candidateId && candidateId <= MAX) { if (existingIds == null) { return candidateId; } if (!existingIds.contains(candidateId)) { return candidateId; } } } }  static long newRandomId(@Nullable Set<Long> existingIds); static long newLinearId(AtomicLong longValue); static final long MIN; static final long MAX; }