Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions sdk/java-sdk/.idea/gradle.xml

This file was deleted.

10 changes: 0 additions & 10 deletions sdk/java-sdk/.idea/misc.xml

This file was deleted.

7 changes: 0 additions & 7 deletions sdk/java-sdk/.idea/vcs.xml

This file was deleted.

185 changes: 0 additions & 185 deletions sdk/java-sdk/.idea/workspace.xml

This file was deleted.

7 changes: 0 additions & 7 deletions sdk/java-sdk/src/main/java/org/lightswitch/Main.java

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.lightswitch.sdk.client;

import org.lightswitch.sdk.user.LightSwitchUser;

public interface LightSwitchClient {

boolean getBooleanFlag(String key, boolean defaultValue, LightSwitchUser user);

int getIntFlag(String key, int defaultValue, LightSwitchUser user);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about why the flag restricts the number type to Int. It seems like there could be many cases where storing a Long would be necessary.


String getStringFlag(String key, String defaultValue, LightSwitchUser user);

void destroy();

boolean isEnabled(String key, LightSwitchUser user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.lightswitch.sdk.client;

import org.lightswitch.sdk.exception.LightSwitchCastException;
import org.lightswitch.sdk.model.FeatureFlag;
import org.lightswitch.sdk.user.LightSwitchUser;

import java.util.HashMap;
import java.util.Map;

public class LightSwitchClientImpl implements LightSwitchClient {

// TODO develop FeatureFlagRepository, httpClient, SSEClient
private final Map<String, FeatureFlag> featureFlags; // Mock

public LightSwitchClientImpl() {
// Mock data
this.featureFlags = new HashMap<>();

featureFlags.put("new-dashboard", new FeatureFlag(
"new-dashboard",
false, // default
Map.of(
Map.of("region", "US"), true // active "US" user
)
));

featureFlags.put("beta-feature", new FeatureFlag(
"beta-feature",
false,
Map.of(
Map.of("plan", "premium"), true // active "premium" user
)
));

featureFlags.put("discount", new FeatureFlag(
"discount",
0, // default sale rates : 0%
Map.of(
Map.of("region", "US"), 100, // US user 100% sale
Map.of("region", "EU"), 50 // EU user 50% sale
)
));
}

private FeatureFlag getMockFlag(String key) {
return featureFlags.get(key);
}

private <T> T castValue(String key, Object value, Class<T> expectedType) {
if (!expectedType.isInstance(value)) {
throw new LightSwitchCastException(key, value, expectedType);
}
return expectedType.cast(value);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about move all private methods to the bottom?


@Override
public boolean getBooleanFlag(String key, boolean defaultValue, LightSwitchUser user) {
FeatureFlag flag = getMockFlag(key);
if (flag == null) return defaultValue;

Object value = flag.evaluate(user);
return castValue(key, value, Boolean.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since 58~62 lines are duplicated to other methods too, it would be better to extract private method for these.

}

@Override
public int getIntFlag(String key, int defaultValue, LightSwitchUser user) {
FeatureFlag flag = getMockFlag(key);
if (flag == null) return defaultValue;

Object value = flag.evaluate(user);
return castValue(key, value, Integer.class);
}

@Override
public String getStringFlag(String key, String defaultValue, LightSwitchUser user) {
FeatureFlag flag = getMockFlag(key);
if (flag == null) return defaultValue;

Object value = flag.evaluate(user);
return castValue(key, value, String.class);
}

@Override
public void destroy() {
featureFlags.clear();
}

@Override
public boolean isEnabled(String key, LightSwitchUser user) {
return getBooleanFlag(key, false, user);
}
}
Empty file.
Loading
Loading