IDBuilder { public static byte[] build(Blueprint blueprint) { ByteBuffer bb = ByteBuffer.allocate(8); switch (blueprint.getMode()) { case SPREAD: long reverseTimestamp = Long.reverse(blueprint.getTimestamp()); bb.putLong(reverseTimestamp); break; case TIME_SEQUENTIAL: long timestamp = blueprint.getTimestamp(); bb.putLong(timestamp << 22); break; } byte[] tsBytes = bb.array(); int or = tsBytes[5] | (byte) blueprint.getSequence(); tsBytes[5] = (byte) or; int flagGeneratorCluster = blueprint.getGeneratorId() << 4; flagGeneratorCluster += blueprint.getClusterId(); flagGeneratorCluster += blueprint.getMode().getModeMask() << 12; tsBytes[7] = (byte) flagGeneratorCluster; flagGeneratorCluster >>>= 8; tsBytes[6] = (byte) flagGeneratorCluster; return tsBytes; }  static byte[] build(Blueprint blueprint); static Blueprint parse(byte[] id); static int parseSequenceId(byte[] id); static int parseGeneratorId(byte[] id); static int parseClusterId(byte[] id); static long parseTimestamp(byte[] id); static Mode parseMode(byte[] id);  }
@Test public void buildSequenceCounterOnly() { final byte[] result = IDBuilder.build(new Blueprint(0, 0x22, 0, 0)); final byte[] sixthByte = new byte[]{result[5]}; final String expected = "22"; assertThat(Hex.encodeHexString(sixthByte), is(expected)); }
@Test public void buildGeneratorIdOnly() { final byte[] result = IDBuilder.build(new Blueprint(0, 0, 0x27, 0)); final byte[] lastTwoBytes = new byte[]{result[6], result[7]}; final String expected = "0270"; assertThat(Hex.encodeHexString(lastTwoBytes), is(expected)); }
@Test public void buildClusterIdOnly() { final byte[] result = IDBuilder.build(new Blueprint(0, 0, 0, 5)); final byte[] lastTwoBytes = new byte[]{result[6], result[7]}; final String expected = "0005"; assertThat(Hex.encodeHexString(lastTwoBytes), is(expected)); }
@Test public void buildModeOnlySpread() { final byte[] result = IDBuilder.build(new Blueprint(0, 0, 0, 0)); final byte[] lastTwoBytes = new byte[]{result[6], result[7]}; final String expected = "0000"; assertThat(Hex.encodeHexString(lastTwoBytes), is(expected)); }
@Test public void buildModeOnlySpreadExplicit() { final byte[] result = IDBuilder.build(new Blueprint(0, 0, 0, 0, Mode.SPREAD)); final byte[] lastTwoBytes = new byte[]{result[6], result[7]}; final String expected = "0000"; assertThat(Hex.encodeHexString(lastTwoBytes), is(expected)); }
@Test public void buildModeOnlyTimeExplicit() { final byte[] result = IDBuilder.build(new Blueprint(0, 0, 0, 0, Mode.TIME_SEQUENTIAL)); final byte[] lastTwoBytes = new byte[]{result[6], result[7]}; final String expected = "1000"; assertThat(Hex.encodeHexString(lastTwoBytes), is(expected)); }
@Test public void buildZero() { final byte[] result = IDBuilder.build(new Blueprint(0, 0, 0, 0)); final byte[] zero = new byte[8]; assertThat(result, is(zero)); }
@Test public void buildMostlyOnes() { final byte[] result = IDBuilder.build(new Blueprint( Blueprint.MAX_TIMESTAMP, Blueprint.MAX_SEQUENCE_COUNTER, Blueprint.MAX_GENERATOR_ID, Blueprint.MAX_CLUSTER_ID, Mode.SPREAD )); final String expected = "ffffffffffff0fff"; assertThat(Hex.encodeHexString(result), is(expected)); }
@Test public void buildTimestampOnly() { final long TEST_TS_A = 143062936275L; final String TEST_A_REVERSED = "cb54ecf284000000"; final byte[] result_a = IDBuilder.build(new Blueprint(TEST_TS_A, 0, 0, 0)); assertThat(Hex.encodeHexString(result_a).toLowerCase(), is(TEST_A_REVERSED)); final long TEST_TS_B = 0x3FFFFFFFDL; final String TEST_B_REVERSED = "bfffffffc0000000"; final byte[] result_b = IDBuilder.build(new Blueprint(TEST_TS_B, 0, 0, 0)); assertThat(Hex.encodeHexString(result_b).toLowerCase(), is(TEST_B_REVERSED)); }