1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.dynamic.sql.util;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.Optional;
22 import java.util.function.UnaryOperator;
23
24 public class FragmentAndParameters {
25
26 private final String fragment;
27 private final Map<String, Object> parameters;
28
29 private FragmentAndParameters(Builder builder) {
30 fragment = Objects.requireNonNull(builder.fragment);
31 parameters = Objects.requireNonNull(builder.parameters);
32 }
33
34 public String fragment() {
35 return fragment;
36 }
37
38 public Map<String, Object> parameters() {
39 return parameters;
40 }
41
42
43
44
45
46
47
48 public FragmentAndParameters mapFragment(UnaryOperator<String> mapper) {
49 return FragmentAndParameters.withFragment(mapper.apply(fragment))
50 .withParameters(parameters)
51 .build();
52 }
53
54 public static Builder withFragment(String fragment) {
55 return new Builder().withFragment(fragment);
56 }
57
58 public static FragmentAndParameters fromFragment(String fragment) {
59 return new Builder().withFragment(fragment).build();
60 }
61
62 public static class Builder {
63 private String fragment;
64 private final Map<String, Object> parameters = new HashMap<>();
65
66 public Builder withFragment(String fragment) {
67 this.fragment = fragment;
68 return this;
69 }
70
71 public Builder withParameter(String key, Object value) {
72 parameters.put(key, value);
73 return this;
74 }
75
76 public Builder withParameters(Map<String, Object> parameters) {
77 this.parameters.putAll(parameters);
78 return this;
79 }
80
81 public FragmentAndParameters build() {
82 return new FragmentAndParameters(this);
83 }
84
85 public Optional<FragmentAndParameters> buildOptional() {
86 return Optional.of(build());
87 }
88 }
89 }