14
14
import java .util .Map ;
15
15
import java .util .Objects ;
16
16
17
- public class SwitchContent implements CodeEmitter {
17
+ public class SwitchBody implements CodeEmitter {
18
18
19
19
private List <SwitchCases > cases = new ArrayList <>();
20
20
private @ Nullable SwitchCases defaultCase ;
21
21
22
- private SwitchContent (Collection <SwitchCases > branches ) {
22
+ private SwitchBody (Collection <SwitchCases > branches ) {
23
23
this .then (branches );
24
24
}
25
25
26
26
@ Contract (value = "_ -> new" , pure = true )
27
- public static SwitchContent of (SwitchCases ... branches ) {
27
+ public static SwitchBody of (SwitchCases ... branches ) {
28
28
return of (Arrays .asList (branches ));
29
29
}
30
30
31
31
@ Contract (value = "_ -> new" , pure = true )
32
- public static SwitchContent of (Collection <SwitchCases > branches ) {
33
- return new SwitchContent (branches );
32
+ public static SwitchBody of (Collection <SwitchCases > branches ) {
33
+ return new SwitchBody (branches );
34
34
}
35
35
36
36
@ Contract (value = "_ -> this" , mutates = "this" )
37
- public SwitchContent then (SwitchCases ... branches ) {
37
+ public SwitchBody then (SwitchCases ... branches ) {
38
38
return then (Arrays .asList (branches ));
39
39
}
40
40
41
41
@ Contract (value = "_ -> this" , mutates = "this" )
42
- public SwitchContent then (Collection <SwitchCases > branches ) {
42
+ public SwitchBody then (Collection <SwitchCases > branches ) {
43
43
Preconditions .checkArgument (branches .stream ().noneMatch (SwitchCases ::isDefault ), "These switch cases cannot contains default one!" );
44
44
this .cases .addAll (branches );
45
45
return this ;
46
46
}
47
47
48
48
@ Contract (value = "_ -> this" , mutates = "this" )
49
- public SwitchContent withDefault (SwitchCases defaultCase ) {
49
+ public SwitchBody withDefault (SwitchCases defaultCase ) {
50
50
Preconditions .checkArgument (defaultCase .isDefault (), "This switch case is not a default one!" );
51
51
this .defaultCase = defaultCase ;
52
52
return this ;
53
53
}
54
54
55
55
@ Contract (value = "_ -> this" , mutates = "this" )
56
- public SwitchContent withDefault (CodeBlock content ) {
57
- return withDefault (SwitchCases .ofDefault (content , false ));
56
+ public SwitchBody withDefault (CodeBlock body ) {
57
+ return withDefault (SwitchCases .ofDefault (body , false ));
58
58
}
59
59
60
60
public void mergeSimilarBranches (Comparator <String > keySort ) {
61
61
List <SwitchCases > cases = new ArrayList <>();
62
62
63
63
List <SwitchCases > nonDefaultCases = new ArrayList <>();
64
- Map <CodeBlock , SwitchCases .SwitchCasesChain > handleObjects = new HashMap <>();
65
- @ Nullable CodeBlock defaultValue = this .defaultCase == null ? null : this .defaultCase .content ();
64
+ Map <CodeBlock , SwitchCases .SwitchCasesChain > stats = new HashMap <>();
65
+ @ Nullable CodeBlock defaultValue = this .defaultCase == null ? null : this .defaultCase .body ();
66
66
67
67
// merge similar cases, and omit cases similar to the default
68
68
for (SwitchCases branch : this .cases ) {
69
- CodeBlock content = branch .content ();
69
+ CodeBlock body = branch .body ();
70
70
71
- if (defaultValue == null || !Objects .equals (branch .content (), defaultValue )) {
71
+ if (defaultValue == null || !Objects .equals (branch .body (), defaultValue )) {
72
72
nonDefaultCases .add (branch );
73
73
74
74
final SwitchCases .SwitchCasesChain newCases ;
75
- if (handleObjects .containsKey (content )) {
76
- newCases = handleObjects .get (content );
75
+ if (stats .containsKey (body )) {
76
+ newCases = stats .get (body );
77
77
} else {
78
78
newCases = SwitchCases .chain ();
79
- newCases .sortValues (keySort );
79
+ newCases .sortLabels (keySort );
80
80
}
81
- newCases .addAll (Objects .requireNonNull (branch .values ()));
81
+ newCases .addAll (Objects .requireNonNull (branch .labels ()));
82
82
83
- handleObjects .put (content , newCases );
83
+ stats .put (body , newCases );
84
84
}
85
85
}
86
86
@@ -98,13 +98,13 @@ public void mergeSimilarBranches(Comparator<String> keySort) {
98
98
}
99
99
100
100
// complete builders with collected infos
101
- for (Map .Entry <CodeBlock , SwitchCases .SwitchCasesChain > entry : handleObjects .entrySet ()) {
102
- SwitchCases .SwitchCasesChain branch = entry .getValue ();
101
+ for (Map .Entry <CodeBlock , SwitchCases .SwitchCasesChain > stat : stats .entrySet ()) {
102
+ SwitchCases .SwitchCasesChain branch = stat .getValue ();
103
103
104
104
if (inlined ) {
105
105
branch .inlined (arrow );
106
106
}
107
- cases .add (branch .enclosingContent ( entry .getKey ()).build ());
107
+ cases .add (branch .body ( stat .getKey ()).build ());
108
108
}
109
109
this .cases = cases ;
110
110
}
0 commit comments