-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathPosition.java
More file actions
74 lines (65 loc) · 1.92 KB
/
Position.java
File metadata and controls
74 lines (65 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.semmle.js.ast;
/**
* A source position identifying a single character.
* <p>
* Note that this class remains distinct from {@link com.semmle.util.locations.Position},
* due to the 1-based line number convention and the tendency for users of this class to provide
* dummy offset values. Although the classes are structurally identical, it is not always safe to
* convert one into the other.
*/
public class Position implements Comparable<Position> {
private final int line, column, offset;
public Position(int line, int column, int offset) {
this.line = line;
this.column = column;
this.offset = offset;
}
/** The line number (1-based) of this position. */
public int getLine() {
return line;
}
/** The column number (0-based) of this position. */
public int getColumn() {
return column;
}
/**
* The offset (0-based) of this position from the start of the file, that is, the number of
* characters that precede it.
* <p>
* Note that in some cases, a dummy value is filled in for the offset.
*/
public int getOffset() {
return offset;
}
@Override
public int compareTo(Position that) {
if (this.line < that.line) return -1;
if (this.line == that.line)
if (this.column < that.column) return -1;
else if (this.column == that.column) return 0;
else return 1;
return 1;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + column;
result = prime * result + line;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
Position other = (Position) obj;
if (column != other.column) return false;
if (line != other.line) return false;
return true;
}
@Override
public String toString() {
return line + ":" + column;
}
}