Skip to content

Commit 145e42f

Browse files
committed
feat: idea for api design
Signed-off-by: Eric Deandrea <eric.deandrea@ibm.com>
1 parent 79f86aa commit 145e42f

File tree

3 files changed

+221
-0
lines changed

3 files changed

+221
-0
lines changed

api/src/main/java/ai/docling/api/DoclingApi.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public interface DoclingApi {
3333
*/
3434
<T extends DoclingApi, B extends DoclingApiBuilder<T, B>> DoclingApiBuilder<T, B> toBuilder();
3535

36+
/**
37+
* A builder interface for constructing implementations of {@link DoclingApi}. This interface
38+
* supports a fluent API for setting configuration properties before building an instance.
39+
*
40+
* @param <T> the type of the {@link DoclingApi} implementation being built.
41+
* @param <B> the type of the concrete builder implementation.
42+
*/
3643
interface DoclingApiBuilder<T extends DoclingApi, B extends DoclingApiBuilder<T, B>> {
3744
/**
3845
* Builds and returns an instance of the specified type, representing the completed configuration

api/src/main/java/ai/docling/api/convert/response/DocumentResponse.java

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Map;
55
import java.util.Optional;
66

7+
import org.jspecify.annotations.Nullable;
8+
79
import com.fasterxml.jackson.annotation.JsonInclude;
810
import com.fasterxml.jackson.annotation.JsonProperty;
911
import tools.jackson.databind.annotation.JsonDeserialize;
@@ -12,21 +14,80 @@
1214
@JsonInclude(JsonInclude.Include.NON_ABSENT)
1315
@JsonDeserialize(builder = DocumentResponse.Builder.class)
1416
public interface DocumentResponse {
17+
/**
18+
* Retrieves the content of the doc tags, if available.
19+
*
20+
* @return the content of the doc tags, or null if not present
21+
*/
22+
@Nullable
1523
String doctagsContent();
24+
25+
/**
26+
* Retrieves the filename associated with the document.
27+
*
28+
* @return the filename of the document as a string
29+
*/
1630
String filename();
31+
32+
/**
33+
* Retrieves the HTML content associated with the document, if available.
34+
*
35+
* @return the HTML content as a string, or null if not present
36+
*/
37+
@Nullable
1738
String htmlContent();
39+
40+
/**
41+
* Retrieves the JSON content associated with the document.
42+
*
43+
* @return a map representing the JSON content, or an empty map if no JSON content is present
44+
*/
1845
Map<String, Object> jsonContent();
46+
47+
/**
48+
* Retrieves the Markdown content associated with the document, if available.
49+
*
50+
* @return the Markdown content as a string, or null if no Markdown content is present
51+
*/
52+
@Nullable
1953
String markdownContent();
54+
55+
/**
56+
* Retrieves the plain text content associated with the document, if available.
57+
*
58+
* @return the plain text content as a string, or null if no text content is present
59+
*/
60+
@Nullable
2061
String textContent();
2162

63+
/**
64+
* Creates a new {@code Builder} instance initialized with the current state of the {@code DocumentResponse}.
65+
*
66+
* @return a {@code Builder} instance populated with the values from this {@code DocumentResponse}
67+
*/
2268
default Builder toBuilder() {
2369
return new Builder(this);
2470
}
2571

72+
/**
73+
* Creates and returns a new instance of the {@code Builder} class, which can be used to
74+
* construct a {@code DocumentResponse} object in a step-by-step manner.
75+
*
76+
* @return a new {@code Builder} instance
77+
*/
2678
static Builder builder() {
2779
return new Builder();
2880
}
2981

82+
/**
83+
* Default implementation of the {@link DocumentResponse} interface.
84+
* This record represents the response containing document data in various formats.
85+
* It is an immutable data structure that consolidates information related to a document,
86+
* such as its filename, content in multiple formats, and metadata.
87+
*
88+
* Each instance ensures the provided JSON content is unmodifiable by copying
89+
* the input map if it is present, or initializing it to an empty map otherwise.
90+
*/
3091
record DefaultDocumentResponse(String doctagsContent,
3192
String filename,
3293
String htmlContent,
@@ -50,6 +111,26 @@ public DefaultDocumentResponse(Builder builder) {
50111
}
51112
}
52113

114+
/**
115+
* A builder class for constructing instances of {@code DocumentResponse}.
116+
*
117+
* This class provides a step-by-step approach to configure and create a
118+
* {@code DocumentResponse} object. Each method in this class sets a specific
119+
* property of the object being built. Once all the desired properties are set,
120+
* the {@code build} method is used to create the final {@code DocumentResponse}
121+
* instance.
122+
*
123+
* The builder supports customization of various document-related attributes,
124+
* including doc tags content, filename, HTML content, JSON content, Markdown
125+
* content, and plain text content.
126+
*
127+
* By default, the builder initializes attributes with an empty state or default
128+
* values. If a {@code DocumentResponse} instance is provided to the constructor,
129+
* the builder is pre-populated with the attributes from the given response.
130+
*
131+
* This class is intended for internal use and is protected to restrict its
132+
* accessibility outside the defining package or class hierarchy.
133+
*/
53134
@JsonPOJOBuilder(withPrefix = "")
54135
class Builder {
55136
protected String doctagsContent;
@@ -59,10 +140,30 @@ class Builder {
59140
protected String markdownContent;
60141
protected String textContent;
61142

143+
/**
144+
* Constructs a new {@code Builder} instance.
145+
*
146+
* This constructor initializes a builder with default or empty states for all
147+
* attributes. It is protected to restrict direct instantiation outside of the
148+
* defining package or class hierarchy.
149+
*
150+
* The {@code Builder} class is primarily used to facilitate the creation of
151+
* {@code DocumentResponse} objects through a step-by-step configuration process.
152+
*/
62153
protected Builder() {
63154

64155
}
65156

157+
/**
158+
* Constructs a new {@code Builder} instance using the provided {@code DocumentResponse}.
159+
*
160+
* This constructor initializes the builder's fields with the data from the given
161+
* {@code DocumentResponse} object. It allows for the creation of a {@code Builder}
162+
* instance pre-populated with the state of an existing {@code DocumentResponse}.
163+
*
164+
* @param documentResponse the {@code DocumentResponse} instance whose data will
165+
* populate the fields of this builder
166+
*/
66167
protected Builder(DocumentResponse documentResponse) {
67168
this.doctagsContent = documentResponse.doctagsContent();
68169
this.filename = documentResponse.filename();
@@ -72,42 +173,95 @@ protected Builder(DocumentResponse documentResponse) {
72173
this.textContent = documentResponse.textContent();
73174
}
74175

176+
/**
177+
* Sets the doctags content for the builder instance.
178+
*
179+
* @param doctagsContent the doctags content to be set
180+
* @return this Builder instance for method chaining
181+
*/
75182
@JsonProperty("doctags_content")
76183
public Builder doctagsContent(String doctagsContent) {
77184
this.doctagsContent = doctagsContent;
78185
return this;
79186
}
80187

188+
/**
189+
* Sets the filename for the builder instance.
190+
*
191+
* @param filename the filename to be set
192+
* @return this Builder instance for method chaining
193+
*/
81194
@JsonProperty("filename")
82195
public Builder filename(String filename) {
83196
this.filename = filename;
84197
return this;
85198
}
86199

200+
/**
201+
* Sets the HTML content for the builder instance.
202+
*
203+
* @param htmlContent the HTML content to be set
204+
* @return this Builder instance for method chaining
205+
*/
87206
@JsonProperty("html_content")
88207
public Builder htmlContent(String htmlContent) {
89208
this.htmlContent = htmlContent;
90209
return this;
91210
}
92211

212+
/**
213+
* Sets the JSON content for the builder instance.
214+
*
215+
* The JSON content is represented as a map of key-value pairs, where the keys
216+
* are {@code String} objects, and the values are {@code Object} instances.
217+
*
218+
* @param jsonContent the JSON content to be set, represented as a {@code Map<String, Object>}
219+
* @return this {@link Builder} instance for method chaining
220+
*/
93221
@JsonProperty("json_content")
94222
public Builder jsonContent(Map<String, Object> jsonContent) {
95223
this.jsonContent = jsonContent;
96224
return this;
97225
}
98226

227+
/**
228+
* Sets the Markdown content for this builder instance.
229+
*
230+
* The Markdown content represents the textual data formatted in Markdown syntax,
231+
* which can include headings, lists, links, and other Markdown elements.
232+
*
233+
* @param markdownContent the Markdown content to be set, represented as a {@code String}
234+
* @return this {@link Builder} instance for method chaining
235+
*/
99236
@JsonProperty("md_content")
100237
public Builder markdownContent(String markdownContent) {
101238
this.markdownContent = markdownContent;
102239
return this;
103240
}
104241

242+
/**
243+
* Sets the plain text content for this builder instance.
244+
*
245+
* The plain text content represents unformatted textual data that can be
246+
* used for display or processing purposes within the application.
247+
*
248+
* @param textContent the plain text content to be set, represented as a {@code String}
249+
* @return this {@link Builder} instance for method chaining
250+
*/
105251
@JsonProperty("text_content")
106252
public Builder textContent(String textContent) {
107253
this.textContent = textContent;
108254
return this;
109255
}
110256

257+
/**
258+
* Creates and returns a {@link DocumentResponse} instance based on the current state of this {@link Builder}.
259+
*
260+
* <p>The returned {@link DocumentResponse} will encapsulate the values configured in the builder,
261+
* and further modifications to the builder instance will not affect the created {@code DocumentResponse}.
262+
*
263+
* @return a new {@code DocumentResponse} instance constructed from the builder's state
264+
*/
111265
public DocumentResponse build() {
112266
return new DefaultDocumentResponse(this);
113267
}

client/src/main/java/ai/docling/client/DoclingClient.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,29 @@ public Builder toBuilder() {
7474
return new Builder(this);
7575
}
7676

77+
/**
78+
* Creates and returns a new instance of {@link Builder} for constructing a {@link DoclingClient}.
79+
* The builder allows customization of configuration such as base URL, HTTP client, and JSON mapper.
80+
*
81+
* @return a new {@link Builder} instance for creating a {@link DoclingClient}.
82+
*/
7783
public static Builder builder() {
7884
return new Builder();
7985
}
8086

87+
/**
88+
* A builder class for creating instances of {@link DoclingClient}. This builder supports a fluent
89+
* API for configuring properties such as the base URL, HTTP client, and JSON mapper.
90+
*
91+
* <p>The {@link Builder} provides customization options through the available methods and ensures
92+
* proper validation of inputs during configuration.
93+
*
94+
* <p>An instance of {@code Builder} can be obtained via {@link DoclingClient#builder()} or
95+
* {@link DoclingClient#toBuilder()}.
96+
*
97+
* <p>This class is an implementation of {@link DoclingApiBuilder}, allowing it to construct
98+
* {@code DoclingClient} instances as part of the API construction process.
99+
*/
81100
public static final class Builder implements DoclingApiBuilder<DoclingClient, Builder> {
82101
private URI baseUrl = DEFAULT_BASE_URL;
83102
private HttpClient.Builder httpClientBuilder = HttpClient.newBuilder();
@@ -92,26 +111,67 @@ private Builder(DoclingClient doclingClient) {
92111
this.jsonMapperBuilder = doclingClient.jsonMapper.rebuild();
93112
}
94113

114+
/**
115+
* Sets the base URL for the Docling API service.
116+
*
117+
* <p>The URL string will be parsed and converted to a {@link URI}.
118+
*
119+
* @param baseUrl the base URL as a string (must not be blank)
120+
* @return this {@link Builder} instance for method chaining
121+
* @throws IllegalArgumentException if baseUrl is null or blank
122+
*/
95123
public Builder baseUrl(String baseUrl) {
96124
this.baseUrl = URI.create(ensureNotBlank(baseUrl, "baseUrl"));
97125
return this;
98126
}
99127

128+
/**
129+
* Sets the base URL for the Docling API service.
130+
*
131+
* @param baseUrl the base URL as a {@link URI}
132+
* @return this {@link Builder} instance for method chaining
133+
*/
100134
public Builder baseUrl(URI baseUrl) {
101135
this.baseUrl = baseUrl;
102136
return this;
103137
}
104138

139+
/**
140+
* Sets the HTTP client builder to be used for creating the underlying HTTP client.
141+
*
142+
* <p>This allows customization of HTTP client properties such as timeouts,
143+
* proxy settings, SSL context, and other connection parameters.
144+
*
145+
* @param httpClientBuilder the {@link HttpClient.Builder} to use
146+
* @return this {@link Builder} instance for method chaining
147+
*/
105148
public Builder httpClientBuilder(HttpClient.Builder httpClientBuilder) {
106149
this.httpClientBuilder = httpClientBuilder;
107150
return this;
108151
}
109152

153+
/**
154+
* Sets the JSON mapper builder to be used for creating the JSON mapper.
155+
*
156+
* <p>This allows customization of JSON serialization and deserialization behavior,
157+
* such as configuring features, modules, or property naming strategies.
158+
*
159+
* @param jsonMapperBuilder the {@link JsonMapper.Builder} to use
160+
* @return this {@link Builder} instance for method chaining
161+
*/
110162
public Builder jsonParser(JsonMapper.Builder jsonMapperBuilder) {
111163
this.jsonMapperBuilder = jsonMapperBuilder;
112164
return this;
113165
}
114166

167+
/**
168+
* Builds and returns a new {@link DoclingClient} instance with the configured properties.
169+
*
170+
* <p>This method validates all required parameters and constructs the client.
171+
*
172+
* @return a new {@link DoclingClient} instance
173+
* @throws IllegalArgumentException if any required parameter is null
174+
*/
115175
@Override
116176
public DoclingClient build() {
117177
return new DoclingClient(this);

0 commit comments

Comments
 (0)