16
16
package org .mybatis .dynamic .sql .util ;
17
17
18
18
import java .util .ArrayList ;
19
+ import java .util .Collections ;
19
20
import java .util .HashMap ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .Optional ;
23
24
import java .util .stream .Collector ;
24
25
25
26
public class FragmentCollector {
26
- final List <FragmentAndParameters > fragments = new ArrayList <>();
27
+ final List <String > fragments = new ArrayList <>();
28
+ final Map <String , Object > parameters = new HashMap <>();
27
29
28
30
public FragmentCollector () {
29
31
super ();
@@ -34,20 +36,22 @@ private FragmentCollector(FragmentAndParameters initialFragment) {
34
36
}
35
37
36
38
public void add (FragmentAndParameters fragmentAndParameters ) {
37
- fragments .add (fragmentAndParameters );
39
+ fragments .add (fragmentAndParameters .fragment ());
40
+ parameters .putAll (fragmentAndParameters .parameters ());
38
41
}
39
42
40
43
public FragmentCollector merge (FragmentCollector other ) {
41
44
fragments .addAll (other .fragments );
45
+ parameters .putAll (other .parameters );
42
46
return this ;
43
47
}
44
48
45
49
public Optional <String > firstFragment () {
46
- return fragments .stream ().findFirst (). map ( FragmentAndParameters :: fragment ) ;
50
+ return fragments .stream ().findFirst ();
47
51
}
48
52
49
53
public String collectFragments (Collector <CharSequence , ?, String > fragmentCollector ) {
50
- return fragments .stream ().map ( FragmentAndParameters :: fragment ). collect (fragmentCollector );
54
+ return fragments .stream ().collect (fragmentCollector );
51
55
}
52
56
53
57
public FragmentAndParameters toFragmentAndParameters (Collector <CharSequence , ?, String > fragmentCollector ) {
@@ -57,9 +61,7 @@ public FragmentAndParameters toFragmentAndParameters(Collector<CharSequence, ?,
57
61
}
58
62
59
63
public Map <String , Object > parameters () {
60
- return fragments .stream ()
61
- .map (FragmentAndParameters ::parameters )
62
- .collect (HashMap ::new , HashMap ::putAll , HashMap ::putAll );
64
+ return Collections .unmodifiableMap (parameters );
63
65
}
64
66
65
67
public boolean hasMultipleFragments () {
0 commit comments