Skip to content

Commit

Permalink
Collect Fragments and Parameters as we go
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffgbutler committed Feb 10, 2025
1 parent 59599cf commit 716ed06
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
package org.mybatis.dynamic.sql.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collector;

public class FragmentCollector {
final List<FragmentAndParameters> fragments = new ArrayList<>();
final List<String> fragments = new ArrayList<>();
final Map<String, Object> parameters = new HashMap<>();

public FragmentCollector() {
super();
Expand All @@ -34,20 +36,22 @@ private FragmentCollector(FragmentAndParameters initialFragment) {
}

public void add(FragmentAndParameters fragmentAndParameters) {
fragments.add(fragmentAndParameters);
fragments.add(fragmentAndParameters.fragment());
parameters.putAll(fragmentAndParameters.parameters());
}

public FragmentCollector merge(FragmentCollector other) {
fragments.addAll(other.fragments);
parameters.putAll(other.parameters);
return this;
}

public Optional<String> firstFragment() {
return fragments.stream().findFirst().map(FragmentAndParameters::fragment);
return fragments.stream().findFirst();
}

public String collectFragments(Collector<CharSequence, ?, String> fragmentCollector) {
return fragments.stream().map(FragmentAndParameters::fragment).collect(fragmentCollector);
return fragments.stream().collect(fragmentCollector);
}

public FragmentAndParameters toFragmentAndParameters(Collector<CharSequence, ?, String> fragmentCollector) {
Expand All @@ -57,9 +61,7 @@ public FragmentAndParameters toFragmentAndParameters(Collector<CharSequence, ?,
}

public Map<String, Object> parameters() {
return fragments.stream()
.map(FragmentAndParameters::parameters)
.collect(HashMap::new, HashMap::putAll, HashMap::putAll);
return Collections.unmodifiableMap(parameters);
}

public boolean hasMultipleFragments() {
Expand Down

0 comments on commit 716ed06

Please sign in to comment.