001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 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 * Maps line numbers to their start column. 033 */ 034 private final SortedMap<Integer, Integer> lines = new TreeMap<>(); 035 036 /** 037 * Get the starting column for a given line number. 038 * 039 * @param lineNum the specified line number 040 * 041 * @return the starting column for the given line number 042 */ 043 public Integer getStartColumn(Integer lineNum) { 044 return lines.get(lineNum); 045 } 046 047 /** 048 * Get the starting column for the first line. 049 * 050 * @return the starting column for the first line. 051 */ 052 public int firstLineCol() { 053 final Integer firstLineKey = lines.firstKey(); 054 return lines.get(firstLineKey); 055 } 056 057 /** 058 * Get the line number of the first line. 059 * 060 * @return the line number of the first line 061 */ 062 public int firstLine() { 063 return lines.firstKey(); 064 } 065 066 /** 067 * Get the line number of the last line. 068 * 069 * @return the line number of the last line 070 */ 071 public int lastLine() { 072 return lines.lastKey(); 073 } 074 075 /** 076 * Add a line to this set of lines. 077 * 078 * @param lineNum the line to add 079 * @param col the starting column of the new line 080 */ 081 public void addLineAndCol(int lineNum, int col) { 082 lines.put(lineNum, col); 083 } 084 085 /** 086 * Determines if this set of lines is empty. 087 * 088 * @return true if it is empty, false otherwise 089 */ 090 public boolean isEmpty() { 091 return lines.isEmpty(); 092 } 093 094 @Override 095 public String toString() { 096 return "LineSet[firstLine=" + firstLine() + ", lastLine=" + lastLine() + "]"; 097 } 098}