001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2016 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        if (line == lineColumn.line) {
066            return Integer.compare(column, lineColumn.column);
067        }
068        else {
069            return Integer.compare(line, lineColumn.line);
070        }
071    }
072
073    @Override
074    public boolean equals(Object other) {
075        if (this == other) {
076            return true;
077        }
078        if (other == null || getClass() != other.getClass()) {
079            return false;
080        }
081        final LineColumn lineColumn = (LineColumn) other;
082        return Objects.equals(line, lineColumn.line)
083                && Objects.equals(column, lineColumn.column);
084    }
085
086    @Override
087    public int hashCode() {
088        return Objects.hash(line, column);
089    }
090}