public static <T extends ApiMessage> Optional<T> recordAtIndexAs(
            Class<T> recordClazz,
            List<ApiMessageAndVersion> recordsAndVersions,
            int recordIndex
    ) {
        if (recordIndex > recordsAndVersions.size() - 1) {
            return Optional.empty();
        } else {
            if (recordIndex == -1) {
                return recordsAndVersions.stream().map(ApiMessageAndVersion::message)
                    .filter(record -> record.getClass().isAssignableFrom(recordClazz))
                    .map(recordClazz::cast)
                    .findFirst();
            } else {
                ApiMessageAndVersion messageAndVersion = recordsAndVersions.get(recordIndex);
                ApiMessage record = messageAndVersion.message();
                if (record.getClass().isAssignableFrom(recordClazz)) {
                    return Optional.of(recordClazz.cast(record));
                } else {
                    return Optional.empty();
                }
            }

        }
    }
Replay a list of records.

@param target                The object to invoke the replay function on.
@param recordsAndVersions    A list of records.
metadata/src/test/java/org/apache/kafka/metadata/RecordTestUtils.java