Class MapIterationInForEachLoopCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.MapIterationInForEachLoopCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable,com.puppycrawl.tools.checkstyle.api.Contextualizable
public class MapIterationInForEachLoopCheck extends com.puppycrawl.tools.checkstyle.api.AbstractCheckThis check can help you to write the whole for-each map iteration more correctly.
1. If you iterate over a map using map.keySet() or map.entrySet(), but your code uses only map values, Check will propose you to use map.values() instead of map.keySet() or map.entrySet(). Replacing map.keySet() or map.entrySet() with map.values() for such cases can a bit improve an iteration performance.
Bad:
for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getValue()); }for (String key : map.keySet()) { System.out.println(map.get(key)); }Good:
for (String value : map.values()) { System.out.println(value); }2. If you iterate over a map using map.entrySet(), but never call entry.getValue(), Check will propose you to use map.keySet() instead of map.entrySet(). to iterate over map keys only.
Bad:
for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey()); }Good:
for (String key : map.keySet()) { System.out.println(key); }3. If you iterate over a map with map.keySet() and use both keys and values, check will propose you to use map.entrySet() to improve an iteration performance by avoiding search operations inside a map. For this case, iteration can significantly grow up a performance.
Bad:
for (String key : map.keySet()) { System.out.println(key + " " + map.get(key)); }Good:
for (Map.Entry<String, String>S entry : map.entrySet()) { System.out.println(entry.getValue() + " " + entry.getKey()); }- Since:
- 1.11.0
- Author:
- Max Vetrenko
-
-
Field Summary
Fields Modifier and Type Field Description static StringMSG_KEY_ENTRYSETThe key is pointing to the warning message text in "messages.properties" file.static StringMSG_KEY_KEYSETThe key is pointing to the warning message text in "messages.properties" file.static StringMSG_KEY_VALUESThe key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description MapIterationInForEachLoopCheck()Creates default importList and mapImportClassesNamesList.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbeginTree(com.puppycrawl.tools.checkstyle.api.DetailAST ast)int[]getAcceptableTokens()int[]getDefaultTokens()int[]getRequiredTokens()voidsetProposeEntrySetUsage(boolean proposeEntrySetUsage)Set aProcessingEntrySet.voidsetProposeKeySetUsage(boolean proposeKeySetUsage)Set aProcessingKeySet.voidsetProposeValuesUsage(boolean proposeValuesUsage)Set aProcessingValue.voidsetSupportedMapImplQualifiedNames(String... setSupportedMapImplQualifiedNames)Set user's map implementations.voidvisitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
-
-
-
Field Detail
-
MSG_KEY_KEYSET
public static final String MSG_KEY_KEYSET
The key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ENTRYSET
public static final String MSG_KEY_ENTRYSET
The key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_VALUES
public static final String MSG_KEY_VALUES
The key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setSupportedMapImplQualifiedNames
public final void setSupportedMapImplQualifiedNames(String... setSupportedMapImplQualifiedNames)
Set user's map implementations. It must state the full paths of imported classes. Import paths must be separated by commas. For example: java.util.Map, java.util.HashMap.- Parameters:
setSupportedMapImplQualifiedNames- User's set of map implementations.
-
setProposeValuesUsage
public void setProposeValuesUsage(boolean proposeValuesUsage)
Set aProcessingValue. If value is true, Check will process cases, where values() method will be suitable.- Parameters:
proposeValuesUsage- User's value of mProcessingValue.
-
setProposeKeySetUsage
public void setProposeKeySetUsage(boolean proposeKeySetUsage)
Set aProcessingKeySet. If value is true, Check will process cases, where keySet() method will be suitable.- Parameters:
proposeKeySetUsage- User's value of mIsCheckKeySetProcessingEnabled.
-
setProposeEntrySetUsage
public void setProposeEntrySetUsage(boolean proposeEntrySetUsage)
Set aProcessingEntrySet. If value is true, Check will process cases, where entrySet() method will be suitable.- Parameters:
proposeEntrySetUsage- User's value of mIsCheckEntrySetProcessingEnabled.
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokensin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
beginTree
public void beginTree(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
beginTreein classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitTokenin classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
-