|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.github.javaparser; |
|
|
|
import com.github.javaparser.ast.Node; |
|
|
|
import java.util.Objects; |
|
|
|
import static com.github.javaparser.utils.Utils.assertNotNull; |
|
|
|
|
|
|
|
*/ |
|
public class Position implements Comparable<Position> { |
|
public final int line; |
|
public final int column; |
|
|
|
|
|
|
|
*/ |
|
public static final int FIRST_LINE = 1; |
|
|
|
|
|
*/ |
|
public static final int FIRST_COLUMN = 1; |
|
|
|
|
|
*/ |
|
public static final Position HOME = new Position(FIRST_LINE, FIRST_COLUMN); |
|
|
|
|
|
|
|
|
|
*/ |
|
public static final int ABSOLUTE_BEGIN_LINE = -1; |
|
|
|
public static final int ABSOLUTE_END_LINE = -2; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Position(int line, int column) { |
|
if (line < Position.ABSOLUTE_END_LINE) { |
|
|
|
throw new IllegalArgumentException("Can't position at line " + line); |
|
} |
|
if (column < -1) { |
|
// TODO: This allows/permits column 0, which seemingly contradicts first column being 1 |
|
// ... (see also nextLine() which indicates 1 being the first column of the next line) |
|
// ... (see also valid() which requires a column > 0) |
|
|
|
throw new IllegalArgumentException("Can't position at column " + column); |
|
} |
|
this.line = line; |
|
this.column = column; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Deprecated |
|
public static Position pos(int line, int column) { |
|
return new Position(line, column); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Position withColumn(int column) { |
|
return new Position(this.line, column); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Position withLine(int line) { |
|
return new Position(line, this.column); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Position right(int characters) { |
|
return new Position(line, this.column + characters); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public Position nextLine() { |
|
return new Position(line + 1, FIRST_COLUMN); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean valid() { |
|
|
|
return line >= FIRST_LINE && column >= FIRST_COLUMN; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean invalid() { |
|
return !valid(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Position orIfInvalid(Position alternativePosition) { |
|
assertNotNull(alternativePosition); |
|
// TODO: Why the || ? |
|
|
|
if (valid() || alternativePosition.invalid()) { |
|
return this; |
|
} |
|
return alternativePosition; |
|
} |
|
|
|
public boolean isAfter(Position otherPosition) { |
|
assertNotNull(otherPosition); |
|
if (otherPosition.line == Position.ABSOLUTE_BEGIN_LINE) { |
|
|
|
return true; |
|
} |
|
if (line > otherPosition.line) { |
|
return true; |
|
} else if (line == otherPosition.line) { |
|
return column > otherPosition.column; |
|
} |
|
return false; |
|
|
|
} |
|
|
|
public boolean isAfterOrEqual(Position otherPosition) { |
|
assertNotNull(otherPosition); |
|
return isAfter(otherPosition) || equals(otherPosition); |
|
} |
|
|
|
public boolean isBefore(Position otherPosition) { |
|
assertNotNull(otherPosition); |
|
if (otherPosition.line == Position.ABSOLUTE_END_LINE) { |
|
|
|
return true; |
|
} |
|
if (line < otherPosition.line) { |
|
return true; |
|
} else if (line == otherPosition.line) { |
|
return column < otherPosition.column; |
|
} |
|
return false; |
|
} |
|
|
|
public boolean isBeforeOrEqual(Position otherPosition) { |
|
assertNotNull(otherPosition); |
|
return isBefore(otherPosition) || equals(otherPosition); |
|
} |
|
|
|
@Override |
|
public int compareTo(Position otherPosition) { |
|
assertNotNull(otherPosition); |
|
if (isBefore(otherPosition)) { |
|
return -1; |
|
} |
|
if (isAfter(otherPosition)) { |
|
return 1; |
|
} |
|
return 0; |
|
} |
|
|
|
@Override |
|
public boolean equals(Object o) { |
|
if (this == o) return true; |
|
if (o == null || getClass() != o.getClass()) return false; |
|
|
|
Position otherPosition = (Position) o; |
|
|
|
return Objects.equals(line, otherPosition.line) |
|
&& Objects.equals(column, otherPosition.column); |
|
} |
|
|
|
@Override |
|
public int hashCode() { |
|
return Objects.hash(line, column); |
|
} |
|
|
|
@Override |
|
public String toString() { |
|
return "(line " + line + ",col " + column + ")"; |
|
} |
|
} |