001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.api;
021
022import java.util.Objects;
023
024/**
025 * Immutable line and column numbers.
026 *
027 */
028public class LineColumn implements Comparable<LineColumn> {
029
030    /** The one-based line number. */
031    private final int line;
032
033    /** The zero-based column number. */
034    private final int column;
035
036    /**
037     * Constructs a new pair of line and column numbers.
038     * @param line the one-based line number
039     * @param column the zero-based column number
040     */
041    public LineColumn(int line, int column) {
042        this.line = line;
043        this.column = column;
044    }
045
046    /**
047     * Gets the one-based line number.
048     * @return the one-based line number
049     */
050    public int getLine() {
051        return line;
052    }
053
054    /**
055     * Gets the zero-based column number.
056     * @return the zero-based column number
057     */
058    public int getColumn() {
059        return column;
060    }
061
062    @Override
063    public int compareTo(LineColumn lineColumn) {
064        final int result;
065        if (line == lineColumn.line) {
066            result = Integer.compare(column, lineColumn.column);
067        }
068        else {
069            result = Integer.compare(line, lineColumn.line);
070        }
071        return result;
072    }
073
074    @Override
075    public boolean equals(Object other) {
076        if (this == other) {
077            return true;
078        }
079        if (other == null || getClass() != other.getClass()) {
080            return false;
081        }
082        final LineColumn lineColumn = (LineColumn) other;
083        return Objects.equals(line, lineColumn.line)
084                && Objects.equals(column, lineColumn.column);
085    }
086
087    @Override
088    public int hashCode() {
089        return Objects.hash(line, column);
090    }
091
092}