org.conqat.lib.commons.diff.DiffDescription 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.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.conqat.lib.commons.assertion.CCSMAssert;
import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.UnmodifiableList;
import org.conqat.lib.commons.string.StringUtils;
import org.conqat.lib.commons.test.IndexValueClass;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* A class describing a diff.
*/
@IndexValueClass(containedInBackup = true)
public class DiffDescription implements Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 1;
/** The name of this description. */
@JsonProperty("name")
private final String name;
/**
* Line changes for the left lines. The integers are organized in pairs, giving the start
* (inclusive) and end (exclusive) lines of a region.
*/
@JsonProperty("leftChangeLines")
private final List leftChangeLines = new ArrayList<>();
/**
* Line changes for the right lines. The integers are organized in pairs, giving the start
* (inclusive) and end (exclusive) lines of a region.
*/
@JsonProperty("rightChangeLines")
private final List rightChangeLines = new ArrayList<>();
/**
* Change tokens for the left text. These are used to highlight the exact change within changed
* lines. The integers are organized in pairs, giving the start (inclusive) and end (exclusive)
* offsets of a region.
*/
@JsonProperty("leftChangeRegions")
private final List leftChangeRegions = new ArrayList<>();
/**
* Change tokens for the left text. These are used to highlight the exact change within changed
* lines. The integers are organized in pairs, giving the start (inclusive) and end (exclusive)
* offsets of a region.
*/
@JsonProperty("rightChangeRegions")
private final List rightChangeRegions = new ArrayList<>();
@JsonCreator
public DiffDescription() {
this(StringUtils.EMPTY_STRING);
}
/** Constructor. */
public DiffDescription(String name) {
this.name = name;
}
/** Returns the name. */
public String getName() {
return name;
}
/**
* Adds a line region that is matched between the left and right element. This denotes a part where
* these regions differ. First lines are inclusive, last lines are exclusive. All lines are 1-based.
* Adding regions must be performed in ascending order.
*/
public void addLineRegion(int leftFirstLine, int leftEndLine, int rightFirstLine, int rightEndLine) {
addLineRegion(leftFirstLine, leftEndLine, leftChangeLines);
addLineRegion(rightFirstLine, rightEndLine, rightChangeLines);
}
/**
* Inserts a line region into either {@link #leftChangeLines} or {@link #rightChangeLines}.
*/
private static void addLineRegion(int firstLine, int endLine, List changeLines) {
CCSMAssert.isTrue(firstLine >= 0, "May only insert positive lines!");
CCSMAssert.isTrue(firstLine <= endLine, "End must not be before start!");
CCSMAssert.isTrue(changeLines.isEmpty() || CollectionUtils.getLast(changeLines) - 1 <= firstLine,
"Must insert in ascending order!");
changeLines.add(firstLine);
changeLines.add(endLine);
}
/**
* Adds a change within a line region (that is highlighted) for the left side. Start is inclusive,
* end is exclusive.
*/
public void addLeftChange(int startOffset, int endOffset) {
addChange(startOffset, endOffset, leftChangeRegions);
}
/**
* Adds a change within a line region (that is highlighted) for the right side. Start is inclusive,
* end is exclusive.
*/
public void addRightChange(int startOffset, int endOffset) {
addChange(startOffset, endOffset, rightChangeRegions);
}
/**
* Adds a change within a line region. Start is inclusive, end is exclusive.
*/
private static void addChange(int startOffset, int endOffset, List changeTokens) {
if (!changeTokens.isEmpty() && startOffset <= CollectionUtils.getLast(changeTokens)) {
changeTokens.set(changeTokens.size() - 1, endOffset);
} else {
changeTokens.add(startOffset);
changeTokens.add(endOffset);
}
}
/**
* Returns the change regions for the left lines. The integers are organized in pairs, giving the
* start (inclusive) and end (exclusive) index of a region.
*/
public UnmodifiableList getLeftChangeRegions() {
return CollectionUtils.asUnmodifiable(leftChangeRegions);
}
/**
* Returns the change regions for the right lines. The integers are organized in pairs, giving the
* start (inclusive) and end (exclusive) index of a region.
*/
public UnmodifiableList getRightChangeRegions() {
return CollectionUtils.asUnmodifiable(rightChangeRegions);
}
/**
* Adds the given list to the leftChangeRegions
*/
public void addLeftChangeRegions(UnmodifiableList leftChangeRegions) {
this.leftChangeRegions.addAll(leftChangeRegions);
}
/**
* Adds the given list to the rightChangeRegions
*/
public void addRightChangeRegions(UnmodifiableList rightChangeRegions) {
this.rightChangeRegions.addAll(rightChangeRegions);
}
/**
* Returns the line changes for the left lines. The integers are organized in pairs, giving the
* start (inclusive) and end (exclusive) lines of a region.
*/
public UnmodifiableList getLeftChangeLines() {
return CollectionUtils.asUnmodifiable(leftChangeLines);
}
/**
* Returns the line changes for the right lines. The integers are organized in pairs, giving the
* start (inclusive) and end (exclusive) lines of a region.
*/
public UnmodifiableList getRightChangeLines() {
return CollectionUtils.asUnmodifiable(rightChangeLines);
}
}