|
2 | 2 |
|
3 | 3 | import java.util.HashMap; |
4 | 4 | import java.util.Map; |
| 5 | +import java.util.Optional; |
5 | 6 |
|
6 | 7 | import org.jspecify.annotations.Nullable; |
7 | 8 |
|
8 | 9 | import com.fasterxml.jackson.annotation.JsonInclude; |
9 | 10 | import com.fasterxml.jackson.annotation.JsonProperty; |
10 | 11 |
|
11 | | -@JsonInclude(JsonInclude.Include.NON_NULL) |
12 | | -public record DocumentResponse( |
| 12 | +import tools.jackson.databind.annotation.JsonDeserialize; |
| 13 | +import tools.jackson.databind.annotation.JsonPOJOBuilder; |
13 | 14 |
|
14 | | - @JsonProperty("doctags_content") @Nullable String doctagsContent, |
| 15 | +@JsonInclude(JsonInclude.Include.NON_ABSENT) |
| 16 | +@JsonDeserialize(builder = DocumentResponse.Builder.class) |
| 17 | +public interface DocumentResponse { |
| 18 | + /** |
| 19 | + * Retrieves the content of the doc tags, if available. |
| 20 | + * |
| 21 | + * @return the content of the doc tags, or null if not present |
| 22 | + */ |
| 23 | + @Nullable |
| 24 | + String doctagsContent(); |
15 | 25 |
|
16 | | - @JsonProperty("filename") String filename, |
| 26 | + /** |
| 27 | + * Retrieves the filename associated with the document. |
| 28 | + * |
| 29 | + * @return the filename of the document as a string |
| 30 | + */ |
| 31 | + String filename(); |
17 | 32 |
|
18 | | - @JsonProperty("html_content") @Nullable String htmlContent, |
| 33 | + /** |
| 34 | + * Retrieves the HTML content associated with the document, if available. |
| 35 | + * |
| 36 | + * @return the HTML content as a string, or null if not present |
| 37 | + */ |
| 38 | + @Nullable |
| 39 | + String htmlContent(); |
19 | 40 |
|
20 | | - @JsonProperty("json_content") @Nullable Map<String, Object> jsonContent, |
| 41 | + /** |
| 42 | + * Retrieves the JSON content associated with the document. |
| 43 | + * |
| 44 | + * @return a map representing the JSON content, or an empty map if no JSON content is present |
| 45 | + */ |
| 46 | + Map<String, Object> jsonContent(); |
21 | 47 |
|
22 | | - @JsonProperty("md_content") @Nullable String markdownContent, |
| 48 | + /** |
| 49 | + * Retrieves the Markdown content associated with the document, if available. |
| 50 | + * |
| 51 | + * @return the Markdown content as a string, or null if no Markdown content is present |
| 52 | + */ |
| 53 | + @Nullable |
| 54 | + String markdownContent(); |
23 | 55 |
|
24 | | - @JsonProperty("text_content") @Nullable String textContent |
| 56 | + /** |
| 57 | + * Retrieves the plain text content associated with the document, if available. |
| 58 | + * |
| 59 | + * @return the plain text content as a string, or null if no text content is present |
| 60 | + */ |
| 61 | + @Nullable |
| 62 | + String textContent(); |
25 | 63 |
|
26 | | -) { |
| 64 | + /** |
| 65 | + * Creates a new {@code Builder} instance initialized with the current state of the {@code DocumentResponse}. |
| 66 | + * |
| 67 | + * @return a {@code Builder} instance populated with the values from this {@code DocumentResponse} |
| 68 | + */ |
| 69 | + default Builder toBuilder() { |
| 70 | + return new Builder(this); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates and returns a new instance of the {@code Builder} class, which can be used to |
| 75 | + * construct a {@code DocumentResponse} object in a step-by-step manner. |
| 76 | + * |
| 77 | + * @return a new {@code Builder} instance |
| 78 | + */ |
| 79 | + static Builder builder() { |
| 80 | + return new Builder(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Default implementation of the {@link DocumentResponse} interface. |
| 85 | + * This record represents the response containing document data in various formats. |
| 86 | + * It is an immutable data structure that consolidates information related to a document, |
| 87 | + * such as its filename, content in multiple formats, and metadata. |
| 88 | + * |
| 89 | + * Each instance ensures the provided JSON content is unmodifiable by copying |
| 90 | + * the input map if it is present, or initializing it to an empty map otherwise. |
| 91 | + */ |
| 92 | + record DefaultDocumentResponse(String doctagsContent, |
| 93 | + String filename, |
| 94 | + String htmlContent, |
| 95 | + Map<String, Object> jsonContent, |
| 96 | + String markdownContent, |
| 97 | + String textContent) implements DocumentResponse { |
27 | 98 |
|
28 | | - public DocumentResponse { |
29 | | - if (jsonContent != null) { |
30 | | - jsonContent = new HashMap<>(jsonContent); |
| 99 | + public DefaultDocumentResponse { |
| 100 | + jsonContent = Optional.ofNullable(jsonContent) |
| 101 | + .map(Map::copyOf) |
| 102 | + .orElseGet(Map::of); |
| 103 | + } |
| 104 | + |
| 105 | + public DefaultDocumentResponse(Builder builder) { |
| 106 | + this(builder.doctagsContent, |
| 107 | + builder.filename, |
| 108 | + builder.htmlContent, |
| 109 | + builder.jsonContent, |
| 110 | + builder.markdownContent, |
| 111 | + builder.textContent); |
31 | 112 | } |
32 | 113 | } |
33 | 114 |
|
| 115 | + /** |
| 116 | + * A builder class for constructing instances of {@code DocumentResponse}. |
| 117 | + * |
| 118 | + * This class provides a step-by-step approach to configure and create a |
| 119 | + * {@code DocumentResponse} object. Each method in this class sets a specific |
| 120 | + * property of the object being built. Once all the desired properties are set, |
| 121 | + * the {@code build} method is used to create the final {@code DocumentResponse} |
| 122 | + * instance. |
| 123 | + * |
| 124 | + * The builder supports customization of various document-related attributes, |
| 125 | + * including doc tags content, filename, HTML content, JSON content, Markdown |
| 126 | + * content, and plain text content. |
| 127 | + * |
| 128 | + * By default, the builder initializes attributes with an empty state or default |
| 129 | + * values. If a {@code DocumentResponse} instance is provided to the constructor, |
| 130 | + * the builder is pre-populated with the attributes from the given response. |
| 131 | + * |
| 132 | + * This class is intended for internal use and is protected to restrict its |
| 133 | + * accessibility outside the defining package or class hierarchy. |
| 134 | + */ |
| 135 | + @JsonPOJOBuilder(withPrefix = "") |
| 136 | + class Builder { |
| 137 | + protected String doctagsContent; |
| 138 | + protected String filename; |
| 139 | + protected String htmlContent; |
| 140 | + protected Map<String, Object> jsonContent = new HashMap<>(); |
| 141 | + protected String markdownContent; |
| 142 | + protected String textContent; |
| 143 | + |
| 144 | + /** |
| 145 | + * Constructs a new {@code Builder} instance. |
| 146 | + * |
| 147 | + * This constructor initializes a builder with default or empty states for all |
| 148 | + * attributes. It is protected to restrict direct instantiation outside of the |
| 149 | + * defining package or class hierarchy. |
| 150 | + * |
| 151 | + * The {@code Builder} class is primarily used to facilitate the creation of |
| 152 | + * {@code DocumentResponse} objects through a step-by-step configuration process. |
| 153 | + */ |
| 154 | + protected Builder() { |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Constructs a new {@code Builder} instance using the provided {@code DocumentResponse}. |
| 160 | + * |
| 161 | + * This constructor initializes the builder's fields with the data from the given |
| 162 | + * {@code DocumentResponse} object. It allows for the creation of a {@code Builder} |
| 163 | + * instance pre-populated with the state of an existing {@code DocumentResponse}. |
| 164 | + * |
| 165 | + * @param documentResponse the {@code DocumentResponse} instance whose data will |
| 166 | + * populate the fields of this builder |
| 167 | + */ |
| 168 | + protected Builder(DocumentResponse documentResponse) { |
| 169 | + this.doctagsContent = documentResponse.doctagsContent(); |
| 170 | + this.filename = documentResponse.filename(); |
| 171 | + this.htmlContent = documentResponse.htmlContent(); |
| 172 | + this.jsonContent = documentResponse.jsonContent(); |
| 173 | + this.markdownContent = documentResponse.markdownContent(); |
| 174 | + this.textContent = documentResponse.textContent(); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Sets the doctags content for the builder instance. |
| 179 | + * |
| 180 | + * @param doctagsContent the doctags content to be set |
| 181 | + * @return this Builder instance for method chaining |
| 182 | + */ |
| 183 | + @JsonProperty("doctags_content") |
| 184 | + public Builder doctagsContent(String doctagsContent) { |
| 185 | + this.doctagsContent = doctagsContent; |
| 186 | + return this; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Sets the filename for the builder instance. |
| 191 | + * |
| 192 | + * @param filename the filename to be set |
| 193 | + * @return this Builder instance for method chaining |
| 194 | + */ |
| 195 | + @JsonProperty("filename") |
| 196 | + public Builder filename(String filename) { |
| 197 | + this.filename = filename; |
| 198 | + return this; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Sets the HTML content for the builder instance. |
| 203 | + * |
| 204 | + * @param htmlContent the HTML content to be set |
| 205 | + * @return this Builder instance for method chaining |
| 206 | + */ |
| 207 | + @JsonProperty("html_content") |
| 208 | + public Builder htmlContent(String htmlContent) { |
| 209 | + this.htmlContent = htmlContent; |
| 210 | + return this; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Sets the JSON content for the builder instance. |
| 215 | + * |
| 216 | + * The JSON content is represented as a map of key-value pairs, where the keys |
| 217 | + * are {@code String} objects, and the values are {@code Object} instances. |
| 218 | + * |
| 219 | + * @param jsonContent the JSON content to be set, represented as a {@code Map<String, Object>} |
| 220 | + * @return this {@link Builder} instance for method chaining |
| 221 | + */ |
| 222 | + @JsonProperty("json_content") |
| 223 | + public Builder jsonContent(Map<String, Object> jsonContent) { |
| 224 | + this.jsonContent = jsonContent; |
| 225 | + return this; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Sets the Markdown content for this builder instance. |
| 230 | + * |
| 231 | + * The Markdown content represents the textual data formatted in Markdown syntax, |
| 232 | + * which can include headings, lists, links, and other Markdown elements. |
| 233 | + * |
| 234 | + * @param markdownContent the Markdown content to be set, represented as a {@code String} |
| 235 | + * @return this {@link Builder} instance for method chaining |
| 236 | + */ |
| 237 | + @JsonProperty("md_content") |
| 238 | + public Builder markdownContent(String markdownContent) { |
| 239 | + this.markdownContent = markdownContent; |
| 240 | + return this; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Sets the plain text content for this builder instance. |
| 245 | + * |
| 246 | + * The plain text content represents unformatted textual data that can be |
| 247 | + * used for display or processing purposes within the application. |
| 248 | + * |
| 249 | + * @param textContent the plain text content to be set, represented as a {@code String} |
| 250 | + * @return this {@link Builder} instance for method chaining |
| 251 | + */ |
| 252 | + @JsonProperty("text_content") |
| 253 | + public Builder textContent(String textContent) { |
| 254 | + this.textContent = textContent; |
| 255 | + return this; |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * Creates and returns a {@link DocumentResponse} instance based on the current state of this {@link Builder}. |
| 260 | + * |
| 261 | + * <p>The returned {@link DocumentResponse} will encapsulate the values configured in the builder, |
| 262 | + * and further modifications to the builder instance will not affect the created {@code DocumentResponse}. |
| 263 | + * |
| 264 | + * @return a new {@code DocumentResponse} instance constructed from the builder's state |
| 265 | + */ |
| 266 | + public DocumentResponse build() { |
| 267 | + return new DefaultDocumentResponse(this); |
| 268 | + } |
| 269 | + } |
34 | 270 | } |
0 commit comments