public int positionDiff(LogOffsetMetadata that) {
        if (messageOffsetOnly() || that.messageOffsetOnly())
            throw new KafkaException(this + " cannot compare its segment position with " + that + " since it only has message offset info");
        if (!onSameSegment(that))
            throw new KafkaException(this + " cannot compare its segment position with " + that + " since they are not on the same segment");

        return this.relativePositionInSegment - that.relativePositionInSegment;
    }
@Test
    void testPositionDiff() {
        LogOffsetMetadata metadata1 = new LogOffsetMetadata(1L);
        LogOffsetMetadata metadata2 = new LogOffsetMetadata(5L, 0L, 5);
        KafkaException exception = assertThrows(KafkaException.class, () -> metadata1.positionDiff(metadata2));
        assertTrue(exception.getMessage().endsWith("since it only has message offset info"));

        exception = assertThrows(KafkaException.class, () -> metadata2.positionDiff(metadata1));
        assertTrue(exception.getMessage().endsWith("since it only has message offset info"));

        LogOffsetMetadata metadata3 = new LogOffsetMetadata(15L, 10L, 5);
        exception = assertThrows(KafkaException.class, () -> metadata3.positionDiff(metadata2));
        assertTrue(exception.getMessage().endsWith("since they are not on the same segment"));

        LogOffsetMetadata metadata4 = new LogOffsetMetadata(40L, 10L, 100);
        assertEquals(95, metadata4.positionDiff(metadata3));
    }