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 * @author Martin von Gagern
028 */
029public class LineColumn implements Comparable<LineColumn> {
030
031    /** The one-based line number. */
032    private final int line;
033
034    /** The zero-based column number. */
035    private final int column;
036
037    /**
038     * Constructs a new pair of line and column numbers.
039     * @param line the one-based line number
040     * @param column the zero-based column number
041     */
042    public LineColumn(int line, int column) {
043        this.line = line;
044        this.column = column;
045    }
046
047    /**
048     * Gets the one-based line number.
049     * @return the one-based line number
050     */
051    public int getLine() {
052        return line;
053    }
054
055    /**
056     * Gets the zero-based column number.
057     * @return the zero-based column number
058     */
059    public int getColumn() {
060        return column;
061    }
062
063    @Override
064    public int compareTo(LineColumn lineColumn) {
065        final int result;
066        if (line == lineColumn.line) {
067            result = Integer.compare(column, lineColumn.column);
068        }
069        else {
070            result = Integer.compare(line, lineColumn.line);
071        }
072        return result;
073    }
074
075    @Override
076    public boolean equals(Object other) {
077        if (this == other) {
078            return true;
079        }
080        if (other == null || getClass() != other.getClass()) {
081            return false;
082        }
083        final LineColumn lineColumn = (LineColumn) other;
084        return Objects.equals(line, lineColumn.line)
085                && Objects.equals(column, lineColumn.column);
086    }
087
088    @Override
089    public int hashCode() {
090        return Objects.hash(line, column);
091    }
092
093}