Class LDContext
- All Implemented Interfaces:
JsonSerializable
LDContext is the newer replacement for the previous, less flexible LDUser type.
The current SDK still supports LDUser, but LDContext is now the preferred model and may
entirely replace LDUser in the future.
To create an LDContext of a single kind, such as a user, you may use create(String)
or create(ContextKind, String) when only the key matters; or, to specify other
attributes, use builder(String).
To create an LDContext with multiple kinds, use createMulti(LDContext...) or
multiBuilder().
An LDContext can be in an error state if it was built with invalid attributes. See
isValid() and getError().
LaunchDarkly defines a standard JSON encoding for contexts, used by the JavaScript SDK
and also in analytics events. LDContext can be converted to and from JSON in any of
these ways:
- With
JsonSerialization. - With Gson, if and only if you configure your
Gsoninstance withLDGson. - With Jackson, if and only if you configure your
ObjectMapperinstance withLDJackson.
To learn more about contexts, read the documentation.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ContextBuilderbuilder(ContextKind kind, String key) static ContextBuilderCreates aContextBuilderfor building an LDContext, initializing itskeyand settingkindtoContextKind.DEFAULT.static ContextBuilderbuilderFromContext(LDContext context) Creates a builder whose properties are the same as an existing single-kind LDContext.static LDContextcreate(ContextKind kind, String key) Creates a single-kind LDContext with only the kind and keys specified.static LDContextCreates a single-kind LDContext with a kind ofContextKind.DEFAULT} and the specified key.static LDContextcreateMulti(LDContext... contexts) Creates a multi-kind LDContext out of the specified single-kind LDContexts.booleanstatic LDContextDeprecated.Returns the names of all non-built-in attributes that have been set in this context.getError()Returns null for a valid LDContext, or an error message for an invalid one.Returns a string that describes the LDContext uniquely based onkindandkeyvalues.getIndividualContext(int index) Returns the single-kind LDContext corresponding to one of the kinds in this context.Returns the single-kind LDContext corresponding to one of the kinds in this context.getIndividualContext(String kind) Same asgetIndividualContext(ContextKind), but specifies the kind as a plain string.intReturns the number of context kinds in this context.getKey()Returns the context'skeyattribute.getKind()Returns the context'skindattribute.getName()Returns the context'snameattribute.getPrivateAttribute(int index) Retrieves one of the private attribute references that were specified for this context.intReturns the number of private attribute references that were specified for this context.getValue(AttributeRef attributeRef) Looks up the value of any attribute of the context, or a value contained within an attribute, based on anAttributeRef.Looks up the value of any attribute of the context by name.inthashCode()booleanReturns true if this context is only intended for flag evaluations and will not be indexed by LaunchDarkly.booleanReturns true if this is a multi-kind context.booleanisValid()Returnstruefor a valid LDContext,falsefor an invalid one.static ContextMultiBuilderCreates aContextMultiBuilderfor building a multi-kind context.toString()Returns a string representation of the context.
-
Method Details
-
create
Creates a single-kind LDContext with a kind ofContextKind.DEFAULT} and the specified key.To specify additional properties, use
builder(String). To create a multi-kind LDContext, usecreateMulti(LDContext...)ormultiBuilder(). To create a single-kind LDContext of a different kind than "user", usecreate(ContextKind, String).- Parameters:
key- the context key- Returns:
- an LDContext
- See Also:
-
create
Creates a single-kind LDContext with only the kind and keys specified.To specify additional properties, use
builder(ContextKind, String). To create a multi-kind LDContext, usecreateMulti(LDContext...)ormultiBuilder().- Parameters:
kind- the context kind; if null,ContextKind.DEFAULTwill be usedkey- the context key- Returns:
- an LDContext
- See Also:
-
createMulti
Creates a multi-kind LDContext out of the specified single-kind LDContexts.To create a single-kind Context, use
create(String),create(ContextKind, String), orbuilder(String).For the returned LDContext to be valid, the contexts list must not be empty, and all of its elements must be valid LDContexts. Otherwise, the returned LDContext will be invalid as reported by
getError().If only one context parameter is given, the method returns that same context.
If the nested context is multi-kind, this is exactly equivalent to adding each of the individual kinds from it separately. For instance, in the following example, "multi1" and "multi2" end up being exactly the same:
LDContext c1 = LDContext.create(ContextKind.of("kind1"), "key1"); LDContext c2 = LDContext.create(ContextKind.of("kind2"), "key2"); LDContext c3 = LDContext.create(ContextKind.of("kind3"), "key3"); LDContext multi1 = LDContext.createMulti(c1, c2, c3); LDContext c1plus2 = LDContext.createMulti(c1, c2); LDContext multi2 = LDContext.createMulti(c1plus2, c3);- Parameters:
contexts- a list of contexts- Returns:
- an LDContext
- See Also:
-
fromUser
Deprecated.useLDContextdirectly instead.Converts a user to an equivalentLDContextinstance.This method is used by the SDK whenever an application passes a
LDUserinstance to methods such asidentify. The SDK operates internally on theLDContextmodel, which is more flexible than the older LDUser model: an L User can always be converted to an LDContext, but not vice versa. TheContextKindof the resulting Context isContextKind.DEFAULT("user").Because there is some overhead to this conversion, it is more efficient for applications to construct an LDContext and pass that to the SDK, rather than an LDUser. This is also recommended because the LDUser type may be removed in a future version of the SDK.
If the
userparameter is null, or if the user has a null key, the method returns an LDContext in an invalid state (seeisValid()).- Parameters:
user- an LDUser object- Returns:
- an LDContext with the same attributes as the LDUser
-
builder
Creates aContextBuilderfor building an LDContext, initializing itskeyand settingkindtoContextKind.DEFAULT.You may use
ContextBuildermethods to set additional attributes and/or change theContextBuilder.kind(ContextKind)before callingContextBuilder.build(). If you do not change any values, the defaults for the LDContext are that itskindisContextKind.DEFAULT("user"), itskeyis set to the key parameter passed here,anonymousisfalse, and it has no values for any other attributes.This method is for building an LDContext that has only a single Kind. To define a multi-kind LDContext, use
multiBuilder().if
keyis an empty string, there is no default. An LDContext must have a non-empty key, so if you callContextBuilder.build()in this state without usingContextBuilder.key(String)to set the key, you will get an invalid LDContext.- Parameters:
key- the context key- Returns:
- a builder
- See Also:
-
builder
Creates aContextBuilderfor building an LDContext, initializing itskeyandkind.You may use
ContextBuildermethods to set additional attributes and/or change theContextBuilder.kind(ContextKind)before callingContextBuilder.build(). If you do not change any values, the defaults for the LDContext are that itskindandkeyis set to the parameters passed here,anonymousisfalse, and it has no values for any other attributes.This method is for building an LDContext that has only a single Kind. To define a multi-kind LDContext, use
multiBuilder().if
keyis an empty string, there is no default. An LDContext must have a non-empty key, so if you callContextBuilder.build()in this state without usingContextBuilder.key(String)to set the key, you will get an invalid LDContext.- Parameters:
kind- the context kind; if null,ContextKind.DEFAULTis usedkey- the context key- Returns:
- a builder
- See Also:
-
builderFromContext
Creates a builder whose properties are the same as an existing single-kind LDContext.You may then change the builder's state in any way and call
ContextBuilder.build()to create a new independent LDContext.- Parameters:
context- the context to copy from- Returns:
- a builder
- See Also:
-
multiBuilder
Creates aContextMultiBuilderfor building a multi-kind context.This method is for building a Context that has multiple
ContextKindvalues, each with its own nested LDContext. To define a single-kind context, usebuilder(String)instead.- Returns:
- a builder
- See Also:
-
isValid
public boolean isValid()Returnstruefor a valid LDContext,falsefor an invalid one.A valid context is one that can be used in SDK operations. An invalid context is one that is missing necessary attributes or has invalid attributes, indicating an incorrect usage of the SDK API. The only ways for a context to be invalid are:
- It has a disallowed value for the
kindproperty. SeeContextKind. - It is a single-kind context whose
keyis empty. - It is a multi-kind context that does not have any kinds. See
createMulti(LDContext...). - It is a multi-kind context where the same kind appears more than once.
- It is a multi-kind context where at least one of the nested LDContexts has an error.
- It was created with
fromUser(LDUser)from a null LDUser reference, or from an LDUser that had a null key.
In any of these cases,
isValid()will return false, andgetError()will return a description of the error.Since in normal usage it is easy for applications to be sure they are using context kinds correctly, and because throwing an exception is undesirable in application code that uses LaunchDarkly, the SDK stores the error state in the LDContext itself and checks for such errors at the time the Context is used, such as in a flag evaluation. At that point, if the context is invalid, the operation will fail in some well-defined way as described in the documentation for that method, and the SDK will generally log a warning as well. But in any situation where you are not sure if you have a valid LDContext, you can check
isValid()orgetError().- Returns:
- true if the context is valid
- See Also:
- It has a disallowed value for the
-
getError
Returns null for a valid LDContext, or an error message for an invalid one.If this is null, then
isValid()is true. If it is non-null, thenisValid()is false.- Returns:
- an error description or null
- See Also:
-
getKind
Returns the context'skindattribute.Every valid context has a non-empty
ContextKind. For multi-kind contexts, this value isContextKind.MULTIand the kinds within the context can be inspected withgetIndividualContext(int)orgetIndividualContext(String).- Returns:
- the context kind
- See Also:
-
isMultiple
public boolean isMultiple()Returns true if this is a multi-kind context.If this value is true, then
getKind()is guaranteed to beContextKind.MULTI, and you can inspect the individual contexts for each kind withgetIndividualContext(int)orgetIndividualContext(ContextKind).If this value is false, then
getKind()is guaranteed to return a value that is notContextKind.MULTI.- Returns:
- true for a multi-kind context, false for a single-kind context
-
getKey
Returns the context'skeyattribute.For a single-kind context, this value is set by one of the LDContext factory methods or builders (
create(String),create(ContextKind, String),builder(String),builder(ContextKind, String)).For a multi-kind context, there is no single value and
getKey()returns an empty string. UsegetIndividualContext(int)orgetIndividualContext(String)to inspect the LDContext for a particular kind, then callgetKey()on it.This value is never null.
- Returns:
- the context key
- See Also:
-
getName
Returns the context'snameattribute.For a single-kind context, this value is set by
ContextBuilder.name(String). It is null if no value was set.For a multi-kind context, there is no single value and
getName()returns null. UsegetIndividualContext(int)orgetIndividualContext(String)to inspect the LDContext for a particular kind, then callgetName()on it.- Returns:
- the context name or null
- See Also:
-
isAnonymous
public boolean isAnonymous()Returns true if this context is only intended for flag evaluations and will 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
anonymousto 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,anonymousdoes not mean there is noname), and the context will still have whateverkeyyou 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.
- Returns:
- true if the context should be excluded from the LaunchDarkly database
- See Also:
-
getValue
Looks up the value of any attribute of the context by name.This includes only attributes that are addressable in evaluations-- not metadata such as
getPrivateAttribute(int).For a single-kind context, the attribute name can be any custom attribute that was set by methods like
ContextBuilder.set(String, boolean). It can also be one of the built-in ones like "kind", "key", or "name"; in such cases, it is equivalent togetKind(),getKey(), orgetName(), except that the value is returned using the general-purposeLDValuetype.For a multi-kind context, the only supported attribute name is "kind". Use
getIndividualContext(int)orgetIndividualContext(ContextKind)to inspect the LDContext for a particular kind and then get its attributes.This method does not support complex expressions for getting individual values out of JSON objects or arrays, such as "/address/street". Use
getValue(AttributeRef)with anAttributeReffor that purpose.If the value is found, the return value is the attribute value, using the type
LDValueto represent a value of any JSON type.If there is no such attribute, the return value is
LDValue.ofNull()(the method never returns a Javanull). An attribute that actually exists cannot have a null value.- Parameters:
attributeName- the desired attribute name- Returns:
- the value or
LDValue.ofNull() - See Also:
-
getValue
Looks up the value of any attribute of the context, or a value contained within an attribute, based on anAttributeRef.This includes only attributes that are addressable in evaluations-- not metadata such as
getPrivateAttribute(int).This implements the same behavior that the SDK uses to resolve attribute references during a flag evaluation. In a single-kind context, the
AttributeRefcan represent a simple attribute name-- either a built-in one like "name" or "key", or a custom attribute that was set by methods likeContextBuilder.set(String, String)-- or, it can be a slash-delimited path using a JSON-Pointer-like syntax. SeeAttributeReffor more details.For a multi-kind context, the only supported attribute name is "kind". Use
getIndividualContext(int)orgetIndividualContext(ContextKind)to inspect the LDContext for a particular kind and then get its attributes.This method does not support complex expressions for getting individual values out of JSON objects or arrays, such as "/address/street". Use
getValue(AttributeRef)with anAttributeReffor that purpose.If the value is found, the return value is the attribute value, using the type
LDValueto represent a value of any JSON type.If there is no such attribute, the return value is
LDValue.ofNull()(the method never returns a Javanull). An attribute that actually exists cannot have a null value.- Parameters:
attributeRef- an attribute reference- Returns:
- the attribute value
-
getCustomAttributeNames
Returns the names of all non-built-in attributes that have been set in this context.For a single-kind context, this includes all the names that were passed to any of the overloads of
ContextBuilder.set(String, LDValue)as long as the values were not null (since a null value in LaunchDarkly is equivalent to the attribute not being set).For a multi-kind context, there are no such names.
- Returns:
- an iterable of strings (may be empty, but will never be null)
-
getIndividualContextCount
public int getIndividualContextCount()Returns the number of context kinds in this context.For a valid single-kind context, this returns 1. For a multi-kind context, it returns the number of kinds that were added with
createMulti(LDContext...)ormultiBuilder(). For an invalid context, it returns zero.- Returns:
- the number of context kinds
-
getIndividualContext
Returns the single-kind LDContext corresponding to one of the kinds in this context.If this method is called on a single-kind LDContext, then the only allowable value for
indexis zero, and the return value on success is the same LDContext. If the method is called on a multi-kind context, then index must be non-negative and less than the number of kinds (that is, less than the return value ofgetIndividualContextCount()), and the return value on success is one of the individual LDContexts within.- Parameters:
index- the zero-based index of the context to get- Returns:
- an
LDContext, or null if the index was out of range
-
getIndividualContext
Returns the single-kind LDContext corresponding to one of the kinds in this context.If this method is called on a single-kind LDContext, then the only allowable value for
kindis the same asgetKind(), and the return value on success is the same LDContext. If the method is called on a multi-kind context, thenkindshould be match the kind of one of the contexts that was added withcreateMulti(LDContext...)ormultiBuilder(), and the return value on success is the corresponding individual LDContext within.- Parameters:
kind- the context kind to get; if null, defaults toContextKind.DEFAULT- Returns:
- an
LDContext, or null if that kind was not found
-
getIndividualContext
Same asgetIndividualContext(ContextKind), but specifies the kind as a plain string.- Parameters:
kind- the context kind to get- Returns:
- an
LDContext, or null if that kind was not found
-
getPrivateAttributeCount
public int getPrivateAttributeCount()Returns the number of private attribute references that were specified for this context.This is equal to the total number of values passed to
ContextBuilder.privateAttributes(String...)and/or its overloadContextBuilder.privateAttributes(AttributeRef...).- Returns:
- the number of private attribute references
-
getPrivateAttribute
Retrieves one of the private attribute references that were specified for this context.- Parameters:
index- a non-negative index that must be less thangetPrivateAttributeCount()- Returns:
- an
AttributeRef, or null if the index was out of range
-
getFullyQualifiedKey
Returns a string that describes the LDContext uniquely based onkindandkeyvalues.This value is used whenever LaunchDarkly needs a string identifier based on all of the
kindandkeyvalues in the context; the SDK may use this for caching previously seen contexts, for instance.- Returns:
- the fully-qualified key
-
toString
Returns a string representation of the context.For a valid context, this is currently defined as being the same as the JSON representation, since that is the simplest way to represent all of the LDContext properties. However, application code should not rely on
toString()always being the same as the JSON representation. If you specifically want the latter, useJsonSerialization.serialize(JsonSerializable).For an invalid context,
toString()returns a description of why it is invalid. -
equals
-
hashCode
public int hashCode()
-
LDContextdirectly instead.