1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2015 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19 package com.puppycrawl.tools.checkstyle.checks.indentation;
20
21 import java.util.BitSet;
22
23 /**
24 * Encapsulates representation of notion of expected indentation levels.
25 * Provide a way to have multiple accaptable levels.
26 *
27 * @author o_sukhodolsky
28 */
29 public class IndentLevel
30 {
31 /** set of acceptable indentation levels. */
32 private final BitSet levels = new BitSet();
33
34 /**
35 * Creates new instance with one accaptable indentation level.
36 * @param indent accaptable indentation level.
37 */
38 public IndentLevel(int indent)
39 {
40 levels.set(indent);
41 }
42
43 /**
44 * Creates new instance for nested structure.
45 * @param base parent's level
46 * @param offsets offsets from parent's level.
47 */
48 public IndentLevel(IndentLevel base, int... offsets)
49 {
50 final BitSet src = base.levels;
51 for (int i = src.nextSetBit(0); i >= 0; i = src.nextSetBit(i + 1)) {
52 for (int offset : offsets) {
53 levels.set(i + offset);
54 }
55 }
56 }
57
58 /**
59 * Checks wether we have more than one level.
60 * @return wether we have more than one level.
61 */
62 public final boolean isMultiLevel()
63 {
64 return levels.cardinality() > 1;
65 }
66
67 /**
68 * Checks if given indentation is acceptable.
69 * @param indent indentation to check.
70 * @return true if given indentation is acceptable,
71 * false otherwise.
72 */
73 public boolean accept(int indent)
74 {
75 return levels.get(indent);
76 }
77
78 /**
79 * @param indent indentation to check.
80 * @return true if <code>indent</code> less then minimal of
81 * acceptable indentation levels, false otherwise.
82 */
83 public boolean gt(int indent)
84 {
85 return levels.nextSetBit(0) > indent;
86 }
87
88 /**
89 * Adds one more acceptable indentation level.
90 * @param indent new acceptable indentation.
91 */
92 public void addAcceptedIndent(int indent)
93 {
94 levels.set(indent);
95 }
96
97 /**
98 * Adds one more acceptable indentation level.
99 * @param indent new acceptable indentation.
100 */
101 public void addAcceptedIndent(IndentLevel indent)
102 {
103 levels.or(indent.levels);
104 }
105
106 /**
107 * Returns first indentation level.
108 * @return indentation level.
109 */
110 public int getFirstIndentLevel()
111 {
112 return levels.nextSetBit(0);
113 }
114
115 /**
116 * Returns last indentation level.
117 * @return indentation level.
118 */
119 public int getLastIndentLevel()
120 {
121 return levels.length() - 1;
122 }
123
124 @Override
125 public String toString()
126 {
127 if (levels.cardinality() == 1) {
128 return String.valueOf(levels.nextSetBit(0));
129 }
130 final StringBuilder sb = new StringBuilder();
131 for (int i = levels.nextSetBit(0); i >= 0;
132 i = levels.nextSetBit(i + 1))
133 {
134 if (sb.length() > 0) {
135 sb.append(", ");
136 }
137 sb.append(i);
138 }
139 return sb.toString();
140 }
141 }