2121import org .springframework .util .MultiValueMap ;
2222
2323/**
24- * {@link VariableNameFactory} implementation keeping track of defined names resolving name clashes using internal
25- * counter appending {@code _%d} to a suggested name in case of a clash.
24+ * Non thread safe {@link VariableNameFactory} implementation keeping track of defined names resolving name clashes
25+ * using internal counters appending {@code _%d} to a suggested name in case of a clash.
2626 *
2727 * @author Christoph Strobl
2828 * @since 4.0
@@ -31,36 +31,43 @@ class LocalVariableNameFactory implements VariableNameFactory {
3131
3232 private final MultiValueMap <String , String > variables ;
3333
34+ /**
35+ * Create a new {@link LocalVariableNameFactory} considering available {@link MethodMetadata#getMethodArguments()
36+ * method arguments}.
37+ *
38+ * @param methodMetadata source metadata
39+ * @return new instance of {@link LocalVariableNameFactory}.
40+ */
3441 static LocalVariableNameFactory forMethod (MethodMetadata methodMetadata ) {
3542 return of (methodMetadata .getMethodArguments ().keySet ());
3643 }
3744
38- static LocalVariableNameFactory empty () {
39- return of (Set .of ());
40- }
41-
42- static LocalVariableNameFactory of (Set <String > variables ) {
43- return new LocalVariableNameFactory (variables );
45+ /**
46+ * Create a new {@link LocalVariableNameFactory} with a predefined set of initial variable names.
47+ *
48+ * @param predefinedVariables variables already known to be used in the given context.
49+ * @return new instance of {@link LocalVariableNameFactory}.
50+ */
51+ static LocalVariableNameFactory of (Set <String > predefinedVariables ) {
52+ return new LocalVariableNameFactory (predefinedVariables );
4453 }
4554
4655 LocalVariableNameFactory (Iterable <String > predefinedVariableNames ) {
4756
4857 variables = new LinkedMultiValueMap <>();
49- for (String parameterName : predefinedVariableNames ) {
50- variables .add (parameterName , parameterName );
51- }
58+ predefinedVariableNames .forEach (varName -> variables .add (varName , varName ));
5259 }
5360
5461 @ Override
55- public String generateName (String suggestedName ) {
62+ public String generateName (String intendedVariableName ) {
5663
57- if (!variables .containsKey (suggestedName )) {
58- variables .add (suggestedName , suggestedName );
59- return suggestedName ;
64+ if (!variables .containsKey (intendedVariableName )) {
65+ variables .add (intendedVariableName , intendedVariableName );
66+ return intendedVariableName ;
6067 }
6168
62- String targetName = suggestTargetName (suggestedName );
63- variables .add (suggestedName , targetName );
69+ String targetName = suggestTargetName (intendedVariableName );
70+ variables .add (intendedVariableName , targetName );
6471 variables .add (targetName , targetName );
6572 return targetName ;
6673 }
0 commit comments