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.insert.render;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  import java.util.Objects;
21  import java.util.Optional;
22  
23  import org.jspecify.annotations.Nullable;
24  
25  public class FieldAndValueAndParameters {
26      private final String fieldName;
27      private final String valuePhrase;
28      private final Map<String, Object> parameters;
29  
30      private FieldAndValueAndParameters(Builder builder) {
31          fieldName = Objects.requireNonNull(builder.fieldName);
32          valuePhrase = Objects.requireNonNull(builder.valuePhrase);
33          parameters = builder.parameters;
34      }
35  
36      public String fieldName() {
37          return fieldName;
38      }
39  
40      public String valuePhrase() {
41          return valuePhrase;
42      }
43  
44      public Map<String, Object> parameters() {
45          return parameters;
46      }
47  
48      public static Builder withFieldName(String fieldName) {
49          return new Builder().withFieldName(fieldName);
50      }
51  
52      public static class Builder {
53          private @Nullable String fieldName;
54          private @Nullable String valuePhrase;
55          private final Map<String, Object> parameters = new HashMap<>();
56  
57          public Builder withFieldName(String fieldName) {
58              this.fieldName = fieldName;
59              return this;
60          }
61  
62          public Builder withValuePhrase(String valuePhrase) {
63              this.valuePhrase = valuePhrase;
64              return this;
65          }
66  
67          public Builder withParameter(String key, @Nullable Object value) {
68              // the value can be null because a parameter type converter may return null
69  
70              //noinspection DataFlowIssue
71              parameters.put(key, value);
72              return this;
73          }
74  
75          public FieldAndValueAndParameters build() {
76              return new FieldAndValueAndParameters(this);
77          }
78  
79          public Optional<FieldAndValueAndParameters> buildOptional() {
80              return Optional.of(build());
81          }
82      }
83  }