View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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   * @author Martin von Gagern
28   */
29  public class LineColumn implements Comparable<LineColumn> {
30  
31      /** The one-based line number. */
32      private final int line;
33  
34      /** The zero-based column number. */
35      private final int column;
36  
37      /**
38       * Constructs a new pair of line and column numbers.
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       * @return the one-based line number
50       */
51      public int getLine() {
52          return line;
53      }
54  
55      /**
56       * Gets the zero-based column number.
57       * @return the zero-based column number
58       */
59      public int getColumn() {
60          return column;
61      }
62  
63      @Override
64      public int compareTo(LineColumn lineColumn) {
65          final int result;
66          if (line == lineColumn.line) {
67              result = Integer.compare(column, lineColumn.column);
68          }
69          else {
70              result = Integer.compare(line, lineColumn.line);
71          }
72          return result;
73      }
74  
75      @Override
76      public boolean equals(Object other) {
77          if (this == other) {
78              return true;
79          }
80          if (other == null || getClass() != other.getClass()) {
81              return false;
82          }
83          final LineColumn lineColumn = (LineColumn) other;
84          return Objects.equals(line, lineColumn.line)
85                  && Objects.equals(column, lineColumn.column);
86      }
87  
88      @Override
89      public int hashCode() {
90          return Objects.hash(line, column);
91      }
92  
93  }