View Javadoc
1   /*
2    *    Copyright 2016-2024 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.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       * Return a new instance with the same parameters and a transformed fragment.
44       *
45       * @param mapper a function that can change the value of the fragment
46       * @return a new instance with the same parameters and a transformed fragment
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  }