View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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   * @noinspection ClassCanBeRecord
28   * @noinspectionreason Public API class – converting to record would break binary compatibility.
29   */
30  public class LineColumn implements Comparable<LineColumn> {
31  
32      /** The one-based line number. */
33      private final int line;
34  
35      /** The zero-based column number. */
36      private final int column;
37  
38      /**
39       * Constructs a new pair of line and column numbers.
40       *
41       * @param line the one-based line number
42       * @param column the zero-based column number
43       */
44      public LineColumn(int line, int column) {
45          this.line = line;
46          this.column = column;
47      }
48  
49      /**
50       * Gets the one-based line number.
51       *
52       * @return the one-based line number
53       */
54      public int getLine() {
55          return line;
56      }
57  
58      /**
59       * Gets the zero-based column number.
60       *
61       * @return the zero-based column number
62       */
63      public int getColumn() {
64          return column;
65      }
66  
67      @Override
68      public int compareTo(LineColumn lineColumn) {
69          final int result;
70          if (line == lineColumn.line) {
71              result = Integer.compare(column, lineColumn.column);
72          }
73          else {
74              result = Integer.compare(line, lineColumn.line);
75          }
76          return result;
77      }
78  
79      @Override
80      public boolean equals(Object other) {
81          if (this == other) {
82              return true;
83          }
84          if (other == null || getClass() != other.getClass()) {
85              return false;
86          }
87          final LineColumn lineColumn = (LineColumn) other;
88          return Objects.equals(line, lineColumn.line)
89                  && Objects.equals(column, lineColumn.column);
90      }
91  
92      @Override
93      public int hashCode() {
94          return Objects.hash(line, column);
95      }
96  
97  }