From a15b62b572bf8a77ac0fd72747f47b856aa6ea0f Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Mon, 27 Apr 2009 12:35:19 +0000 Subject: [PATCH] use immutablemap where possible --- .../tools/checkstyle/api/TokenTypes.java | 13 ++++++---- .../checks/imports/AccessResult.java | 16 ++++++------ .../SuppressWithNearbyCommentFilter.java | 25 ++++++++----------- suppressions.xml | 2 +- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/TokenTypes.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/TokenTypes.java index 894b09090..f710aaf6e 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/TokenTypes.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/TokenTypes.java @@ -18,10 +18,10 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.api; -import com.google.common.collect.Maps; +import com.google.common.collect.ImmutableMap; + import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaTokenTypes; import java.lang.reflect.Field; -import java.util.Map; import java.util.ResourceBundle; /** @@ -3337,13 +3337,14 @@ public final class TokenTypes //////////////////////////////////////////////////////////////////////// /** maps from a token name to value */ - private static final Map TOKEN_NAME_TO_VALUE = - Maps.newHashMap(); + private static final ImmutableMap TOKEN_NAME_TO_VALUE; /** maps from a token value to name */ private static final String[] TOKEN_VALUE_TO_NAME; // initialise the constants static { + final ImmutableMap.Builder builder = + ImmutableMap.builder(); final Field[] fields = TokenTypes.class.getDeclaredFields(); String[] tempTokenValueToName = new String[0]; for (final Field f : fields) { @@ -3355,7 +3356,7 @@ public final class TokenTypes final String name = f.getName(); try { final int tokenValue = f.getInt(name); - TOKEN_NAME_TO_VALUE.put(name, tokenValue); + builder.put(name, tokenValue); if (tokenValue > tempTokenValueToName.length - 1) { final String[] temp = new String[tokenValue + 1]; System.arraycopy(tempTokenValueToName, 0, @@ -3373,6 +3374,8 @@ public final class TokenTypes System.exit(1); } } + + TOKEN_NAME_TO_VALUE = builder.build(); TOKEN_VALUE_TO_NAME = tempTokenValueToName; } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/AccessResult.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/AccessResult.java index 244cb02f0..f30dd793b 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/AccessResult.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/imports/AccessResult.java @@ -18,9 +18,9 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.checks.imports; -import com.google.common.collect.Maps; +import com.google.common.collect.ImmutableMap; + import java.io.Serializable; -import java.util.Map; /** * Represents the result of an access check. @@ -54,13 +54,15 @@ final class AccessResult LABEL_UNKNOWN); /** map from results names to the respective result */ - private static final Map NAME_TO_LEVEL = - Maps.newHashMap(); + private static final ImmutableMap NAME_TO_LEVEL; static { - NAME_TO_LEVEL.put(LABEL_ALLOWED, ALLOWED); - NAME_TO_LEVEL.put(LABEL_DISALLOWED, DISALLOWED); - NAME_TO_LEVEL.put(LABEL_UNKNOWN, UNKNOWN); + final ImmutableMap.Builder builder = + ImmutableMap.builder(); + builder.put(LABEL_ALLOWED, ALLOWED); + builder.put(LABEL_DISALLOWED, DISALLOWED); + builder.put(LABEL_UNKNOWN, UNKNOWN); + NAME_TO_LEVEL = builder.build(); } /** Code for the access result. */ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java index 8d0b5146f..f33b801d4 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java @@ -18,19 +18,7 @@ //////////////////////////////////////////////////////////////////////////////// package com.puppycrawl.tools.checkstyle.filters; -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -import org.apache.commons.beanutils.ConversionException; - +import com.google.common.collect.Lists; import com.puppycrawl.tools.checkstyle.api.AuditEvent; import com.puppycrawl.tools.checkstyle.api.AutomaticBean; import com.puppycrawl.tools.checkstyle.api.FileContents; @@ -38,6 +26,15 @@ import com.puppycrawl.tools.checkstyle.api.Filter; import com.puppycrawl.tools.checkstyle.api.TextBlock; import com.puppycrawl.tools.checkstyle.api.Utils; import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder; +import java.lang.ref.WeakReference; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import org.apache.commons.beanutils.ConversionException; /** *

@@ -292,7 +289,7 @@ public class SuppressWithNearbyCommentFilter //TODO: Investigate performance improvement with array /** Tagged comments */ - private final List mTags = new ArrayList(); + private final List mTags = Lists.newArrayList(); /** * References the current FileContents for this filter. diff --git a/suppressions.xml b/suppressions.xml index 8dac962b9..904f92151 100755 --- a/suppressions.xml +++ b/suppressions.xml @@ -22,7 +22,7 @@ lines="28"/> + lines="28"/>