21
21
import org .springframework .util .MultiValueMap ;
22
22
23
23
/**
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.
26
26
*
27
27
* @author Christoph Strobl
28
28
* @since 4.0
@@ -31,36 +31,43 @@ class LocalVariableNameFactory implements VariableNameFactory {
31
31
32
32
private final MultiValueMap <String , String > variables ;
33
33
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
+ */
34
41
static LocalVariableNameFactory forMethod (MethodMetadata methodMetadata ) {
35
42
return of (methodMetadata .getMethodArguments ().keySet ());
36
43
}
37
44
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 );
44
53
}
45
54
46
55
LocalVariableNameFactory (Iterable <String > predefinedVariableNames ) {
47
56
48
57
variables = new LinkedMultiValueMap <>();
49
- for (String parameterName : predefinedVariableNames ) {
50
- variables .add (parameterName , parameterName );
51
- }
58
+ predefinedVariableNames .forEach (varName -> variables .add (varName , varName ));
52
59
}
53
60
54
61
@ Override
55
- public String generateName (String suggestedName ) {
62
+ public String generateName (String intendedVariableName ) {
56
63
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 ;
60
67
}
61
68
62
- String targetName = suggestTargetName (suggestedName );
63
- variables .add (suggestedName , targetName );
69
+ String targetName = suggestTargetName (intendedVariableName );
70
+ variables .add (intendedVariableName , targetName );
64
71
variables .add (targetName , targetName );
65
72
return targetName ;
66
73
}
0 commit comments