Skip to content

Commit

Permalink
Fixed sevntu-checkstyle#77. MapIterationInForEachLoop was introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
maxvetrenko committed Mar 1, 2014
1 parent 0196a73 commit bd8e158
Show file tree
Hide file tree
Showing 10 changed files with 1,116 additions and 0 deletions.
Expand Up @@ -53,6 +53,13 @@ IllegalCatchExtended.allowRethrow = Allow catching when re-throwing exception
LogicConditionNeedOptimizationCheck.name = Logic condition need optimization
LogicConditionNeedOptimizationCheck.desc = This check prevents the placement of local variables and fields after calling methods in '&&' and '||' conditions.

MapIterationInForEachLoop.name = Map Iteration In For Each Loop
MapIterationInForEachLoop.desc = This check can help you to write the whole for-each map iteration more correctly:<ol><li> 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. <p>Bad:</p><pre>for (Map.Entry<String, String>; entry : map.entrySet()){<br/> System.out.println(entry.getValue());<br/>}</pre><pre>for (String key : map.keySet(){<br/> System.out.println(map.get(key));<br/>}</pre><p>Good:</p><pre>for (String value : map.values()){<br/> System.out.println(value);<br/>}</pre> </li><li>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. <p>Bad:</p><pre>for (Map.Entry<String, String> entry : map.entrySet()){<br/> System.out.println(entry.getKey());<br/>}</pre><p>Good:</p><pre>for (String key : map.keySet()){<br/> System.out.println(key);<br/>}</pre></li><li>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. <p>Bad:</p><pre>for (String key : map.keySet()){<br/> System.out.println(key + " " + map.get(key));<br/>}</pre><p>Good:</p><pre>for (Map.Entry<String, String> entry : map.entrySet()){<br/> System.out.println(entry.getValue() + " " + entry.getKey());<br/>}</pre></li></ol>
MapIterationInForEachLoop.supportedMapImplQualifiedNames = Enter your own Map implementations.
MapIterationInForEachLoop.proposeValuesUsage = If this checkbox is checked, Check will propose to replace wrong usage to value().
MapIterationInForEachLoop.proposeKeySetUsage = If this checkbox is checked, Check will propose to replace wrong usage to keySet().
MapIterationInForEachLoop.proposeEntrySetUsage = If this checkbox is checked, Check will propose to replace wrong usage to entrySet().

MultipleVariableDeclarationsExtended.name = Multiple Variable Declarations Extended
MultipleVariableDeclarationsExtended.desc = Warn when declaring several variables in one line

Expand Down
Expand Up @@ -167,6 +167,32 @@
</property-metadata>
</rule-metadata>

<rule-metadata name="%MapIterationInForEachLoop.name"
internal-name="MapIterationInForEachLoop" parent="TreeWalker">
<alternative-name
internal-name="com.puppycrawl.tools.checkstyle.checks.coding.MapIterationInForEachLoopCheck" />
<description>%MapIterationInForEachLoop.desc</description>
<property-metadata name="supportedMapImplQualifiedNames"
datatype="String" default-value="java.util.Map, java.util.HashMap, java.util.TreeMap">
<description>%MapIterationInForEachLoop.supportedMapImplQualifiedNames</description>
</property-metadata>
<property-metadata name="proposeValuesUsage"
datatype="Boolean" default-value="true">
<description>%MapIterationInForEachLoop.proposeValuesUsage</description>
</property-metadata>
<property-metadata name="proposeKeySetUsage"
datatype="Boolean" default-value="false">
<description>%MapIterationInForEachLoop.proposeKeySetUsage</description>
</property-metadata>
<property-metadata name="proposeEntrySetUsage"
datatype="Boolean" default-value="false">
<description>%MapIterationInForEachLoop.proposeEntrySetUsage</description>
</property-metadata>
<message-key key="map.iteration.keySet" />
<message-key key="map.iteration.entrySet" />
<message-key key="map.iteration.values" />
</rule-metadata>

<rule-metadata name="%MultipleStringLiteralsExtended.name" internal-name="MultipleStringLiteralsExtended" parent="TreeWalker">
<alternative-name internal-name="com.github.sevntu.checkstyle.checks.coding.MultipleStringLiteralsExtendedCheck" />
<description>%MultipleStringLiteralsExtended.desc</description>
Expand Down

0 comments on commit bd8e158

Please sign in to comment.