Skip to content

Commit 5244939

Browse files
chore: implement builder pattern for datamodel classes and make constructors package-private
Co-Authored-By: [email protected] <[email protected]>
1 parent 0c2b3b5 commit 5244939

File tree

6 files changed

+179
-17
lines changed

6 files changed

+179
-17
lines changed

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/LDAiClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
6060
// Verify the whole value is a JSON object
6161
if (!checkValueWithFailureLogging(value, LDValueType.OBJECT, logger,
6262
"Input to parseAiConfig must be a JSON object")) {
63-
return new AiConfig(enabled, null, null, null, null);
63+
return AiConfig.builder().enabled(enabled).build();
6464
}
6565

6666
// Convert the _meta JSON object into Meta
6767
LDValue valueMeta = value.get("_ldMeta");
6868
if (!checkValueWithFailureLogging(valueMeta, LDValueType.OBJECT, logger, "_ldMeta must be a JSON object")) {
6969
// Q: If we can't read _meta, enabled by spec would be defaulted to false. Does
7070
// it even matter the rest of the values?
71-
return new AiConfig(enabled, null, null, null, null);
71+
return AiConfig.builder().enabled(enabled).build();
7272
}
7373

7474
// The booleanValue will get false if that value is something that we are not expecting
@@ -80,9 +80,10 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
8080
"variationKey should be a string")) {
8181
String variationKey = valueMeta.get("variationKey").stringValue();
8282

83-
meta = new Meta(
84-
variationKey,
85-
Optional.of(valueMeta.get("version").intValue()));
83+
meta = Meta.builder()
84+
.variationKey(variationKey)
85+
.version(Optional.of(valueMeta.get("version").intValue()))
86+
.build();
8687
}
8788

8889
// Convert the optional model from an JSON object of with parameters and custom
@@ -119,7 +120,11 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
119120
}
120121
}
121122

122-
model = new Model(modelName, parameters, custom);
123+
model = Model.builder()
124+
.name(modelName)
125+
.parameters(parameters)
126+
.custom(custom)
127+
.build();
123128
}
124129
}
125130

@@ -147,11 +152,17 @@ protected AiConfig parseAiConfig(LDValue value, String key) {
147152
"provider name must be a String")) {
148153
String providerName = valueProvider.get("name").stringValue();
149154

150-
provider = new Provider(providerName);
155+
provider = Provider.builder().name(providerName).build();
151156
}
152157
}
153158

154-
return new AiConfig(enabled, meta, model, messages, provider);
159+
return AiConfig.builder()
160+
.enabled(enabled)
161+
.meta(meta)
162+
.model(model)
163+
.messages(messages)
164+
.provider(provider)
165+
.build();
155166
}
156167

