package com.atlassian.stash.internal.content;

import com.atlassian.bitbucket.content.ConflictMarker;
import com.atlassian.bitbucket.content.DiffSegmentType;

/**
 * @since 1.3
 */
public class DiffLines {

    private int destinationLine;
    private int sourceLine;

    public DiffLines(int sourceLine, int destinationLine) {
        this.sourceLine = sourceLine;
        this.destinationLine = destinationLine;
    }

    public <T> T withLines(DiffSegmentType segmentType, String line, ConflictMarker marker, boolean truncated,
                           DiffLineCallback<T> callback) {
        try {
            return callback.onLine(segmentType, sourceLine, destinationLine, line, marker, truncated);
        } finally {
            incrementFor(segmentType);
        }
    }

    private void incrementFor(DiffSegmentType segmentType) {
        if (segmentType.isInDestination()) {
            ++destinationLine;
        }
        if (segmentType.isInSource()) {
            ++sourceLine;
        }
    }
}
