org.conqat.lib.commons.diff.LineBasedDiffer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.lib.commons.diff;
import java.util.ArrayList;
import java.util.List;
import org.conqat.lib.commons.string.StringUtils;
/**
* Class for calculating a line-based diff.
*/
public class LineBasedDiffer extends DifferBase {
/** Whether to ignore whitespace in each line. */
private final boolean ignoreWhitespace;
/** Whether to annotate test coverage in each line. */
private final boolean showCoverage;
/** Constructor. Sets {@link #showCoverage} to false. */
public LineBasedDiffer(boolean ignoreWhitespace) {
this(ignoreWhitespace, false);
}
/** Constructor. */
public LineBasedDiffer(boolean ignoreWhitespace, boolean showCoverage) {
this.ignoreWhitespace = ignoreWhitespace;
this.showCoverage = showCoverage;
}
/** {@inheritDoc} */
@Override
protected String getElementText(String element) {
return element;
}
/** {@inheritDoc} */
@Override
protected String getDiffName() {
String diffName = "line-based";
if (ignoreWhitespace) {
diffName += " (ignore whitespace)";
}
if (showCoverage) {
diffName += " with coverage";
}
return diffName;
}
/** {@inheritDoc} */
@Override
protected List getChunks(String elementText, boolean isLeft) {
List lines = StringUtils.splitLinesAsList(elementText);
List result = new ArrayList<>();
int offset = 0;
int lineNumber = 1;
for (String line : lines) {
result.add(createChunkForLine(offset, lineNumber, line));
offset += line.length() + 1;
lineNumber += 1;
}
return result;
}
/** Creates a chunk for a single line of the element. */
private TextChunk createChunkForLine(int offset, int lineNumber, String line) {
int startOffset = offset;
int endOffset = startOffset + line.length();
String comparisonText = line;
if (ignoreWhitespace) {
comparisonText = line.replaceFirst("^\\s+", StringUtils.EMPTY_STRING);
int removed = endOffset - startOffset - comparisonText.length();
startOffset += removed;
comparisonText = line.replaceFirst("\\s+$", StringUtils.EMPTY_STRING);
removed = endOffset - startOffset - comparisonText.length();
endOffset -= removed;
comparisonText = comparisonText.replaceAll("\\s+", StringUtils.EMPTY_STRING);
}
return new TextChunk(startOffset, endOffset, lineNumber, lineNumber + 1, comparisonText);
}
}