Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LineRange and LineRangeList from analysis-model #815

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

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