Skip to content

Commit

Permalink
Merge pull request #815 from uhafner/line-range
Browse files Browse the repository at this point in the history
Add `LineRange` and `LineRangeList` from analysis-model
  • Loading branch information
uhafner authored Sep 28, 2023
2 parents 38b3811 + 1ae357e commit 96a8303
Show file tree
Hide file tree
Showing 3 changed files with 659 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/main/java/edu/hm/hafner/util/LineRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package edu.hm.hafner.util;

import java.io.Serializable;

import edu.umd.cs.findbugs.annotations.CheckForNull;

/**
* A line range in a source file is defined by its first and last line.
*
* @author Ullrich Hafner
*/
public class LineRange implements Serializable {
private static final long serialVersionUID = -4124143085672930110L;

private final int start;
private final int end;

/**
* Creates a new instance of {@link LineRange}.
*
* @param line
* the single line of this range
*/
public LineRange(final int line) {
this(line, line);
}

/**
* Creates a new instance of {@link LineRange}.
*
* @param start
* start of the range
* @param end
* end of the range
*/
public LineRange(final int start, final int end) {
if (start <= 0) {
this.start = 0;
this.end = 0;
}
else if (start < end) {
this.start = start;
this.end = end;
}
else {
this.start = end;
this.end = start;
}
}

/**
* Returns the first line of this range.
*
* @return the first line of this range
*/
public int getStart() {
return start;
}

/**
* Returns the last line of this range.
*
* @return the last line of this range
*/
public int getEnd() {
return end;
}

@Override
public boolean equals(@CheckForNull final Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}

LineRange lineRange = (LineRange) obj;

Check warning on line 78 in src/main/java/edu/hm/hafner/util/LineRange.java

View workflow job for this annotation

GitHub Actions / Autograding results

Varifier

Consider using `var` here to avoid boilerplate.

if (start != lineRange.start) {
return false;
}
return end == lineRange.end;
}

@Override
public int hashCode() {
int result = start;
result = 31 * result + end;
return result;
}

@Override
public String toString() {
return String.format("[%d-%d]", start, end);
}
}

Loading

0 comments on commit 96a8303

Please sign in to comment.