public RemoteLogSegmentMetadata createWithUpdates(RemoteLogSegmentMetadataUpdate rlsmUpdate) {
        if (!remoteLogSegmentId.equals(rlsmUpdate.remoteLogSegmentId())) {
            throw new IllegalArgumentException("Given rlsmUpdate does not have this instance's remoteLogSegmentId.");
        }

        return new RemoteLogSegmentMetadata(remoteLogSegmentId, startOffset,
                endOffset, maxTimestampMs, rlsmUpdate.brokerId(), rlsmUpdate.eventTimestampMs(),
                segmentSizeInBytes, rlsmUpdate.customMetadata(), rlsmUpdate.state(), segmentLeaderEpochs);
    }
@Test
    void createWithUpdates() {
        int brokerId = 0;
        int eventTimestamp = 0;
        int brokerIdFinished = 1;
        int timestampFinished = 1;
        long startOffset = 0L;
        long endOffset = 100L;
        int segmentSize = 123;
        long maxTimestamp = -1L;

        Map<Integer, Long> segmentLeaderEpochs = new HashMap<>();
        segmentLeaderEpochs.put(0, 0L);
        RemoteLogSegmentId segmentId = new RemoteLogSegmentId(TP0, Uuid.randomUuid());
        RemoteLogSegmentMetadata segmentMetadata = new RemoteLogSegmentMetadata(segmentId, startOffset, endOffset,
                maxTimestamp, brokerId, eventTimestamp, segmentSize,
                segmentLeaderEpochs);

        CustomMetadata customMetadata = new CustomMetadata(new byte[]{0, 1, 2, 3});
        RemoteLogSegmentMetadataUpdate segmentMetadataUpdate = new RemoteLogSegmentMetadataUpdate(
                segmentId, timestampFinished, Optional.of(customMetadata), RemoteLogSegmentState.COPY_SEGMENT_FINISHED,
                brokerIdFinished);
        RemoteLogSegmentMetadata updatedMetadata = segmentMetadata.createWithUpdates(segmentMetadataUpdate);

        RemoteLogSegmentMetadata expectedUpdatedMetadata = new RemoteLogSegmentMetadata(
                segmentId, startOffset, endOffset,
                maxTimestamp, brokerIdFinished, timestampFinished, segmentSize, Optional.of(customMetadata),
                RemoteLogSegmentState.COPY_SEGMENT_FINISHED,
                segmentLeaderEpochs
        );
        assertEquals(expectedUpdatedMetadata, updatedMetadata);

        // Check that the original metadata have not changed.
        assertEquals(segmentId, segmentMetadata.remoteLogSegmentId());
        assertEquals(startOffset, segmentMetadata.startOffset());
        assertEquals(endOffset, segmentMetadata.endOffset());
        assertEquals(maxTimestamp, segmentMetadata.maxTimestampMs());
        assertEquals(brokerId, segmentMetadata.brokerId());
        assertEquals(eventTimestamp, segmentMetadata.eventTimestampMs());
        assertEquals(segmentSize, segmentMetadata.segmentSizeInBytes());
        assertEquals(segmentLeaderEpochs, segmentMetadata.segmentLeaderEpochs());
    }