View Javadoc
1   /*
2    *    Copyright 2016-2025 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.dynamic.sql.util;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Optional;
24  import java.util.stream.Collector;
25  
26  public class FragmentCollector {
27      final List<String> fragments = new ArrayList<>();
28      final Map<String, Object> parameters = new HashMap<>();
29  
30      public FragmentCollector() {
31          super();
32      }
33  
34      private FragmentCollector(FragmentAndParameters initialFragment) {
35          add(initialFragment);
36      }
37  
38      public void add(FragmentAndParameters fragmentAndParameters) {
39          fragments.add(fragmentAndParameters.fragment());
40          parameters.putAll(fragmentAndParameters.parameters());
41      }
42  
43      public FragmentCollector merge(FragmentCollector other) {
44          fragments.addAll(other.fragments);
45          parameters.putAll(other.parameters);
46          return this;
47      }
48  
49      public Optional<String> firstFragment() {
50          return fragments.stream().findFirst();
51      }
52  
53      public String collectFragments(Collector<CharSequence, ?, String> fragmentCollector) {
54          return fragments.stream().collect(fragmentCollector);
55      }
56  
57      public FragmentAndParameters toFragmentAndParameters(Collector<CharSequence, ?, String> fragmentCollector) {
58          return FragmentAndParameters.withFragment(collectFragments(fragmentCollector))
59                  .withParameters(parameters())
60                  .build();
61      }
62  
63      public Map<String, Object> parameters() {
64          return Collections.unmodifiableMap(parameters);
65      }
66  
67      public boolean hasMultipleFragments() {
68          return fragments.size() > 1;
69      }
70  
71      public boolean isEmpty() {
72          return fragments.isEmpty();
73      }
74  
75      public static Collector<FragmentAndParameters, FragmentCollector, FragmentCollector> collect() {
76          return Collector.of(FragmentCollector::new,
77                  FragmentCollector::add,
78                  FragmentCollector::merge);
79      }
80  
81      public static Collector<FragmentAndParameters, FragmentCollector, FragmentCollector> collect(
82              FragmentAndParameters initialFragment) {
83          return Collector.of(() -> new FragmentCollector(initialFragment),
84                  FragmentCollector::add,
85                  FragmentCollector::merge);
86      }
87  }