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.checks.indentation;
021
022import java.util.SortedMap;
023import java.util.TreeMap;
024
025/**
026 * Represents a set of lines.
027 *
028 * @author jrichard
029 */
030public class LineSet {
031
032    /**
033     * Maps line numbers to their start column.
034     */
035    private final SortedMap<Integer, Integer> lines = new TreeMap<>();
036
037    /**
038     * Get the starting column for a given line number.
039     *
040     * @param lineNum   the specified line number
041     *
042     * @return the starting column for the given line number
043     */
044    public Integer getStartColumn(Integer lineNum) {
045        return lines.get(lineNum);
046    }
047
048    /**
049     * Get the starting column for the first line.
050     *
051     * @return the starting column for the first line.
052     */
053    public int firstLineCol() {
054        final Integer firstLineKey = lines.firstKey();
055        return lines.get(firstLineKey);
056    }
057
058    /**
059     * Get the line number of the first line.
060     *
061     * @return the line number of the first line
062     */
063    public int firstLine() {
064        return lines.firstKey();
065    }
066
067    /**
068     * Get the line number of the last line.
069     *
070     * @return the line number of the last line
071     */
072    public int lastLine() {
073        return lines.lastKey();
074    }
075
076    /**
077     * Add a line to this set of lines.
078     *
079     * @param lineNum   the line to add
080     * @param col       the starting column of the new line
081     */
082    public void addLineAndCol(int lineNum, int col) {
083        lines.put(lineNum, col);
084    }
085
086    /**
087     * Determines if this set of lines is empty.
088     *
089     * @return true if it is empty, false otherwise
090     */
091    public boolean isEmpty() {
092        return lines.isEmpty();
093    }
094
095    @Override
096    public String toString() {
097        return "LineSet[firstLine=" + lines.firstKey() + ", lastLine=" + lines.lastKey() + "]";
098    }
099
100}