001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2026 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.BitSet; 023 024/** 025 * Encapsulates representation of notion of expected indentation levels. 026 * Provide a way to have multiple acceptable levels. 027 * This class is immutable. 028 */ 029public class IndentLevel { 030 031 /** Set of acceptable indentation levels. */ 032 private final BitSet levels = new BitSet(); 033 034 /** 035 * Creates new instance with one acceptable indentation level. 036 * 037 * @param indent acceptable indentation level. 038 */ 039 public IndentLevel(int indent) { 040 levels.set(indent); 041 } 042 043 /** 044 * Creates new instance for nested structure. 045 * 046 * @param base parent's level 047 * @param offsets offsets from parent's level. 048 */ 049 public IndentLevel(IndentLevel base, int... offsets) { 050 final BitSet src = base.levels; 051 for (int index = src.nextSetBit(0); index >= 0; index = src.nextSetBit(index + 1)) { 052 for (int offset : offsets) { 053 levels.set(index + offset); 054 } 055 } 056 } 057 058 /** 059 * Creates new instance with no acceptable indentation level. 060 * This is only used internally to combine multiple levels. 061 */ 062 private IndentLevel() { 063 } 064 065 /** 066 * Checks whether we have more than one level. 067 * 068 * @return whether we have more than one level. 069 */ 070 public final boolean isMultiLevel() { 071 return levels.cardinality() > 1; 072 } 073 074 /** 075 * Checks if given indentation is acceptable. 076 * 077 * @param indent indentation to check. 078 * @return true if given indentation is acceptable, 079 * false otherwise. 080 */ 081 public boolean isAcceptable(int indent) { 082 return levels.get(indent); 083 } 084 085 /** 086 * Returns true if indent less than minimal of acceptable indentation levels, false otherwise. 087 * 088 * @param indent indentation to check. 089 * @return true if {@code indent} less than minimal of 090 * acceptable indentation levels, false otherwise. 091 */ 092 public boolean isGreaterThan(int indent) { 093 return levels.nextSetBit(0) > indent; 094 } 095 096 /** 097 * Adds one or more acceptable indentation level. 098 * 099 * @param base class to add new indentations to. 100 * @param additions new acceptable indentation. 101 * @return New acceptable indentation level instance. 102 */ 103 public static IndentLevel addAcceptable(IndentLevel base, int... additions) { 104 final IndentLevel result = new IndentLevel(); 105 result.levels.or(base.levels); 106 for (int addition : additions) { 107 result.levels.set(addition); 108 } 109 return result; 110 } 111 112 /** 113 * Combines 2 acceptable indentation level classes. 114 * 115 * @param base class to add new indentations to. 116 * @param addition new acceptable indentation. 117 * @return New acceptable indentation level instance. 118 */ 119 public static IndentLevel addAcceptable(IndentLevel base, IndentLevel addition) { 120 final IndentLevel result = new IndentLevel(); 121 result.levels.or(base.levels); 122 result.levels.or(addition.levels); 123 return result; 124 } 125 126 /** 127 * Returns first indentation level. 128 * 129 * @return indentation level. 130 */ 131 public int getFirstIndentLevel() { 132 return levels.nextSetBit(0); 133 } 134 135 /** 136 * Returns last indentation level. 137 * 138 * @return indentation level. 139 */ 140 public int getLastIndentLevel() { 141 return levels.length() - 1; 142 } 143 144 @Override 145 public String toString() { 146 final String result; 147 if (levels.cardinality() == 1) { 148 result = String.valueOf(levels.nextSetBit(0)); 149 } 150 else { 151 final StringBuilder sb = new StringBuilder(50); 152 for (int index = levels.nextSetBit(0); index >= 0; 153 index = levels.nextSetBit(index + 1)) { 154 if (!sb.isEmpty()) { 155 sb.append(", "); 156 } 157 sb.append(index); 158 } 159 result = sb.toString(); 160 } 161 return result; 162 } 163 164}