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.Collections;
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.Objects;
22  import java.util.Optional;
23  import java.util.function.UnaryOperator;
24  
25  import org.jspecify.annotations.Nullable;
26  
27  public class FragmentAndParameters {
28  
29      private final String fragment;
30      private final Map<String, Object> parameters;
31  
32      private FragmentAndParameters(Builder builder) {
33          fragment = Objects.requireNonNull(builder.fragment);
34          parameters = Collections.unmodifiableMap(builder.parameters);
35      }
36  
37      public String fragment() {
38          return fragment;
39      }
40  
41      public Map<String, Object> parameters() {
42          return parameters;
43      }
44  
45      /**
46       * Return a new instance with the same parameters and a transformed fragment.
47       *
48       * @param mapper a function that can change the value of the fragment
49       * @return a new instance with the same parameters and a transformed fragment
50       */
51      public FragmentAndParameters mapFragment(UnaryOperator<String> mapper) {
52          return withFragment(mapper.apply(fragment))
53                  .withParameters(parameters)
54                  .build();
55      }
56  
57      public static Builder withFragment(String fragment) {
58          return new Builder().withFragment(fragment);
59      }
60  
61      public static FragmentAndParameters fromFragment(String fragment) {
62          return new Builder().withFragment(fragment).build();
63      }
64  
65      public static class Builder {
66          private @Nullable String fragment;
67          private final Map<String, Object> parameters = new HashMap<>();
68  
69          public Builder withFragment(String fragment) {
70              this.fragment = fragment;
71              return this;
72          }
73  
74          public Builder withParameter(String key, @Nullable Object value) {
75              // the value can be null because a parameter type converter may return null
76  
77              //noinspection DataFlowIssue
78              parameters.put(key, value);
79              return this;
80          }
81  
82          public Builder withParameters(Map<String, Object> parameters) {
83              this.parameters.putAll(parameters);
84              return this;
85          }
86  
87          public FragmentAndParameters build() {
88              return new FragmentAndParameters(this);
89          }
90  
91          public Optional<FragmentAndParameters> buildOptional() {
92              return Optional.of(build());
93          }
94      }
95  }