1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.api;
21
22 import java.util.Objects;
23
24 /**
25 * Immutable line and column numbers.
26 *
27 */
28 public class LineColumn implements Comparable<LineColumn> {
29
30 /** The one-based line number. */
31 private final int line;
32
33 /** The zero-based column number. */
34 private final int column;
35
36 /**
37 * Constructs a new pair of line and column numbers.
38 *
39 * @param line the one-based line number
40 * @param column the zero-based column number
41 */
42 public LineColumn(int line, int column) {
43 this.line = line;
44 this.column = column;
45 }
46
47 /**
48 * Gets the one-based line number.
49 *
50 * @return the one-based line number
51 */
52 public int getLine() {
53 return line;
54 }
55
56 /**
57 * Gets the zero-based column number.
58 *
59 * @return the zero-based column number
60 */
61 public int getColumn() {
62 return column;
63 }
64
65 @Override
66 public int compareTo(LineColumn lineColumn) {
67 final int result;
68 if (line == lineColumn.line) {
69 result = Integer.compare(column, lineColumn.column);
70 }
71 else {
72 result = Integer.compare(line, lineColumn.line);
73 }
74 return result;
75 }
76
77 @Override
78 public boolean equals(Object other) {
79 if (this == other) {
80 return true;
81 }
82 if (other == null || getClass() != other.getClass()) {
83 return false;
84 }
85 final LineColumn lineColumn = (LineColumn) other;
86 return Objects.equals(line, lineColumn.line)
87 && Objects.equals(column, lineColumn.column);
88 }
89
90 @Override
91 public int hashCode() {
92 return Objects.hash(line, column);
93 }
94
95 }