1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 The JavaParser Team. |
4 | * |
5 | * This file is part of JavaParser. |
6 | * |
7 | * JavaParser can be used either under the terms of |
8 | * a) the GNU Lesser General Public License as published by |
9 | * the Free Software Foundation, either version 3 of the License, or |
10 | * (at your option) any later version. |
11 | * b) the terms of the Apache License |
12 | * |
13 | * You should have received a copy of both licenses in LICENCE.LGPL and |
14 | * LICENCE.APACHE. Please refer to those files for details. |
15 | * |
16 | * JavaParser is distributed in the hope that it will be useful, |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | * GNU Lesser General Public License for more details. |
20 | */ |
21 | |
22 | package com.github.javaparser; |
23 | |
24 | import static com.github.javaparser.Position.pos; |
25 | |
26 | /** |
27 | * A range of characters in a source file, from "begin" to "end", including the characters at "begin" and "end". |
28 | */ |
29 | public class Range { |
30 | public final Position begin; |
31 | public final Position end; |
32 | |
33 | /** |
34 | * A range of characters in a source file, from "begin" to "end". |
35 | * This range is inclusive of the characters at the "begin" and "end" positions. |
36 | * <p> |
37 | * Note that if the given parameters are reversed (i.e. the end is earlier than begin, then the values are swapped. |
38 | * |
39 | * @param begin The starting position of the range. |
40 | * @param end The end position of the range. |
41 | */ |
42 | public Range(Position begin, Position end) { |
43 | if (begin == null) { |
44 | throw new IllegalArgumentException("begin can't be null"); |
45 | } |
46 | if (end == null) { |
47 | throw new IllegalArgumentException("end can't be null"); |
48 | } |
49 | |
50 | // Force `begin` to be the position that is earliest within the document: |
51 | if (begin.isBefore(end)) { |
52 | this.begin = begin; |
53 | this.end = end; |
54 | } else { |
55 | this.begin = end; |
56 | this.end = begin; |
57 | } |
58 | } |
59 | |
60 | /** |
61 | * Create a new `Range` object using the given begin and end position. |
62 | * |
63 | * @param begin The starting position of the range. |
64 | * @param end The end position of the range. |
65 | * @return A new `Range` object with the given start/end position. |
66 | */ |
67 | public static Range range(Position begin, Position end) { |
68 | return new Range(begin, end); |
69 | } |
70 | |
71 | /** |
72 | * Create a new `Range` object using the given begin and end line/column values. |
73 | * Valid values for each parameter are per the constructor of{@link Position}. |
74 | * |
75 | * @param beginLine The start line of the range. |
76 | * @param beginColumn The start line column of the range. |
77 | * @param endLine The end line of the range. |
78 | * @param endColumn The end column of the range. |
79 | * @return A new `Range` object with the given start/end position. |
80 | */ |
81 | public static Range range(int beginLine, int beginColumn, int endLine, int endColumn) { |
82 | return new Range(new Position(beginLine, beginColumn), new Position(endLine, endColumn)); |
83 | } |
84 | |
85 | /** |
86 | * @param beginColumn The value used to replace the current begin column number. |
87 | * Valid values are per the constructor of{@link Position}. |
88 | * @return A copy of this `Range` object, but with the begin column number replaced with the given column number. |
89 | */ |
90 | public Range withBeginColumn(int beginColumn) { |
91 | return range(begin.withColumn(beginColumn), end); |
92 | } |
93 | |
94 | /** |
95 | * @param beginLine The value used to replace the current begin line number. |
96 | * Valid values are per the constructor of{@link Position}. |
97 | * @return A copy of this `Range` object, but with the begin line number replaced with the given line number. |
98 | */ |
99 | public Range withBeginLine(int beginLine) { |
100 | return range(begin.withLine(beginLine), end); |
101 | } |
102 | |
103 | /** |
104 | * @param endColumn The value used to replace the current end column number. |
105 | * Valid values are per the constructor of{@link Position}. |
106 | * @return A copy of this `Range` object, but with the end column number replaced with the given line column. |
107 | */ |
108 | public Range withEndColumn(int endColumn) { |
109 | return range(begin, end.withColumn(endColumn)); |
110 | } |
111 | |
112 | /** |
113 | * @param endLine The value used to replace the current end line number. |
114 | * Valid values are per the constructor of{@link Position}. |
115 | * @return A copy of this `Range` object, but with the end line number replaced with the given line number. |
116 | */ |
117 | public Range withEndLine(int endLine) { |
118 | return range(begin, end.withLine(endLine)); |
119 | } |
120 | |
121 | |
122 | /** |
123 | * @param begin The value used to replace the current begin position. |
124 | * @return A copy of this `Range` object, but with the begin position replaced with the given position. |
125 | */ |
126 | public Range withBegin(Position begin) { |
127 | return range(begin, this.end); |
128 | } |
129 | |
130 | /** |
131 | * @param end The value used to replace the current end position. |
132 | * @return A copy of this `Range` object, but with the end position replaced with the given position. |
133 | */ |
134 | public Range withEnd(Position end) { |
135 | return range(this.begin, end); |
136 | } |
137 | |
138 | /** |
139 | * Does this loosely contain the other range? |
140 | * <p> |
141 | * As {@link #strictlyContains(Range)}, but also allow ranges which have an equal start and/or end position. |
142 | * In these cases, the `other` range is not strictly "inside" of this range. |
143 | */ |
144 | public boolean contains(Range other) { |
145 | boolean beginResult = (begin.isBeforeOrEqual(other.begin)); |
146 | boolean endResult = (end.isAfterOrEqual(other.end)); |
147 | return beginResult && endResult; |
148 | } |
149 | |
150 | /** |
151 | * Does this loosely contain the other range? |
152 | * <p> |
153 | * As {@link #strictlyContains(Position)}, but a position that is on the "edge" of this range will also pass. |
154 | * <p> |
155 | * For example, if the given position is equal to the start or end position of this range. |
156 | * In these cases, the `other` range is not strictly "inside" of this range. |
157 | */ |
158 | public boolean contains(Position position) { |
159 | return strictlyContains(position) || begin.equals(position) || end.equals(position); |
160 | } |
161 | |
162 | /** |
163 | * Does this strictly contain the other range? |
164 | * <p> |
165 | * It means that this has to be larger than other and it has to start before other and end after other. |
166 | */ |
167 | public boolean strictlyContains(Range other) { |
168 | boolean beginResult = (begin.isBefore(other.begin)); |
169 | boolean endResult = (end.isAfter(other.end)); |
170 | return beginResult && endResult; |
171 | } |
172 | |
173 | /** |
174 | * Does this strictly contain position. |
175 | * <p> |
176 | * It means that the position is after the begin of this range and before the end of this range. |
177 | */ |
178 | public boolean strictlyContains(Position position) { |
179 | return position.isAfter(begin) && position.isBefore(end); |
180 | } |
181 | |
182 | /** |
183 | * Does the other 'Range' overlap with this 'Range'? |
184 | * <p> |
185 | * If two ranges overlap, this range or the other range contains the begin or the end of the other range. |
186 | * <p> |
187 | * Note that if the ends are "touching" (i.e. a begin position == end position), this counts as an overlap |
188 | * because the positions refer to characters, as opposed to boundary between characters. |
189 | * <p> |
190 | * For example, there is an overlap at "C" in the following ranges, with "C" existing within both ranges: |
191 | * <pre> |
192 | * Range 1: ABC |
193 | * Range 2: CDE</pre> |
194 | */ |
195 | public boolean overlapsWith(Range other) { |
196 | return (contains(other.begin) || contains(other.end)) || |
197 | (other.contains(begin) || other.contains(end)); |
198 | } |
199 | |
200 | /** |
201 | * @param position The position to compare against. |
202 | * @return True if the end of this range is before (but not equal to) the given position to compare against. |
203 | */ |
204 | public boolean isBefore(Position position) { |
205 | return end.isBefore(position); |
206 | } |
207 | |
208 | /** |
209 | * @param position The position to compare against. |
210 | * @return True if the start of this range is after (but not equal to) the given position to compare against. |
211 | */ |
212 | public boolean isAfter(Position position) { |
213 | return begin.isAfter(position); |
214 | } |
215 | |
216 | @Override |
217 | public boolean equals(Object o) { |
218 | if (this == o) return true; |
219 | if (o == null || getClass() != o.getClass()) return false; |
220 | |
221 | Range range = (Range) o; |
222 | return begin.equals(range.begin) && end.equals(range.end); |
223 | } |
224 | |
225 | @Override |
226 | public int hashCode() { |
227 | return 31 * begin.hashCode() + end.hashCode(); |
228 | } |
229 | |
230 | @Override |
231 | public String toString() { |
232 | return begin + "-" + end; |
233 | } |
234 | |
235 | /** |
236 | * @return The number of lines that this range represents. |
237 | * <p> |
238 | * If the start line and end line are the same, this range is limited to just one line. |
239 | */ |
240 | public int getLineCount() { |
241 | return end.line - begin.line + 1; |
242 | } |
243 | } |
244 |
Members