From 8be45bd369fee43e420d3a8df69b818290fcb44e Mon Sep 17 00:00:00 2001 From: Brian Remedios Date: Sat, 24 Sep 2011 10:12:46 +0000 Subject: [PATCH] StringBuffer -> StringBuilder switchovers, refactoring git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7337 51baf565-9d33-0410-a72c-fc3788e3496d --- .../strings/AvoidDuplicateLiteralsRule.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java b/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java index 82163df27a..6e51062fc7 100644 --- a/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java +++ b/pmd/src/net/sourceforge/pmd/lang/java/rule/strings/AvoidDuplicateLiteralsRule.java @@ -60,7 +60,7 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { public Set parse(String s) { Set result = new HashSet(); - StringBuffer currentToken = new StringBuffer(); + StringBuilder currentToken = new StringBuilder(); boolean inEscapeMode = false; for (int i = 0; i < s.length(); i++) { if (inEscapeMode) { @@ -74,7 +74,7 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { } if (s.charAt(i) == delimiter) { result.add(currentToken.toString()); - currentToken = new StringBuffer(); + currentToken = new StringBuilder(); } else { currentToken.append(s.charAt(i)); } @@ -128,21 +128,30 @@ public class AvoidDuplicateLiteralsRule extends AbstractJavaRule { super.visit(node, data); - int threshold = getProperty(THRESHOLD_DESCRIPTOR); - for (String key : literals.keySet()) { - List occurrences = literals.get(key); - if (occurrences.size() >= threshold) { - Object[] args = new Object[] { key, Integer.valueOf(occurrences.size()), - Integer.valueOf(occurrences.get(0).getBeginLine()) }; - addViolation(data, occurrences.get(0), args); - } - } + processResults(data); minLength = 2 + getProperty(MINIMUM_LENGTH_DESCRIPTOR); return data; } + private void processResults(Object data) { + + int threshold = getProperty(THRESHOLD_DESCRIPTOR); + + for (Map.Entry> entry : literals.entrySet()) { + List occurrences = entry.getValue(); + if (occurrences.size() >= threshold) { + Object[] args = new Object[] { + entry.getKey(), + Integer.valueOf(occurrences.size()), + Integer.valueOf(occurrences.get(0).getBeginLine()) + }; + addViolation(data, occurrences.get(0), args); + } + } + } + @Override public Object visit(ASTLiteral node, Object data) { if (!node.isStringLiteral()) {