@Override public Summary newSummary() { return new GroupDataSummary(); }
@Override public DeserializeResult summaryFromMemory(Memory serializedSummary) { return GroupDataSummary.fromMemory(serializedSummary); }
@Override public GroupDataSummary union(GroupDataSummary a, GroupDataSummary b) { return GroupDataSummary.mergeInPlace(a, b); }
private void mergeInPlace(GroupDataSummary other) { if (other == null) { return; } // This check is unnecessary since all merges will have valid (or at least non empty data) from the very fact // that they were created (see update above). GroupData targetData = other.getData(); if (targetData == null) { return; } // In-place, so not copying targetData if (data == null) { data = targetData; } else { data.combine(targetData); } }
@Override public List<BulletRecord> getRecords() { merge(); List<BulletRecord> result = new ArrayList<>(); SketchIterator<GroupDataSummary> iterator = this.result.iterator(); for (int count = 0; iterator.next() && count < maxSize; count++) { GroupData data = iterator.getSummary().getData(); result.add(data.getAsBulletRecord(provider)); } return result; }
@SuppressWarnings("unchecked") @Override public GroupDataSummary copy() { GroupDataSummary copy = new GroupDataSummary(); copy.initialized = initialized; copy.data = CachingGroupData.copy(data); return copy; }
/** * This method merges two {@link GroupDataSummary} into each other. It picks the first non-null parameter to * merge into and returns that after merge. * * @param a The first {@link GroupDataSummary} to merge. * @param b The second {@link GroupDataSummary} to merge. * @return The resulting merged summary or null if both arguments were null. */ public static GroupDataSummary mergeInPlace(GroupDataSummary a, GroupDataSummary b) { if (a != null) { a.mergeInPlace(b); return a; } else if (b != null) { return b; } return null; }
/** * Needed to deserialize an instance of this {@link GroupDataSummary} from a {@link Memory}. * * @param serializedSummary The serialized summary as a {@link Memory} object. * @return A {@link DeserializeResult} representing the deserialized summary. */ public static DeserializeResult<GroupDataSummary> fromMemory(Memory serializedSummary) { byte initialized = serializedSummary.getByte(INITIALIZED_POSITION); int size = serializedSummary.getInt(SIZE_POSITION); byte[] data = new byte[size]; serializedSummary.getByteArray(DATA_POSITION, data, 0, size); GroupData deserializedData = SerializerDeserializer.fromBytes(data); GroupDataSummary deserialized = new GroupDataSummary(); deserialized.initialized = initialized != 0; deserialized.data = deserializedData; // Size read is the size of size and the byte in bytes (DATA_POSITION) plus the size of the data (size) return new DeserializeResult<>(deserialized, size + DATA_POSITION); } }