diff --git a/src/main/java/com/launchdarkly/client/LDClient.java b/src/main/java/com/launchdarkly/client/LDClient.java index 48023822d..574fd16b3 100644 --- a/src/main/java/com/launchdarkly/client/LDClient.java +++ b/src/main/java/com/launchdarkly/client/LDClient.java @@ -149,13 +149,25 @@ private void sendFlagRequestEvent(String featureKey, LDUser user, boolean value) /** * Calculates the value of a feature flag for a given user. * - * * @param featureKey the unique featureKey for the feature flag * @param user the end user requesting the flag * @param defaultValue the default value of the flag * @return whether or not the flag should be enabled, or {@code defaultValue} if the flag is disabled in the LaunchDarkly control panel + * @deprecated As of version 0.7.0, renamed to {@link #toggle(String, LDUser, boolean)} */ public boolean getFlag(String featureKey, LDUser user, boolean defaultValue) { + return toggle(featureKey, user, defaultValue); + } + + /** + * Calculates the value of a feature flag for a given user. + * + * @param featureKey the unique featureKey for the feature flag + * @param user the end user requesting the flag + * @param defaultValue the default value of the flag + * @return whether or not the flag should be enabled, or {@code defaultValue} if the flag is disabled in the LaunchDarkly control panel + */ + public boolean toggle(String featureKey, LDUser user, boolean defaultValue) { if (this.offline) { return defaultValue; } diff --git a/src/main/java/com/launchdarkly/client/LDUser.java b/src/main/java/com/launchdarkly/client/LDUser.java index c63192585..915cba829 100644 --- a/src/main/java/com/launchdarkly/client/LDUser.java +++ b/src/main/java/com/launchdarkly/client/LDUser.java @@ -32,6 +32,7 @@ public class LDUser { private String avatar; private String firstName; private String lastName; + private boolean anonymous; private LDCountryCode country; private Map custom; @@ -52,6 +53,7 @@ protected LDUser(Builder builder) { this.email = builder.email; this.name = builder.name; this.avatar = builder.avatar; + this.anonymous = builder.anonymous; this.custom = new HashMap(builder.custom); } @@ -84,6 +86,8 @@ String getKey() { String getAvatar() { return avatar; } + boolean getAnonymous() { return anonymous; } + JsonElement getCustom(String key) { return custom.get(key); } @@ -110,6 +114,7 @@ public static class Builder { private String email; private String name; private String avatar; + private boolean anonymous; private LDCountryCode country; private Map custom; @@ -193,6 +198,16 @@ public Builder firstName(String firstName) { return this; } + /** + * Sets whether this user is anonymous + * @param anonymous whether the user is anonymous + * @return the builder + */ + public Builder anonymous(boolean anonymous) { + this.anonymous = anonymous; + return this; + } + /** * Sets the user's last name * @param lastName the user's last name diff --git a/src/main/java/com/launchdarkly/client/Variation.java b/src/main/java/com/launchdarkly/client/Variation.java index 0d53e4f9c..a79217035 100644 --- a/src/main/java/com/launchdarkly/client/Variation.java +++ b/src/main/java/com/launchdarkly/client/Variation.java @@ -58,7 +58,7 @@ static class Builder { Builder(E value, int weight) { this.value = value; this.weight = weight; - this.userTarget = new TargetRule("key", "in", new ArrayList()); + this.userTarget = new TargetRule("key", "in", new ArrayList()); targets = new ArrayList(); } @@ -81,22 +81,22 @@ Variation build() { static class TargetRule { String attribute; String operator; - List values; + List values; private final Logger logger = LoggerFactory.getLogger(TargetRule.class); - TargetRule(String attribute, String operator, List values) { + TargetRule(String attribute, String operator, List values) { this.attribute = attribute; this.operator = operator; - this.values = new ArrayList(values); + this.values = new ArrayList(values); } - TargetRule(String attribute, List values) { + TargetRule(String attribute, List values) { this(attribute, "in", values); } public boolean matchTarget(LDUser user) { - String uValue = null; + Object uValue = null; if (attribute.equals("key")) { if (user.getKey() != null) { uValue = user.getKey(); @@ -137,6 +137,9 @@ else if (attribute.equals("name")) { uValue = user.getName(); } } + else if (attribute.equals("anonymous")) { + uValue = user.getAnonymous(); + } else { // Custom attribute JsonElement custom = user.getCustom(attribute); diff --git a/src/test/java/com/launchdarkly/client/FeatureRepTest.java b/src/test/java/com/launchdarkly/client/FeatureRepTest.java index c0dfa847c..7628187fb 100644 --- a/src/test/java/com/launchdarkly/client/FeatureRepTest.java +++ b/src/test/java/com/launchdarkly/client/FeatureRepTest.java @@ -8,17 +8,20 @@ public class FeatureRepTest { - private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.singletonList("targetOn@test.com")); + private final Variation.TargetRule targetUserOn = new Variation.TargetRule("key", Collections.singletonList("targetOn@test.com")); - private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.asList("google", "microsoft")); + private final Variation.TargetRule targetGroupOn = new Variation.TargetRule("groups", Arrays.asList("google", "microsoft")); - private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.singletonList("targetOff@test.com")); + private final Variation.TargetRule targetUserOff = new Variation.TargetRule("key", Collections.singletonList("targetOff@test.com")); - private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.asList("oracle")); + private final Variation.TargetRule targetGroupOff = new Variation.TargetRule("groups", Arrays.asList("oracle")); + + private final Variation.TargetRule targetAnonymousOn = new Variation.TargetRule("anonymous", Collections.singletonList(true)); private final Variation trueVariation = new Variation.Builder(true, 80) .target(targetUserOn) .target(targetGroupOn) + .target(targetAnonymousOn) .build(); private final Variation falseVariation = new Variation.Builder(false, 20) @@ -121,4 +124,12 @@ public void testFlagWithCustomAttributeWorksWithLDUserDefaultCtor() { assertNotNull(b); } + @Test + public void testFlagWithAnonymousOn() { + LDUser user = new LDUser.Builder("targetOff@test.com").anonymous(true).build(); + + Boolean b = simpleFlag.evaluate(user); + assertEquals(true, b); + } + } diff --git a/src/test/java/com/launchdarkly/client/LDUserTest.java b/src/test/java/com/launchdarkly/client/LDUserTest.java index 83a541373..43a52975f 100644 --- a/src/test/java/com/launchdarkly/client/LDUserTest.java +++ b/src/test/java/com/launchdarkly/client/LDUserTest.java @@ -65,5 +65,4 @@ public void testLDUserJsonSerializationContainsCountryAsTwoDigitCode() { assert(deserialized.getCountry().equals(LDCountryCode.US)); } - }