157168
protected boolean checkValueWithFailureLogging(LDValue ldValue, LDValueType expectedType, LDLogger logger,

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/AiConfig.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class AiConfig {
1313

1414
private final Provider provider;
1515

16-
public AiConfig(boolean enabled, Meta meta, Model model, List<Message> messages, Provider provider) {
16+
AiConfig(boolean enabled, Meta meta, Model model, List<Message> messages, Provider provider) {
1717
this.enabled = enabled;
1818
this.meta = meta;
1919
this.model = model;
@@ -40,4 +40,47 @@ public Model getModel() {
4040
public Provider getProvider() {
4141
return provider;
4242
}
43+
44+
public static Builder builder() {
45+
return new Builder();
46+
}
47+
48+
public static final class Builder {
49+
private boolean enabled;
50+
private Meta meta;
51+
private Model model;
52+
private List<Message> messages;
53+
private Provider provider;
54+
55+
private Builder() {}
56+
57+
public Builder enabled(boolean enabled) {
58+
this.enabled = enabled;
59+
return this;
60+
}
61+
62+
public Builder meta(Meta meta) {
63+
this.meta = meta;
64+
return this;
65+
}
66+
67+
public Builder model(Model model) {
68+
this.model = model;
69+
return this;
70+
}
71+
72+
public Builder messages(List<Message> messages) {
73+
this.messages = messages;
74+
return this;
75+
}
76+
77+
public Builder provider(Provider provider) {
78+
this.provider = provider;
79+
return this;
80+
}
81+
82+
public AiConfig build() {
83+
return new AiConfig(enabled, meta, model, messages, provider);
84+
}
85+
}
4386
}

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Message.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ public LDValue fromType(Message message) {
1414

1515
@Override
1616
public Message toType(LDValue ldValue) {
17-
return new Message(ldValue.get("content").stringValue(), Role.getRole(ldValue.get("role").stringValue()));
17+
return Message.builder()
18+
.content(ldValue.get("content").stringValue())
19+
.role(Role.getRole(ldValue.get("role").stringValue()))
20+
.build();
1821
}
1922
}
2023

21-
private String content;
24+
private final String content;
2225

23-
private Role role;
26+
private final Role role;
2427

25-
public Message(String content, Role role) {
28+
Message(String content, Role role) {
2629
this.content = content;
2730
this.role = role;
2831
}
@@ -34,4 +37,29 @@ public String getContent() {
3437
public Role getRole() {
3538
return role;
3639
}
40+
41+
public static Builder builder() {
42+
return new Builder();
43+
}
44+
45+
public static final class Builder {
46+
private String content;
47+
private Role role;
48+
49+
private Builder() {}
50+
51+
public Builder content(String content) {
52+
this.content = content;
53+
return this;
54+
}
55+
56+
public Builder role(Role role) {
57+
this.role = role;
58+
return this;
59+
}
60+
61+
public Message build() {
62+
return new Message(content, role);
63+
}
64+
}
3765
}

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Meta.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public final class Meta {
2222
* @param variationKey the variation key
2323
* @param version the version
2424
*/
25-
public Meta(String variationKey, Optional<Integer> version) {
25+
Meta(String variationKey, Optional<Integer> version) {
2626
this.variationKey = variationKey;
2727
this.version = version != null ? version : Optional.empty();
2828
}
@@ -34,4 +34,34 @@ public String getVariationKey() {
3434
public Optional<Integer> getVersion() {
3535
return version;
3636
}
37+
38+
public static Builder builder() {
39+
return new Builder();
40+
}
41+
42+
public static final class Builder {
43+
private String variationKey;
44+
private Optional<Integer> version = Optional.empty();
45+
46+
private Builder() {}
47+
48+
public Builder variationKey(String variationKey) {
49+
this.variationKey = variationKey;
50+
return this;
51+
}
52+
53+
public Builder version(Optional<Integer> version) {
54+
this.version = version;
55+
return this;
56+
}
57+
58+
public Builder version(int version) {
59+
this.version = Optional.of(version);
60+
return this;
61+
}
62+
63+
public Meta build() {
64+
return new Meta(variationKey, version);
65+
}
66+
}
3767
}

lib/sdk/server-ai/src/main/java/com/launchdarkly/sdk/server/ai/datamodel/Model.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public final class Model {
2020
* @param parameters the parameters map
2121
* @param custom the custom map
2222
*/
23-
public Model(String name, Map<String, LDValue> parameters, Map<String, LDValue> custom) {
23+
Model(String name, Map<String, LDValue> parameters, Map<String, LDValue> custom) {
2424
this.name = name;
2525
this.parameters = parameters != null ? Collections.unmodifiableMap(new HashMap<>(parameters))
2626
: Collections.emptyMap();
@@ -38,4 +38,35 @@ public Map<String, LDValue> getParameters() {
3838
public Map<String, LDValue> getCustom() {
3939
return custom;
4040
}
41+
42+
public static Builder builder() {
43+
return new Builder();
44+
}
45+
46+
public static final class Builder {
47+
private String name;
48+
private Map<String, LDValue> parameters;
49+
private Map<String, LDValue> custom;
50+
51+
private Builder() {}
52+
53+
public Builder name(String name) {
54+
this.name = name;
55+
return this;
56+
}
57+
58+
public Builder parameters(Map<String, LDValue> parameters) {
59+
this.parameters = parameters;
60+
return this;
61+
}
62+
63+
public Builder custom(Map<String, LDValue> custom) {
64+
this.custom = custom;
65+
return this;
66+
}
67+
68+
public Model build() {
69+
return new Model(name, parameters, custom);
70+
}
71+
}
4172
}
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
package com.launchdarkly.sdk.server.ai.datamodel;
22

33
public final class Provider {
4-
private String name;
4+
private final String name;
55

6-
public Provider(String name) {
6+
Provider(String name) {
77
this.name = name;
88
}
99

1010
public String getName() {
1111
return name;
1212
}
13+
14+
public static Builder builder() {
15+
return new Builder();
16+
}
17+
18+
public static final class Builder {
19+
private String name;
20+
21+
private Builder() {}
22+
23+
public Builder name(String name) {
24+
this.name = name;
25+
return this;
26+
}
27+
28+
public Provider build() {
29+
return new Provider(name);
30+
}
31+
}
1332
}

0 commit comments

Comments
 (0)