Skip to content

Commit e466ffd

Browse files
author
chris-hoefgen
committed
formatting and code cleanup
1 parent de2be54 commit e466ffd

File tree

7 files changed

+93
-89
lines changed

7 files changed

+93
-89
lines changed

src/examples/java/com/devcycle/examples/CloudExample.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.devcycle.sdk.server.cloud.api.DevCycleCloudClient;
44
import com.devcycle.sdk.server.cloud.model.DevCycleCloudOptions;
5-
import com.devcycle.sdk.server.common.exception.DevCycleException;
65
import com.devcycle.sdk.server.common.model.DevCycleUser;
76

87
public class CloudExample {
@@ -34,7 +33,7 @@ public static void main(String[] args) throws InterruptedException {
3433
Boolean variableValue = false;
3534
try {
3635
variableValue = client.variableValue(user, VARIABLE_KEY, defaultValue);
37-
} catch(IllegalArgumentException e) {
36+
} catch (IllegalArgumentException e) {
3837
System.err.println("Error fetching variable value: " + e.getMessage());
3938
System.exit(1);
4039
}

src/examples/java/com/devcycle/examples/LocalExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.devcycle.examples;
22

3+
import com.devcycle.sdk.server.common.model.DevCycleUser;
34
import com.devcycle.sdk.server.local.api.DevCycleLocalClient;
45
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
5-
import com.devcycle.sdk.server.common.model.DevCycleUser;
66

77
public class LocalExample {
88
public static String VARIABLE_KEY = "test-boolean-variable";
@@ -29,7 +29,7 @@ public static void main(String[] args) throws InterruptedException {
2929
DevCycleLocalClient client = new DevCycleLocalClient(server_sdk_key, options);
3030

3131
for (int i = 0; i < 10; i++) {
32-
if(client.isInitialized()) {
32+
if (client.isInitialized()) {
3333
break;
3434
}
3535
Thread.sleep(500);

src/examples/java/com/devcycle/examples/OpenFeatureExample.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,37 @@ public static void main(String[] args) throws InterruptedException {
2121
System.exit(1);
2222
}
2323

24-
DevCycleLocalOptions options = DevCycleLocalOptions.builder().configPollingIntervalMs(60000)
24+
DevCycleLocalOptions options = DevCycleLocalOptions.builder().configPollingIntervalMS(60000)
2525
.disableAutomaticEventLogging(false).disableCustomEventLogging(false).build();
2626

2727
// Initialize DevCycle Client
2828
DevCycleLocalClient devCycleClient = new DevCycleLocalClient(server_sdk_key, options);
2929

30-
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
3130

3231
for (int i = 0; i < 10; i++) {
33-
if(devCycleClient.isInitialized()) {
32+
if (devCycleClient.isInitialized()) {
3433
break;
3534
}
3635
Thread.sleep(500);
3736
}
3837

39-
Map<String, Value> apiAttrs = new LinkedHashMap();
38+
// Setup OpenFeature with the DevCycle Provider
39+
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
40+
api.setProvider(new DevCycleProvider(devCycleClient));
41+
42+
// Create the evaluation context to use for fetching variable values
43+
Map<String, Value> apiAttrs = new LinkedHashMap<>();
4044
apiAttrs.put("email", new Value("test-user@domain.com"));
4145
apiAttrs.put("country", new Value("US"));
4246

43-
EvaluationContext ctx = new ImmutableContext("test-1234", apiAttrs);
47+
EvaluationContext context = new ImmutableContext("test-1234", apiAttrs);
4448

4549
// The default value can be of type string, boolean, number, or JSON
4650
Boolean defaultValue = false;
4751

48-
api.setProvider(new DevCycleProvider(devCycleClient));
49-
5052
// Fetch variable values using the identifier key, with a default value and user
5153
// object. The default value can be of type string, boolean, number, or JSON
52-
Boolean variableValue = api.getClient().getBooleanValue(VARIABLE_KEY, defaultValue, ctx);
54+
Boolean variableValue = api.getClient().getBooleanValue(VARIABLE_KEY, defaultValue, context);
5355

5456
// Use variable value
5557
if (variableValue) {
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
package com.devcycle.sdk.server.common.api;
22

3-
import com.devcycle.sdk.server.common.exception.DevCycleException;
43
import com.devcycle.sdk.server.common.model.DevCycleUser;
54
import com.devcycle.sdk.server.common.model.Variable;
65

6+
/**
7+
* Base interface for DevCycle clients that can be used to evaluate Features and retrieve variables values.
8+
*/
79
public interface IDevCycleClient {
8-
public boolean isInitialized();
10+
/**
11+
* @return true if the client is initialized and ready to be used. Clients should
12+
* return a default value if they are not initialized.
13+
*/
14+
boolean isInitialized();
915

10-
public <T> T variableValue(DevCycleUser user, String key, T defaultValue);
16+
/**
17+
* @param user (required) The user context for the evaluation.
18+
* @param key (required) The key of the feature variable to evaluate.
19+
* @param defaultValue (required) The default value to return if the feature variable is not found or the user
20+
* does not segment into the feature
21+
* @return the value of the variable for the given user, or the default value if the variable is not found.
22+
*/
23+
<T> T variableValue(DevCycleUser user, String key, T defaultValue);
1124

12-
public <T> Variable<T> variable(DevCycleUser user, String key, T defaultValue);
25+
/**
26+
* @param user (required) The user context for the evaluation.
27+
* @param key (required) The key of the feature variable to evaluate.
28+
* @param defaultValue (required) The default value to return if the feature variable is not found or the user
29+
* does not segment into the feature
30+
* @return the variable for the given user, or the default variable if the variable is not found.
31+
*/
32+
<T> Variable<T> variable(DevCycleUser user, String key, T defaultValue);
1333

14-
public void close();
34+
/**
35+
* Close the client and release any resources.
36+
*/
37+
void close();
1538
}

src/main/java/com/devcycle/sdk/server/openfeature/DevCycleProvider.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.devcycle.sdk.server.openfeature;
22

3-
import dev.openfeature.sdk.*;
43
import com.devcycle.sdk.server.common.api.IDevCycleClient;
5-
import com.devcycle.sdk.server.common.model.*;
4+
import com.devcycle.sdk.server.common.model.DevCycleUser;
5+
import com.devcycle.sdk.server.common.model.Variable;
6+
import dev.openfeature.sdk.*;
67
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
78

8-
public class DevCycleProvider implements FeatureProvider
9-
{
9+
public class DevCycleProvider implements FeatureProvider {
1010
private static final String PROVIDER_NAME = "DevCycleProvider";
1111

1212
private final IDevCycleClient devcycleClient;
@@ -51,13 +51,13 @@ public void shutdown() {
5151
}
5252

5353
<T> ProviderEvaluation<T> resolve(String key, T defaultValue, EvaluationContext ctx) {
54-
if(devcycleClient.isInitialized()) {
54+
if (devcycleClient.isInitialized()) {
5555
try {
56-
DevCycleUser user = UserFactory.createUser(ctx);
56+
DevCycleUser user = DevCycleUserFactory.createUser(ctx);
5757

5858
Variable<T> variable = devcycleClient.variable(user, key, defaultValue);
5959

60-
if(variable == null || variable.getIsDefaulted()) {
60+
if (variable == null || variable.getIsDefaulted()) {
6161
return ProviderEvaluation.<T>builder()
6262
.value(defaultValue)
6363
.reason(Reason.DEFAULT.toString())
@@ -76,8 +76,7 @@ <T> ProviderEvaluation<T> resolve(String key, T defaultValue, EvaluationContext
7676
.errorMessage(e.getMessage())
7777
.build();
7878
}
79-
}
80-
else {
79+
} else {
8180
throw new ProviderNotReadyError("DevCycle client not initialized");
8281
}
8382
}

src/main/java/com/devcycle/sdk/server/openfeature/UserFactory.java renamed to src/main/java/com/devcycle/sdk/server/openfeature/DevCycleUserFactory.java

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,100 +9,81 @@
99
import java.util.LinkedHashMap;
1010
import java.util.Map;
1111

12-
public class UserFactory {
12+
/**
13+
* Utility class for creating a DevCycleUser from an EvaluationContext
14+
*/
15+
class DevCycleUserFactory {
1316
static void setCustomValue(Map<String, Object> customData, String key, Value value) {
1417
// Only support boolean, number, and string types for custom data values
1518
// ignore all other data
16-
if(customData != null && key != null && value != null)
17-
{
18-
if(value.isBoolean()){
19+
if (customData != null && key != null && value != null) {
20+
if (value.isBoolean()) {
1921
customData.put(key, value.asBoolean());
20-
}
21-
else if(value.isNumber())
22-
{
22+
} else if (value.isNumber()) {
2323
customData.put(key, value.asDouble());
24-
}
25-
else if(value.isString())
26-
{
24+
} else if (value.isString()) {
2725
customData.put(key, value.asString());
2826
}
2927
}
3028
}
3129

32-
public static DevCycleUser createUser(EvaluationContext ctx) {
30+
static DevCycleUser createUser(EvaluationContext ctx) {
3331
String userId = "";
3432

3533
if (ctx != null && ctx.getTargetingKey() != null) {
3634
userId = ctx.getTargetingKey();
37-
}
38-
else if(ctx != null && ctx.getValue("user_id") != null) {
35+
} else if (ctx != null && ctx.getValue("user_id") != null) {
3936
userId = ctx.getValue("user_id").asString();
4037
}
4138

42-
if(userId == null || userId.isEmpty()) {
39+
if (userId == null || userId.isEmpty()) {
4340
throw new TargetingKeyMissingError();
4441
}
4542

4643
DevCycleUser user = DevCycleUser.builder().userId(userId).build();
4744

48-
Map<String,Object> customData = new LinkedHashMap();
49-
Map<String,Object> privateCustomData = new LinkedHashMap();
45+
Map<String, Object> customData = new LinkedHashMap<>();
46+
Map<String, Object> privateCustomData = new LinkedHashMap<>();
5047

51-
for(String key : ctx.keySet()) {
52-
if(key.equals("user_id")) {
48+
for (String key : ctx.keySet()) {
49+
if (key.equals("user_id")) {
5350
continue;
5451
}
5552

5653
Value value = ctx.getValue(key);
5754

58-
if(key == "email" && value.isString())
59-
{
55+
if (key.equals("email") && value.isString()) {
6056
user.setEmail(value.asString());
61-
}
62-
else if(key == "name" && value.isString())
63-
{
57+
} else if (key.equals("name") && value.isString()) {
6458
user.setName(value.asString());
65-
}
66-
else if(key == "language" && value.isString())
67-
{
59+
} else if (key.equals("language") && value.isString()) {
6860
user.setLanguage(value.asString());
69-
}
70-
else if(key == "country" && value.isString())
71-
{
61+
} else if (key.equals("country") && value.isString()) {
7262
user.setCountry(value.asString());
73-
}
74-
else if(key == "appVersion" && value.isString())
75-
{
63+
} else if (key.equals("appVersion") && value.isString()) {
7664
user.setAppVersion(value.asString());
77-
}
78-
else if(key == "appBuild" && value.isString())
79-
{
65+
} else if (key.equals("appBuild") && value.isString()) {
8066
user.setAppBuild(value.asString());
81-
}
82-
else if(key == "customData" && value.isStructure())
83-
{
67+
} else if (key.equals("customData") && value.isStructure()) {
8468
Structure customDataStructure = value.asStructure();
85-
for(String dataKey : customDataStructure.keySet()) {
69+
for (String dataKey : customDataStructure.keySet()) {
8670
setCustomValue(customData, dataKey, customDataStructure.getValue(dataKey));
8771
}
88-
}
89-
else if(key == "privateCustomData" && value.isStructure())
90-
{
72+
} else if (key.equals("privateCustomData") && value.isStructure()) {
9173
Structure privateDataStructure = value.asStructure();
92-
for(String dataKey : privateDataStructure.keySet()) {
74+
for (String dataKey : privateDataStructure.keySet()) {
9375
setCustomValue(privateCustomData, dataKey, privateDataStructure.getValue(dataKey));
9476
}
95-
}
96-
else {
77+
} else {
9778
setCustomValue(customData, key, value);
9879
}
9980
}
10081

101-
if(customData.size() > 0){
82+
if (!customData.isEmpty()) {
10283
user.setCustomData(customData);
10384
}
10485

105-
if(privateCustomData.size() > 0){
86+
if (!privateCustomData.isEmpty()) {
10687
user.setPrivateCustomData(privateCustomData);
10788
}
10889

0 commit comments

Comments
 (0)