diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index dc35843e1..fd523a833 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -25,6 +25,7 @@ import com.puppycrawl.tools.checkstyle.api.AutomaticBean; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; import com.puppycrawl.tools.checkstyle.api.Context; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.FileSetCheck; import com.puppycrawl.tools.checkstyle.api.Filter; import com.puppycrawl.tools.checkstyle.api.FilterSet; @@ -37,7 +38,6 @@ import java.io.File; import java.util.List; import java.util.Locale; import java.util.Set; -import java.util.Stack; import java.util.StringTokenizer; /** @@ -369,7 +369,7 @@ public class Checker extends AutomaticBean } } - final Stack s = new Stack(); + final FastStack s = FastStack.newInstance(); s.push(root); final StringTokenizer tok = new StringTokenizer(aPath, File.separator); while (tok.hasMoreTokens()) { @@ -396,7 +396,7 @@ public class Checker extends AutomaticBean // already contains one sb.append(File.separatorChar); } - sb.append(s.elementAt(i)); + sb.append(s.get(i)); } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java index bf0b25d24..532e80593 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java @@ -23,6 +23,7 @@ import com.google.common.collect.Maps; import com.puppycrawl.tools.checkstyle.api.AbstractLoader; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; import com.puppycrawl.tools.checkstyle.api.Configuration; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.SeverityLevel; import java.io.BufferedInputStream; import java.io.FileInputStream; @@ -34,7 +35,6 @@ import java.net.URL; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Stack; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; @@ -198,8 +198,8 @@ public final class ConfigurationLoader /** property resolver **/ private final PropertyResolver mOverridePropsResolver; /** the loaded configurations **/ - private final Stack mConfigStack = - new Stack(); + private final FastStack mConfigStack = + FastStack.newInstance(); /** the Configuration that is being built */ private Configuration mConfiguration; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java index b57ecc28f..5a8392989 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/PackageNamesLoader.java @@ -21,13 +21,13 @@ package com.puppycrawl.tools.checkstyle; import com.google.common.collect.Sets; import com.puppycrawl.tools.checkstyle.api.AbstractLoader; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.FastStack; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Enumeration; import java.util.Set; -import java.util.Stack; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; @@ -56,7 +56,7 @@ public final class PackageNamesLoader "checkstyle_packages.xml"; /** The temporary stack of package name parts */ - private final Stack mPackageStack = new Stack(); + private final FastStack mPackageStack = FastStack.newInstance(); /** The fully qualified package names. */ private final Set mPackageNames = Sets.newLinkedHashSet(); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FastStack.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FastStack.java new file mode 100644 index 000000000..b7880f219 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FastStack.java @@ -0,0 +1,139 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2008 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.api; + +import com.google.common.collect.Lists; +import java.util.Iterator; +import java.util.List; + +/** + * Simple implementation of a LIFO Stack that can be used instead of + * {@link java.util.Vector} which is synchronized. + * @author oliverb + * @param The type to hold. + */ +public class FastStack implements Iterable +{ + /** Hold the entries in the stack. */ + private final List mEntries = Lists.newArrayList(); + + /** + * Pushes the supplied element onto the stack. + * @param aElement the element to push onto the stack. + */ + public void push(E aElement) + { + mEntries.add(aElement); + } + + /** + * Returns whether the stack is empty. + * @return whether the stack is empty. + */ + public boolean isEmpty() + { + return mEntries.size() == 0; + } + + /** + * Returns the number of entries in the stack. + * @return the number of entries in the stack. + */ + public int size() + { + return mEntries.size(); + } + + /** + * Returns the entry at the top of the stack without removing it. + * @return the top entry + * @throws IllegalStateException if the stack is empty. + */ + public E peek() + { + if (mEntries.size() == 0) { + throw new IllegalStateException("FastStack is empty"); + } + return mEntries.get(mEntries.size() - 1); + } + + /** + * Returns the entry at the top of the stack by removing it. + * @return the top entry + * @throws IllegalStateException if the stack is empty. + */ + public E pop() + { + if (mEntries.size() == 0) { + throw new IllegalStateException("FastStack is empty"); + } + return mEntries.remove(mEntries.size() - 1); + } + + /** + * Return the element at the specified index. + * @param aIndex the index to return + * @return the element at the index + * @throws IllegalArgumentException if index out of range + */ + public E get(int aIndex) + { + if ((aIndex < 0) || (aIndex >= mEntries.size())) { + throw new IllegalArgumentException("index out of range."); + } + return mEntries.get(aIndex); + } + + /** + * Returns if the stack contains the specified element. + * @param aElement the element to find + * @return whether the stack contains the entry + */ + public boolean contains(E aElement) + { + return mEntries.contains(aElement); + } + + /** + * Clears the stack. + */ + public void clear() + { + mEntries.clear(); + } + + /** + * Returns an interator that goes from the oldest element to the newest. + * @return an iterator + */ + public Iterator iterator() + { + return mEntries.iterator(); + } + + /** + * Factory method to create a new instance. + * @param the type of elements to hold in the stack. + * @return a new instance of {@link FastStack} + */ + public static FastStack newInstance() + { + return new FastStack(); + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java index a3746aa08..4af5eb457 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/DeclarationOrderCheck.java @@ -20,10 +20,10 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.Scope; import com.puppycrawl.tools.checkstyle.api.ScopeUtils; import com.puppycrawl.tools.checkstyle.api.TokenTypes; -import java.util.Stack; /** *

@@ -72,7 +72,7 @@ public class DeclarationOrderCheck extends Check * List of Declaration States. This is necessary due to * inner classes that have their own state */ - private final Stack mScopeStates = new Stack(); + private final FastStack mScopeStates = FastStack.newInstance(); /** * private class to encapsulate the state diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java index f95631d84..caa46cc4a 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/FinalLocalVariableCheck.java @@ -20,11 +20,11 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.ScopeUtils; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import java.util.HashMap; import java.util.Map; -import java.util.Stack; /** *

@@ -44,8 +44,8 @@ import java.util.Stack; public class FinalLocalVariableCheck extends Check { /** Scope Stack */ - private final Stack> mScopeStack = - new Stack>(); + private final FastStack> mScopeStack = + FastStack.newInstance(); @Override public int[] getDefaultTokens() diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ModifiedControlVariableCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ModifiedControlVariableCheck.java index 166cf3233..e351435b5 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ModifiedControlVariableCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ModifiedControlVariableCheck.java @@ -20,8 +20,8 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; -import java.util.Stack; /** * Check for ensuring that for loop control variables are not modified * inside the for block. @@ -31,10 +31,10 @@ import java.util.Stack; public final class ModifiedControlVariableCheck extends Check { /** Current set of parameters. */ - private Stack mCurrentVariables = new Stack(); + private FastStack mCurrentVariables = FastStack.newInstance(); /** Stack of block parameters. */ - private final Stack> mVariableStack = - new Stack>(); + private final FastStack> mVariableStack = + FastStack.newInstance(); @Override public int[] getDefaultTokens() @@ -157,7 +157,7 @@ public final class ModifiedControlVariableCheck extends Check private void enterBlock() { mVariableStack.push(mCurrentVariables); - mCurrentVariables = new Stack(); + mCurrentVariables = FastStack.newInstance(); } /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ParameterAssignmentCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ParameterAssignmentCheck.java index 350482cb6..ca37d2552 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ParameterAssignmentCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ParameterAssignmentCheck.java @@ -21,9 +21,9 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import com.google.common.collect.Sets; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import java.util.Set; -import java.util.Stack; /** *

@@ -42,8 +42,8 @@ import java.util.Stack; public final class ParameterAssignmentCheck extends Check { /** Stack of methods' parameters. */ - private final Stack> mParameterNamesStack = - new Stack>(); + private final FastStack> mParameterNamesStack = + FastStack.newInstance(); /** Current set of perameters. */ private Set mParameterNames; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java index 6000dfb60..5a2ba5260 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java @@ -19,9 +19,9 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; -import java.util.Stack; /** *

@@ -42,7 +42,7 @@ public final class ReturnCountCheck extends AbstractFormatCheck private static final int DEFAULT_MAX = 2; /** Stack of method contexts. */ - private final Stack mContextStack = new Stack(); + private final FastStack mContextStack = FastStack.newInstance(); /** Maximum allowed number of return stmts. */ private int mMax; /** Current method context. */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/FinalClassCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/FinalClassCheck.java index bd4c6e557..ae3725122 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/FinalClassCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/FinalClassCheck.java @@ -20,9 +20,9 @@ package com.puppycrawl.tools.checkstyle.checks.design; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.ScopeUtils; import com.puppycrawl.tools.checkstyle.api.TokenTypes; -import java.util.Stack; /** *

@@ -41,7 +41,7 @@ public class FinalClassCheck extends Check { /** Keeps ClassDesc objects for stack of declared classes. */ - private final Stack mClasses = new Stack(); + private final FastStack mClasses = FastStack.newInstance(); @Override public int[] getDefaultTokens() diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java index 3bbdc88fe..5c51a9fb2 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java @@ -19,9 +19,9 @@ package com.puppycrawl.tools.checkstyle.checks.design; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; -import java.util.Stack; /** *

Ensures that exceptions (defined as any class name conforming @@ -41,7 +41,7 @@ public final class MutableExceptionCheck extends AbstractFormatCheck /** Default value for format property. */ private static final String DEFAULT_FORMAT = "^.*Exception$|^.*Error$"; /** Stack of checking information for classes. */ - private final Stack mCheckingStack = new Stack(); + private final FastStack mCheckingStack = FastStack.newInstance(); /** Should we check current class or not. */ private boolean mChecking; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java index 7c0097a83..de9d4a558 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/ImportControlLoader.java @@ -20,11 +20,11 @@ package com.puppycrawl.tools.checkstyle.checks.imports; import com.puppycrawl.tools.checkstyle.api.AbstractLoader; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.FastStack; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URI; -import java.util.Stack; import javax.xml.parsers.ParserConfigurationException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; @@ -45,7 +45,7 @@ final class ImportControlLoader extends AbstractLoader "com/puppycrawl/tools/checkstyle/checks/imports/import_control_1_0.dtd"; /** Used to hold the {@link PkgControl} objects. */ - private final Stack mStack = new Stack(); + private final FastStack mStack = FastStack.newInstance(); /** * Constructs an instance. diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocStyleCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocStyleCheck.java index efb95e33e..2b0f87328 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocStyleCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocStyleCheck.java @@ -20,6 +20,7 @@ package com.puppycrawl.tools.checkstyle.checks.javadoc; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.FileContents; import com.puppycrawl.tools.checkstyle.api.Scope; import com.puppycrawl.tools.checkstyle.api.ScopeUtils; @@ -27,7 +28,6 @@ import com.puppycrawl.tools.checkstyle.api.TextBlock; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.checks.CheckUtils; import java.util.List; -import java.util.Stack; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -293,7 +293,7 @@ public class JavadocStyleCheck private void checkHtml(final DetailAST aAST, final TextBlock aComment) { final int lineno = aComment.getStartLineNo(); - final Stack htmlStack = new Stack(); + final FastStack htmlStack = FastStack.newInstance(); final String[] text = aComment.getText(); final List typeParameters = CheckUtils.getTypeParameterNames(aAST); @@ -335,8 +335,7 @@ public class JavadocStyleCheck // Identify any tags left on the stack. String lastFound = ""; // Skip multiples, like ... - for (int i = 0; i < htmlStack.size(); i++) { - final HtmlTag htag = htmlStack.elementAt(i); + for (final HtmlTag htag : htmlStack) { if (!isSingleTag(htag) && !htag.getId().equals(lastFound) && !typeParameters.contains(htag.getId())) @@ -356,9 +355,9 @@ public class JavadocStyleCheck * @param aHtmlStack the stack of opened HTML tags. * @param aToken the current HTML tag name that has been closed. */ - private void checkUnclosedTags(Stack aHtmlStack, String aToken) + private void checkUnclosedTags(FastStack aHtmlStack, String aToken) { - final Stack unclosedTags = new Stack(); + final FastStack unclosedTags = FastStack.newInstance(); HtmlTag lastOpenTag = aHtmlStack.pop(); while (!aToken.equalsIgnoreCase(lastOpenTag.getId())) { // Find unclosed elements. Put them on a stack so the @@ -374,8 +373,8 @@ public class JavadocStyleCheck // Output the unterminated tags, if any String lastFound = ""; // Skip multiples, like .. - for (int i = 0; i < unclosedTags.size(); i++) { - lastOpenTag = unclosedTags.get(i); + for (final HtmlTag htag : unclosedTags) { + lastOpenTag = htag; if (lastOpenTag.getId().equals(lastFound)) { continue; } @@ -417,15 +416,14 @@ public class JavadocStyleCheck * @return false if a previous open tag was found * for the token. */ - private boolean isExtraHtml(String aToken, Stack aHtmlStack) + private boolean isExtraHtml(String aToken, FastStack aHtmlStack) { boolean isExtra = true; - for (int i = 0; i < aHtmlStack.size(); i++) { + for (final HtmlTag td : aHtmlStack) { // Loop, looking for tags that are closed. // The loop is needed in case there are unclosed // tags on the stack. In that case, the stack would // not be empty, but this tag would still be extra. - final HtmlTag td = aHtmlStack.elementAt(i); if (aToken.equalsIgnoreCase(td.getId())) { isExtra = false; break; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractClassCouplingCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractClassCouplingCheck.java index 378652878..1515fcfc0 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractClassCouplingCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractClassCouplingCheck.java @@ -21,11 +21,11 @@ package com.puppycrawl.tools.checkstyle.checks.metrics; import com.google.common.collect.Sets; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.FullIdent; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.checks.CheckUtils; import java.util.Set; -import java.util.Stack; /** * Base class for coupling calculation. @@ -43,7 +43,7 @@ public abstract class AbstractClassCouplingCheck extends Check private String mPackageName; /** Stack of contexts. */ - private final Stack mContextStack = new Stack(); + private final FastStack mContextStack = FastStack.newInstance(); /** Current context. */ private Context mContext; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractComplexityCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractComplexityCheck.java index 0ef20c280..dedf3fab6 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractComplexityCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractComplexityCheck.java @@ -20,9 +20,9 @@ package com.puppycrawl.tools.checkstyle.checks.metrics; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import java.math.BigInteger; -import java.util.Stack; /** * Base class for checks the calculate complexity based around methods. @@ -37,7 +37,7 @@ public abstract class AbstractComplexityCheck private static final BigInteger INITIAL_VALUE = BigInteger.ONE; /** stack of values - all but the current value */ - private final Stack mValueStack = new Stack(); + private final FastStack mValueStack = FastStack.newInstance(); /** the current value */ private BigInteger mCurrentValue = BigInteger.ZERO; diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java index 6362059fa..b36814f02 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java @@ -20,9 +20,9 @@ package com.puppycrawl.tools.checkstyle.checks.metrics; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; import com.puppycrawl.tools.checkstyle.checks.CheckUtils; -import java.util.Stack; /** * Restricts nested boolean operators (&&, ||, &, | and ^) to @@ -37,7 +37,7 @@ public final class BooleanExpressionComplexityCheck extends Check private static final int DEFAULT_MAX = 3; /** Stack of contexts. */ - private final Stack mContextStack = new Stack(); + private final FastStack mContextStack = FastStack.newInstance(); /** Maximum allowed complexity. */ private int mMax; /** Current context. */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java index 074b94c8f..aa1be3093 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/JavaNCSSCheck.java @@ -20,8 +20,8 @@ package com.puppycrawl.tools.checkstyle.checks.metrics; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; -import java.util.Stack; /** * This check calculates the Non Commenting Source Statements (NCSS) metric for @@ -56,7 +56,7 @@ public class JavaNCSSCheck extends Check private int mMethodMax = METHOD_MAX_NCSS; /** list containing the stacked counters */ - private Stack mCounters; + private FastStack mCounters; @Override public int[] getDefaultTokens() @@ -131,7 +131,7 @@ public class JavaNCSSCheck extends Check @Override public void beginTree(DetailAST aRootAST) { - mCounters = new Stack(); + mCounters = new FastStack(); //add a counter for the file mCounters.push(new Counter()); @@ -155,9 +155,8 @@ public class JavaNCSSCheck extends Check //check if token is countable if (isCountable(aAST)) { //increment the stacked counters - final int size = mCounters.size(); - for (int i = 0; i < size; i++) { - (mCounters.get(i)).increment(); + for (final Counter c : mCounters) { + c.increment(); } } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/sizes/ExecutableStatementCountCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/sizes/ExecutableStatementCountCheck.java index 18f11ac02..5020b9231 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/sizes/ExecutableStatementCountCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/sizes/ExecutableStatementCountCheck.java @@ -20,8 +20,8 @@ package com.puppycrawl.tools.checkstyle.checks.sizes; import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FastStack; import com.puppycrawl.tools.checkstyle.api.TokenTypes; -import java.util.Stack; /** * Restricts the number of executable statements to a specified limit @@ -38,7 +38,7 @@ public final class ExecutableStatementCountCheck private int mMax; /** Stack of method contexts. */ - private final Stack mContextStack = new Stack(); + private final FastStack mContextStack = FastStack.newInstance(); /** Current method context. */ private Context mContext; diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java b/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java index b79302bce..90b1d5f72 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/AllApiTests.java @@ -6,7 +6,7 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses( {AbstractViolationReporterTest.class, AutomaticBeanTest.class, DetailASTTest.class, ScopeTest.class, - TokenTypesTest.class}) + TokenTypesTest.class, FastStackTest.class}) public class AllApiTests { } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/api/FastStackTest.java b/src/tests/com/puppycrawl/tools/checkstyle/api/FastStackTest.java new file mode 100644 index 000000000..bd791790c --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/api/FastStackTest.java @@ -0,0 +1,59 @@ +package com.puppycrawl.tools.checkstyle.api; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class FastStackTest +{ + @Test(expected = IllegalStateException.class) + public void testPeek() + { + new FastStack().peek(); + } + + @Test(expected = IllegalStateException.class) + public void testPop() + { + new FastStack().pop(); + } + + @Test + public void testNormal() + { + final FastStack fs = FastStack.newInstance(); + assertNotNull(fs); + assertTrue(fs.isEmpty()); + assertEquals(0, fs.size()); + final int num = 100; + for (int i = 0; i < num; i++) + { + fs.push(i); + } + assertEquals(num, fs.size()); + assertFalse(fs.isEmpty()); + assertEquals(1, fs.get(1).intValue()); + assertEquals(num - 1, fs.peek().intValue()); + assertEquals(num, fs.size()); + for (int i = 0; i < num; i++) + { + fs.pop(); + } + assertTrue(fs.isEmpty()); + assertEquals(0, fs.size()); + + fs.push(666); + assertEquals(1, fs.size()); + assertFalse(fs.isEmpty()); + assertTrue(fs.contains(666)); + assertFalse(fs.contains(667)); + fs.clear(); + assertFalse(fs.contains(666)); + assertTrue(fs.isEmpty()); + assertEquals(0, fs.size()); + assertFalse(fs.iterator().hasNext()); + } +}