Class ContextBuilder
LDContext
.
Use this type if you need to construct a context that has only a single kind. To define a
multi-kind context, use LDContext.createMulti(LDContext...)
or
LDContext.multiBuilder()
.
Obtain an instance of ContextBuilder by calling LDContext.builder(String)
or
LDContext.builder(ContextKind, String)
. Then, call setter methods such as
name(String)
or set(String, String)
to specify any additional attributes.
Then, call build()
to create the context. ContextBuilder setters return a reference
to the same builder, so calls can be
chained:
LDContext context = LDContext.builder("context-key-123abc")
.name("my-name)
.set("country", "us")
.build();
A ContextBuilder should not be accessed by multiple threads at once. Once you have called
build()
, the resulting LDContext is immutable and is safe to use from multiple
threads. Instances created with build()
are not affected by subsequent actions
taken on the builder.
-
Method Summary
Modifier and TypeMethodDescriptionanonymous
(boolean anonymous) Sets whether the context is only intended for flag evaluations and should not be indexed by LaunchDarkly.build()
Creates anLDContext
from the current builder properties.Sets the context's key attribute.kind
(ContextKind kind) Sets the context's kind attribute.Sets the context's kind attribute, as a string.Sets the context's name attribute.privateAttributes
(AttributeRef... attributeRefs) Equivalent toprivateAttributes(String...)
, but uses theAttributeRef
type.privateAttributes
(String... attributeRefs) Designates any number of context attributes, or properties within them, as private: that is, their values will not be recorded by LaunchDarkly.Same asset(String, LDValue)
for a boolean value.Same asset(String, LDValue)
for a double-precision numeric value.Same asset(String, LDValue)
for an integer numeric value.Sets the value of any attribute for the context.Same asset(String, LDValue)
for a string value.boolean
Same asset(String, LDValue)
, but returns a boolean indicating whether the attribute was successfully set.
-
Method Details
-
build
Creates anLDContext
from the current builder properties.The LDContext is immutable and will not be affected by any subsequent actions on the ContextBuilder.
It is possible to specify invalid attributes for a ContextBuilder, such as an empty key. Instead of throwing an exception, the ContextBuilder always returns an LDContext and you can check
LDContext.isValid()
orLDContext.getError()
to see if it has an error. SeeLDContext.isValid()
for more information about invalid conditions. If you pass an invalid LDContext to an SDK method, the SDK will detect this and will log a description of the error.- Returns:
- a new
LDContext
-
kind
Sets the context's kind attribute.Every LDContext has a kind. Setting it to an empty string or null is equivalent to
ContextKind.DEFAULT
("user"). This value is case-sensitive. For validation rules, seeContextKind
.- Parameters:
kind
- the context kind- Returns:
- the builder
- See Also:
-
kind
Sets the context's kind attribute, as a string.This method is a shortcut for calling
kind(ContextKind.of(kindName))
, since the method name already prevents ambiguity about the intended type- Parameters:
kindString
- the context kind- Returns:
- the builder
- See Also:
-
key
Sets the context's key attribute.Every Context has a key, which is always a string. It cannot be an empty string, but there are no other restrictions on its value.
The key attribute can be referenced by flag rules, flag target lists, and segments.
- Parameters:
key
- the context key- Returns:
- the builder
- See Also:
-
name
Sets the context's name attribute.This attribute is optional. It has the following special rules:
- Unlike most other attributes, it is always a string if it is specified.
- The LaunchDarkly dashboard treats this attribute as the preferred display name for contexts.
- Parameters:
name
- the name attribute (null to unset the attribute)- Returns:
- the builder
- See Also:
-
anonymous
Sets whether the context is only intended for flag evaluations and should not be indexed by LaunchDarkly.The default value is false. False means that this LDContext represents an entity such as a user that you want to be able to see on the LaunchDarkly dashboard.
Setting
anonymous
to true excludes this context from the database that is used by the dashboard. It does not exclude it from analytics event data, so it is not the same as making attributes private; all non-private attributes will still be included in events and data export. There is no limitation on what other attributes may be included (so, for instance,anonymous
does not mean there is noname
), and the context will still have whateverkey
you have given it.This value is also addressable in evaluations as the attribute name "anonymous". It is always treated as a boolean true or false in evaluations.
- Parameters:
anonymous
- true if the context should be excluded from the LaunchDarkly database- Returns:
- the builder
- See Also:
-
set
Sets the value of any attribute for the context.This includes only attributes that are addressable in evaluations-- not metadata such as
privateAttributes(String...)
. IfattributeName
is "privateAttributes", you will be setting an attribute with that name which you can use in evaluations or to record data for your own purposes, but it will be unrelated toprivateAttributes(String...)
.This method uses the
LDValue
type to represent a value of any JSON type: null, boolean, number, string, array, or object. For all attribute names that do not have special meaning to LaunchDarkly, you may use any of those types. Values of different JSON types are always treated as different values: for instance, null, false, and the empty string "" are not the the same, and the number 1 is not the same as the string "1".The following attribute names have special restrictions on their value types, and any value of an unsupported type will be ignored (leaving the attribute unchanged):
- "kind", "key": Must be a string. See
kind(ContextKind)
andkey(String)
. - "name": Must be a string or null. See
name(String)
. - "anonymous": Must be a boolean. See
anonymous(boolean)
.
The attribute name "_meta" is not allowed, because it has special meaning in the JSON schema for contexts; any attempt to set an attribute with this name has no effect. Also, any attempt to set an attribute with an empty or null name has no effect.
Values that are JSON arrays or objects have special behavior when referenced in flag/segment rules.
A value of
null
orLDValue.ofNull()
is equivalent to removing any current non-default value of the attribute. Null is not a valid attribute value in the LaunchDarkly model; any expressions in feature flags that reference an attribute with a null value will behave as if the attribute did not exist.- Parameters:
attributeName
- the attribute name to setvalue
- the value to set- Returns:
- the builder
- See Also:
- "kind", "key": Must be a string. See
-
set
Same asset(String, LDValue)
for a boolean value.- Parameters:
attributeName
- the attribute name to setvalue
- the value to set- Returns:
- the builder
- See Also:
-
set
Same asset(String, LDValue)
for an integer numeric value.- Parameters:
attributeName
- the attribute name to setvalue
- the value to set- Returns:
- the builder
- See Also:
-
set
Same asset(String, LDValue)
for a double-precision numeric value.- Parameters:
attributeName
- the attribute name to setvalue
- the value to set- Returns:
- the builder
- See Also:
-
set
Same asset(String, LDValue)
for a string value.- Parameters:
attributeName
- the attribute name to setvalue
- the value to set- Returns:
- the builder
- See Also:
-
trySet
Same asset(String, LDValue)
, but returns a boolean indicating whether the attribute was successfully set.- Parameters:
attributeName
- the attribute name to setvalue
- the value to set- Returns:
- true if successful; false if the name was invalid or the value was not an allowed type for that attribute
-
privateAttributes
Designates any number of context attributes, or properties within them, as private: that is, their values will not be recorded by LaunchDarkly.Each parameter can be either a simple attribute name (like "email"), or an attribute reference in the syntax described for
AttributeRef
(like "/address/street").- Parameters:
attributeRefs
- attribute references to mark as private- Returns:
- the builder
-
privateAttributes
Equivalent toprivateAttributes(String...)
, but uses theAttributeRef
type.Application code is unlikely to need to use the
AttributeRef
type directly; however, in cases where you are constructing LDContexts constructed repeatedly with the same set of private attributes, if you are also using complex private attribute path references such as "/address/street", converting this to an AttributeRef once and reusing it in manyprivateAttribute
calls is slightly more efficient than passing a string (since it does not need to parse the path repeatedly).- Parameters:
attributeRefs
- attribute references to mark as private- Returns:
- the builder
-