KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > indentation > IndentLevel


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
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.Iterator JavaDoc;
22 import java.util.SortedSet JavaDoc;
23 import java.util.TreeSet JavaDoc;
24
25 /**
26  * Encapsulates representation of notion of expected indentation levels.
27  * Provide a way to have multiple accaptable levels.
28  *
29  * @author o_sukhodolsky
30  */

31 public class IndentLevel
32 {
33     /** set of acceptable indentation levels. */
34     private final SortedSet JavaDoc mLevels = new TreeSet JavaDoc();
35
36     /**
37      * Creates new instance with one accaptable indentation level.
38      * @param aIndent accaptable indentation level.
39      */

40     public IndentLevel(int aIndent)
41     {
42         mLevels.add(new Integer JavaDoc(aIndent));
43     }
44
45     /**
46      * Creates new instance for nested structure.
47      * @param aBase parent's level
48      * @param aOffset offset from parent's level.
49      */

50     public IndentLevel(IndentLevel aBase, int aOffset)
51     {
52         for (final Iterator JavaDoc iter = aBase.mLevels.iterator(); iter.hasNext();) {
53             final int base = ((Integer JavaDoc) iter.next()).intValue();
54             mLevels.add(new Integer JavaDoc(base + aOffset));
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 mLevels.size() > 1;
65     }
66
67     /**
68      * Checks if given indentation is accaptable.
69      * @param aIndent indentation to check.
70      * @return true if givent indentation is acceptable,
71      * false otherwise.
72      */

73     public boolean accept(int aIndent)
74     {
75         return (mLevels.contains(new Integer JavaDoc(aIndent)));
76     }
77
78     /**
79      * @param aIndent indentation to check.
80      * @return true if <code>aIndent</code> less then minimal of
81      * accaptable indentation levels, false otherwise.
82      */

83     public boolean gt(int aIndent)
84     {
85         return (((Integer JavaDoc) mLevels.first()).intValue() > aIndent);
86     }
87
88     /**
89      * Adds one more acceptable indentation level.
90      * @param aIndent new acceptable indentation.
91      */

92     public void addAcceptedIndent(int aIndent)
93     {
94         mLevels.add(new Integer JavaDoc(aIndent));
95     }
96
97     /**
98      * Adds one more acceptable indentation level.
99      * @param aIndent new acceptable indentation.
100      */

101     public void addAcceptedIndent(IndentLevel aIndent)
102     {
103         mLevels.addAll(aIndent.mLevels);
104     }
105
106     /** @return string representation of the object. */
107     public String JavaDoc toString()
108     {
109         if (mLevels.size() == 1) {
110             return mLevels.first().toString();
111         }
112
113         return mLevels.toString();
114     }
115 }
116
Popular Tags