public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }
public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }
public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }
public long extractTimestamp() { return Encoder.bytesToLong(array, array.length - 4 - 8); // - sequence - timestamp }
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testExtracts() throws Exception { byte[] timestamp = Encoder.longToBytes(System.currentTimeMillis()); byte[] sequence = Encoder.intToBytes(1); byte[] serverId = "my-server-id".getBytes(); int uidLength = serverId.length + timestamp.length + sequence.length; byte[] uidArray = new byte[uidLength]; System.arraycopy(serverId, 0, uidArray, 0, serverId.length); System.arraycopy(timestamp, 0, uidArray, serverId.length, timestamp.length); System.arraycopy(sequence, 0, uidArray, serverId.length + timestamp.length, sequence.length); Uid uid = new Uid(uidArray); assertTrue(Arrays.equals(serverId, uid.extractServerId())); assertEquals(Encoder.bytesToLong(timestamp, 0), uid.extractTimestamp()); assertEquals(Encoder.bytesToInt(sequence, 0), uid.extractSequence()); }
public void testLongEncodingDecoding() { byte[] longAsBytes; long result; longAsBytes = Encoder.longToBytes(Long.MAX_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MAX_VALUE, result); longAsBytes = Encoder.longToBytes(Long.MIN_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MIN_VALUE, result); longAsBytes = Encoder.longToBytes(-1L); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(-1L, result); long timestamp = System.currentTimeMillis(); longAsBytes = Encoder.longToBytes(timestamp); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(timestamp, result); byte[] intAsBytes = Encoder.intToBytes(-1); int resultAsInt = Encoder.bytesToInt(intAsBytes, 0); assertEquals(-1, resultAsInt); try { Encoder.bytesToLong(new byte[4], 0); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { assertEquals("a long can only be decoded from 8 bytes of an array (got a 4 byte(s) array, must start at position 0)", ex.getMessage()); } }
public void testLongEncodingDecoding() { byte[] longAsBytes; long result; longAsBytes = Encoder.longToBytes(Long.MAX_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MAX_VALUE, result); longAsBytes = Encoder.longToBytes(Long.MIN_VALUE); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(Long.MIN_VALUE, result); longAsBytes = Encoder.longToBytes(-1L); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(-1L, result); long timestamp = System.currentTimeMillis(); longAsBytes = Encoder.longToBytes(timestamp); result = Encoder.bytesToLong(longAsBytes, 0); assertEquals(timestamp, result); byte[] intAsBytes = Encoder.intToBytes(-1); int resultAsInt = Encoder.bytesToInt(intAsBytes, 0); assertEquals(-1, resultAsInt); try { Encoder.bytesToLong(new byte[4], 0); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException ex) { assertEquals("a long can only be decoded from 8 bytes of an array (got a 4 byte(s) array, must start at position 0)", ex.getMessage()); } }