JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/Position.java
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
22package com.github.javaparser;
23
24import com.github.javaparser.ast.Node;
25
26import java.util.Objects;
27
28import static com.github.javaparser.utils.Utils.assertNotNull;
29
30/**
31 * A position in a source file. Lines and columns start counting at 1.
32 */
33public class Position implements Comparable<Position> {
34    public final int line;
35    public final int column;
36
37    /**
38     * The first line -- note that it is 1-indexed (i.e. the first line is line 1, as opposed to 0)
39     */
40    public static final int FIRST_LINE = 1;
41    /**
42     * The first column -- note that it is 1-indexed (i.e. the first column is column 1, as opposed to 0)
43     */
44    public static final int FIRST_COLUMN = 1;
45    /**
46     * The first position in the file.
47     */
48    public static final Position HOME = new Position(FIRST_LINEFIRST_COLUMN);
49
50
51    /**
52     * Line numbers must be positive, thus
53     */
54    public static final int ABSOLUTE_BEGIN_LINE = -1;
55
56    public static final int ABSOLUTE_END_LINE = -2;
57
58
59    /**
60     * TODO: Do we refer to the characters as columns,
61     *  ...or the spaces between (thus also before/after) characters as columns?
62     */
63    public Position(int lineint column) {
64        if (line < Position.ABSOLUTE_END_LINE) {
65            // TODO/FIXME: This doesn't read correctly due to use of the variable.
66            throw new IllegalArgumentException("Can't position at line " + line);
67        }
68        if (column < -1) {
69            // TODO: This allows/permits column 0, which seemingly contradicts first column being 1
70            //  ... (see also nextLine() which indicates 1 being the first column of the next line)
71            //  ... (see also valid() which requires a column > 0)
72            // TODO: Maybe we need an "ABSOLUTE_BEGIN_LINE" and "ABSOLUTE_END_LINE"?
73            throw new IllegalArgumentException("Can't position at column " + column);
74        }
75        this.line = line;
76        this.column = column;
77    }
78
79    /**
80     * Convenient factory method.
81     *
82     * @deprecated Use the constructor (e.g. {@code new Position(line, column)})
83     */
84    @Deprecated
85    public static Position pos(int lineint column) {
86        return new Position(linecolumn);
87    }
88
89    /**
90     * @return Jump to the given column number, while retaining the current line number.
91     */
92    public Position withColumn(int column) {
93        return new Position(this.linecolumn);
94    }
95
96    /**
97     * @return Jump to the given line number, while retaining the current column number.
98     */
99    public Position withLine(int line) {
100        return new Position(line, this.column);
101    }
102
103    /**
104     * @return a position that is "characters" characters more to the right than this position.
105     */
106    public Position right(int characters) {
107        return new Position(line, this.column + characters);
108    }
109
110    /**
111     * @return a position that is on the start of the next line from this position.
112     */
113    public Position nextLine() {
114        return new Position(line + 1FIRST_COLUMN);
115    }
116
117    /**
118     * Check if the position is usable.
119     * Does not know what it is pointing at, so it can't check if the position is after the end of the source.
120     */
121    public boolean valid() {
122        // TODO / FIXME: Perhaps allow use of the "special" positions e.g. ABSOLUTE_BEGIN_LINE and ABSOLUTE_END_LINE...?
123        return line >= FIRST_LINE && column >= FIRST_COLUMN;
124    }
125
126    /**
127     * @see #valid()
128     * @return The inverse of {@link #valid()}
129     */
130    public boolean invalid() {
131        return !valid();
132    }
133
134    /**
135     * @return If this position is valid, this.
136     *   Otherwise, if the alternativePosition is valid, return that.
137     *   Otherwise otherwise, just return this.
138     *   TODO: Simplify/clarify.
139     */
140    public Position orIfInvalid(Position alternativePosition) {
141        assertNotNull(alternativePosition);
142        // TODO: Why the || ?
143        //  ... It seems that if both this and the alternative are invalid, then we return this..?
144        if (valid() || alternativePosition.invalid()) {
145            return this;
146        }
147        return alternativePosition;
148    }
149
150    public boolean isAfter(Position otherPosition) {
151        assertNotNull(otherPosition);
152        if (otherPosition.line == Position.ABSOLUTE_BEGIN_LINE) {
153            // FIXME: What if both positions are on the same line but different columns..?
154            return true;
155        }
156        if (line > otherPosition.line) {
157            return true;
158        } else if (line == otherPosition.line) {
159            return column > otherPosition.column;
160        }
161        return false;
162
163    }
164
165    public boolean isAfterOrEqual(Position otherPosition) {
166        assertNotNull(otherPosition);
167        return isAfter(otherPosition) || equals(otherPosition);
168    }
169
170    public boolean isBefore(Position otherPosition) {
171        assertNotNull(otherPosition);
172        if (otherPosition.line == Position.ABSOLUTE_END_LINE) {
173            // FIXME: What if both positions are on the same line but different columns..?
174            return true;
175        }
176        if (line < otherPosition.line) {
177            return true;
178        } else if (line == otherPosition.line) {
179            return column < otherPosition.column;
180        }
181        return false;
182    }
183
184    public boolean isBeforeOrEqual(Position otherPosition) {
185        assertNotNull(otherPosition);
186        return isBefore(otherPosition) || equals(otherPosition);
187    }
188
189    @Override
190    public int compareTo(Position otherPosition) {
191        assertNotNull(otherPosition);
192        if (isBefore(otherPosition)) {
193            return -1;
194        }
195        if (isAfter(otherPosition)) {
196            return 1;
197        }
198        return 0;
199    }
200
201    @Override
202    public boolean equals(Object o) {
203        if (this == o) return true;
204        if (o == null || getClass() != o.getClass()) return false;
205
206        Position otherPosition = (Positiono;
207
208        return Objects.equals(lineotherPosition.line)
209                && Objects.equals(columnotherPosition.column);
210    }
211
212    @Override
213    public int hashCode() {
214        return Objects.hash(linecolumn);
215    }
216
217    @Override
218    public String toString() {
219        return "(line " + line + ",col " + column + ")";
220    }
221}
222
MembersX
Position:ABSOLUTE_BEGIN_LINE
Position:pos
Position:toString
Position:compareTo
Position:right
Position:hashCode
Position:orIfInvalid
Position:column
Position:isBeforeOrEqual
Position:valid
Position:ABSOLUTE_END_LINE
Position:withLine
Position:isAfterOrEqual
Position:FIRST_COLUMN
Position:equals:Block:otherPosition
Position:invalid
Position:nextLine
Position:withColumn
Position:line
Position:isAfter
Position:isBefore
Position:Position
Position:HOME
Position:equals
Position:FIRST_LINE
Members
X