IdGenerator { public String next(Instant now) { long time = now.toEpochMilli(); int counter = this.counter.getAndIncrement() & LOW_ORDER_THREE_BYTES; byte[] bytes = new byte[10]; bytes[0] = (byte) (time >> 32); bytes[1] = (byte) (time >> 24); bytes[2] = (byte) (time >> 16); bytes[3] = (byte) (time >> 8); bytes[4] = (byte) time; bytes[5] = (byte) (machineIdentifier >> 16); bytes[6] = (byte) (machineIdentifier >> 8); bytes[7] = (byte) machineIdentifier; bytes[8] = (byte) (counter >> 8); bytes[9] = (byte) counter; return Encodings.hex(bytes); }  String next(Instant now);  }
@Test void next() { Instant now = Instant.ofEpochMilli(1); String id1 = generator.next(now); assertThat(id1).hasSize(20).startsWith("0000000001"); String id2 = generator.next(now); assertThat(id2).hasSize(20).startsWith("0000000001"); assertThat(id1.substring(5, 8)).as("machine identifier should be same").isEqualTo(id2.substring(5, 8)); }