@Test public void generateUniqueId_withMultipleInvocations_shouldReturnUniqueValues() throws Exception { final int expectedIdCount = 100; Set<Long> ids = new HashSet<Long>(expectedIdCount); for (int i = 0; i < expectedIdCount; i++) { final long id = Utils.generateUniqueId(); ids.add(id); } assertThat(ids).hasSize(expectedIdCount); }
public static long generateUniqueId() { for (;;) { final long result = sNextGeneratedId.get(); long newValue = result + 1; if (newValue > Long.MAX_VALUE - 1) { newValue = 1; } if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } }
Utils { public static long generateUniqueId() { for (;;) { final long result = sNextGeneratedId.get(); long newValue = result + 1; if (newValue > Long.MAX_VALUE - 1) { newValue = 1; } if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } } }
Utils { public static long generateUniqueId() { for (;;) { final long result = sNextGeneratedId.get(); long newValue = result + 1; if (newValue > Long.MAX_VALUE - 1) { newValue = 1; } if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } }  }
Utils { public static long generateUniqueId() { for (;;) { final long result = sNextGeneratedId.get(); long newValue = result + 1; if (newValue > Long.MAX_VALUE - 1) { newValue = 1; } if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } }  static String sha1(String string); static long generateUniqueId(); static boolean bitMaskContainsFlag(final int bitMask, final int flag); }
Utils { public static long generateUniqueId() { for (;;) { final long result = sNextGeneratedId.get(); long newValue = result + 1; if (newValue > Long.MAX_VALUE - 1) { newValue = 1; } if (sNextGeneratedId.compareAndSet(result, newValue)) { return result; } } }  static String sha1(String string); static long generateUniqueId(); static boolean bitMaskContainsFlag(final int bitMask, final int flag);  }