1 package org.modeshape.common.text;
2
3 import net.jcip.annotations.Immutable;
4
5
6
7
8 @Immutable
9 public final class Position {
10
11
12
13 public final static Position EMPTY_CONTENT_POSITION = new Position(-1, 1, 0);
14
15 private final int line;
16 private final int column;
17 private final int indexInContent;
18
19 public Position( int indexInContent,
20 int line,
21 int column ) {
22 this.indexInContent = indexInContent < 0 ? -1:indexInContent;
23 this.line = line;
24 this.column = column;
25
26 assert this.indexInContent >= -1;
27 assert this.line > 0;
28 assert this.column >= 0;
29
30 assert this.indexInContent < 0 ? this.line == 1 && this.column == 0: true;
31 }
32
33
34
35
36
37
38 public int getIndexInContent() {
39 return indexInContent;
40 }
41
42
43
44
45
46
47 public int getColumn() {
48 return column;
49 }
50
51
52
53
54
55
56 public int getLine() {
57 return line;
58 }
59
60
61
62
63
64
65 @Override
66 public int hashCode() {
67 return indexInContent;
68 }
69
70
71
72
73
74
75 @Override
76 public String toString() {
77 return "" + indexInContent + ':' + line + ':' + column;
78 }
79
80
81
82
83
84
85
86 public Position add(Position position) {
87 if( this.getIndexInContent() < 0 ) {
88 return position.getIndexInContent() < 0 ? EMPTY_CONTENT_POSITION:position;
89 }
90
91 if( position.getIndexInContent() < 0 ) {
92 return this;
93 }
94
95 int index = this.getIndexInContent() + position.getIndexInContent();
96 int line = position.getLine() + this.getLine() - 1;
97 int column = this.getLine() == 1 ? this.getColumn() + position.getColumn() : this.getColumn();
98
99 return new Position(index, line, column);
100 }
101 }