001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2014 Oliver Burn 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//////////////////////////////////////////////////////////////////////////////// 019package com.puppycrawl.tools.checkstyle.checks.indentation; 020 021import java.util.SortedSet; 022 023import com.google.common.base.Joiner; 024import com.google.common.collect.Sets; 025 026/** 027 * Encapsulates representation of notion of expected indentation levels. 028 * Provide a way to have multiple accaptable levels. 029 * 030 * @author o_sukhodolsky 031 */ 032public class IndentLevel 033{ 034 /** set of acceptable indentation levels. */ 035 private final SortedSet<Integer> mLevels = Sets.newTreeSet(); 036 037 /** 038 * Creates new instance with one accaptable indentation level. 039 * @param aIndent accaptable indentation level. 040 */ 041 public IndentLevel(int aIndent) 042 { 043 mLevels.add(aIndent); 044 } 045 046 /** 047 * Creates new instance for nested structure. 048 * @param aBase parent's level 049 * @param aOffsets offsets from parent's level. 050 */ 051 public IndentLevel(IndentLevel aBase, int... aOffsets) 052 { 053 for (Integer base : aBase.mLevels) { 054 for (int offset : aOffsets) { 055 mLevels.add(base + offset); 056 } 057 } 058 } 059 060 /** 061 * Checks wether we have more than one level. 062 * @return wether we have more than one level. 063 */ 064 public final boolean isMultiLevel() 065 { 066 return mLevels.size() > 1; 067 } 068 069 /** 070 * Checks if given indentation is acceptable. 071 * @param aIndent indentation to check. 072 * @return true if given indentation is acceptable, 073 * false otherwise. 074 */ 075 public boolean accept(int aIndent) 076 { 077 return (mLevels.contains(aIndent)); 078 } 079 080 /** 081 * @param aIndent indentation to check. 082 * @return true if <code>aIndent</code> less then minimal of 083 * acceptable indentation levels, false otherwise. 084 */ 085 public boolean gt(int aIndent) 086 { 087 return ((mLevels.first()).intValue() > aIndent); 088 } 089 090 /** 091 * Adds one more acceptable indentation level. 092 * @param aIndent new acceptable indentation. 093 */ 094 public void addAcceptedIndent(int aIndent) 095 { 096 mLevels.add(aIndent); 097 } 098 099 /** 100 * Adds one more acceptable indentation level. 101 * @param aIndent new acceptable indentation. 102 */ 103 public void addAcceptedIndent(IndentLevel aIndent) 104 { 105 mLevels.addAll(aIndent.mLevels); 106 } 107 108 /** 109 * Returns first indentation level. 110 * @return indentation level. 111 */ 112 public int getFirstIndentLevel() 113 { 114 return mLevels.first(); 115 } 116 117 /** 118 * Returns last indentation level. 119 * @return indentation level. 120 */ 121 public int getLastIndentLevel() 122 { 123 return mLevels.last(); 124 } 125 126 @Override 127 public String toString() 128 { 129 return Joiner.on(", ").join(mLevels); 130 } 131}