/** * Returns the Gradoop ID represented by a byte array * * @param bytes byte representation * @return Gradoop ID */ public static GradoopId fromByteArray(byte[] bytes) { return new GradoopId(bytes); }
@Override public GradoopId copy() { return new GradoopId(this.bytes); }
/** * Returns a new GradoopId * * @return new GradoopId */ public static GradoopId get() { return new GradoopId(dateToTimestampSeconds(new Date()), MACHINE_IDENTIFIER, PROCESS_IDENTIFIER, NEXT_COUNTER.getAndIncrement(), false); }
/** * Returns the Gradoop ID represented by a specified hexadecimal string. * <p> * Note: Implementation taken from org.bson.types.ObjectId * * @param string hexadecimal GradoopId representation * @return GradoopId */ public static GradoopId fromString(String string) { if (!GradoopId.isValid(string)) { throw new IllegalArgumentException( "invalid hexadecimal representation of a GradoopId: [" + string + "]"); } byte[] b = new byte[12]; for (int i = 0; i < b.length; i++) { b[i] = (byte) Integer.parseInt(string.substring(i * 2, i * 2 + 2), 16); } return new GradoopId(b); }
@Test public void testFromBytes() { int randomTime = ThreadLocalRandom.current().nextInt(); int randomMachineId = ThreadLocalRandom.current().nextInt(0, 16777215); short randomProcessId = (short) ThreadLocalRandom.current().nextInt(0, Short.MAX_VALUE); int randomCounter = ThreadLocalRandom.current().nextInt(0, 16777215); GradoopId expectedId = new GradoopId(randomTime, randomMachineId, randomProcessId, randomCounter); byte[] bytes = new byte[GradoopId.ID_SIZE]; ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.putInt(randomTime); byte b1; byte b2; byte b3; b3 = (byte) (randomMachineId & 0xFF); b2 = (byte) ((randomMachineId >> 8) & 0xFF); b1 = (byte) ((randomMachineId >> 16) & 0xFF); buffer.put(b1); buffer.put(b2); buffer.put(b3); buffer.putShort(randomProcessId); b3 = (byte) (randomCounter & 0xFF); b2 = (byte) ((randomCounter >> 8) & 0xFF); b1 = (byte) ((randomCounter >> 16) & 0xFF); buffer.put(b1); buffer.put(b2); buffer.put(b3); GradoopId newId = GradoopId.fromByteArray(bytes); assertEquals(expectedId, newId); }
@Test public void testGetIdsAsList() { GradoopId a = new GradoopId(); GradoopId b = new GradoopId(); GradoopId c = new GradoopId(); GradoopId d = new GradoopId(); GradoopId[] da = new GradoopId[] { d, a }; Embedding embedding = new Embedding(); embedding.add(a); embedding.add(b); embedding.add(c); embedding.add(da); assertEquals(Lists.newArrayList(a, b, c), embedding.getIdsAsList(Lists.newArrayList(0, 1, 2))); assertEquals(Lists.newArrayList(a, c), embedding.getIdsAsList(Lists.newArrayList(0, 2))); assertEquals(Lists.newArrayList(b, d, a), embedding.getIdsAsList(Lists.newArrayList(1, 3))); }
@Test public void testGetIdAsListForIdEntries() { GradoopId a = new GradoopId(); GradoopId b = new GradoopId(); Embedding embedding = new Embedding(); embedding.add(a); embedding.add(b); assertEquals(Lists.newArrayList(a), embedding.getIdAsList(0)); assertEquals(Lists.newArrayList(b), embedding.getIdAsList(1)); }
/** * Test if {@link GradoopId} implements {@link CopyableValue#copyTo(Object)} correctly. */ @Test public void testCopyToOtherGradoopId() { GradoopId inputId = GradoopId.get(); GradoopId emptyId = new GradoopId(); inputId.copyTo(emptyId); assertEquals(inputId, emptyId); }
@Test public void testGetIdAsListForIdListEntries() { GradoopId a = new GradoopId(); GradoopId b = new GradoopId(); GradoopId[] ab = new GradoopId[] { a, b }; GradoopId[] ba = new GradoopId[] { b, a }; Embedding embedding = new Embedding(); embedding.add(a); embedding.add(ab); embedding.add(ba); assertEquals(Lists.newArrayList(a, b), embedding.getIdAsList(1)); assertEquals(Lists.newArrayList(b, a), embedding.getIdAsList(2)); }
DataInputViewStreamWrapper inputViewFromCopiedData = new DataInputViewStreamWrapper(inputStreamFromCopiedData); GradoopId copy = new GradoopId(); copy.read(inputViewFromCopiedData); inputStreamFromCopiedData.close();