Class LDContext
- java.lang.Object
-
- com.launchdarkly.sdk.LDContext
-
- All Implemented Interfaces:
JsonSerializable
public final class LDContext extends java.lang.Object implements JsonSerializable
A collection of attributes that can be referenced in flag evaluations and analytics events.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)
orcreate(ContextKind, String)
when only the key matters; or, to specify other attributes, usebuilder(String)
.To create an LDContext with multiple kinds, use
createMulti(LDContext...)
ormultiBuilder()
.An LDContext can be in an error state if it was built with invalid attributes. See
isValid()
andgetError()
.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
Gson
instance withLDGson
. - With Jackson, if and only if you configure your
ObjectMapper
instance withLDJackson
.
To learn more about contexts, read the documentation.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ContextBuilder
builder(ContextKind kind, java.lang.String key)
static ContextBuilder
builder(java.lang.String key)
Creates aContextBuilder
for building an LDContext, initializing itskey
and settingkind
toContextKind.DEFAULT
.static ContextBuilder
builderFromContext(LDContext context)
Creates a builder whose properties are the same as an existing single-kind LDContext.static LDContext
create(ContextKind kind, java.lang.String key)
Creates a single-kind LDContext with only the kind and keys specified.static LDContext
create(java.lang.String key)
Creates a single-kind LDContext with a kind ofContextKind.DEFAULT
} and the specified key.static LDContext
createMulti(LDContext... contexts)
Creates a multi-kind LDContext out of the specified single-kind LDContexts.boolean
equals(java.lang.Object other)
static LDContext
fromUser(LDUser user)
Deprecated.useLDContext
directly instead.java.lang.Iterable<java.lang.String>
getCustomAttributeNames()
Returns the names of all non-built-in attributes that have been set in this context.java.lang.String
getError()
Returns null for a valid LDContext, or an error message for an invalid one.java.lang.String
getFullyQualifiedKey()
Returns a string that describes the LDContext uniquely based onkind
andkey
values.LDContext
getIndividualContext(int index)
Returns the single-kind LDContext corresponding to one of the kinds in this context.LDContext
getIndividualContext(ContextKind kind)
Returns the single-kind LDContext corresponding to one of the kinds in this context.LDContext
getIndividualContext(java.lang.String kind)
Same asgetIndividualContext(ContextKind)
, but specifies the kind as a plain string.int
getIndividualContextCount()
Returns the number of context kinds in this context.java.lang.String
getKey()
Returns the context'skey
attribute.ContextKind
getKind()
Returns the context'skind
attribute.java.lang.String
getName()
Returns the context'sname
attribute.AttributeRef
getPrivateAttribute(int index)
Retrieves one of the private attribute references that were specified for this context.int
getPrivateAttributeCount()
Returns the number of private attribute references that were specified for this context.LDValue
getValue(AttributeRef attributeRef)
Looks up the value of any attribute of the context, or a value contained within an attribute, based on anAttributeRef
.LDValue
getValue(java.lang.String attributeName)
Looks up the value of any attribute of the context by name.int
hashCode()
boolean
isAnonymous()
Returns true if this context is only intended for flag evaluations and will not be indexed by LaunchDarkly.boolean
isMultiple()
Returns true if this is a multi-kind context.boolean
isValid()
Returnstrue
for a valid LDContext,false
for an invalid one.static ContextMultiBuilder
multiBuilder()
Creates aContextMultiBuilder
for building a multi-kind context.java.lang.String
toString()
Returns a string representation of the context.
-
-
-
Method Detail
-
create
public static LDContext create(java.lang.String key)
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(ContextKind, String)
,builder(String)
-
create
public static LDContext create(ContextKind kind, java.lang.String key)
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.DEFAULT
will be usedkey
- the context key- Returns:
- an LDContext
- See Also:
create(String)
,builder(ContextKind, String)
-
createMulti
public static LDContext createMulti(LDContext... contexts)
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:
multiBuilder()
-
fromUser
@Deprecated public static LDContext fromUser(LDUser user)
Deprecated.useLDContext
directly instead.Converts a user to an equivalentLDContext
instance.This method is used by the SDK whenever an application passes a
LDUser
instance to methods such asidentify
. The SDK operates internally on theLDContext
model, which is more flexible than the older LDUser model: an L User can always be converted to an LDContext, but not vice versa. TheContextKind
of 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
user
parameter 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
public static ContextBuilder builder(java.lang.String key)
Creates aContextBuilder
for building an LDContext, initializing itskey
and settingkind
toContextKind.DEFAULT
.You may use
ContextBuilder
methods 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 itskind
isContextKind.DEFAULT
("user"), itskey
is set to the key parameter passed here,anonymous
isfalse
, 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
key
is 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(ContextKind, String)
,multiBuilder()
,create(String)
-
builder
public static ContextBuilder builder(ContextKind kind, java.lang.String key)
Creates aContextBuilder
for building an LDContext, initializing itskey
andkind
.You may use
ContextBuilder
methods 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 itskind
andkey
is set to the parameters passed here,anonymous
isfalse
, 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
key
is 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.DEFAULT
is usedkey
- the context key- Returns:
- a builder
- See Also:
builder(String)
,multiBuilder()
,create(ContextKind, String)
-
builderFromContext
public static ContextBuilder builderFromContext(LDContext context)
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:
builder(String)
-
multiBuilder
public static ContextMultiBuilder multiBuilder()
Creates aContextMultiBuilder
for building a multi-kind context.This method is for building a Context that has multiple
ContextKind
values, each with its own nested LDContext. To define a single-kind context, usebuilder(String)
instead.- Returns:
- a builder
- See Also:
createMulti(LDContext...)
-
isValid
public boolean isValid()
Returnstrue
for a valid LDContext,false
for 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
kind
property. SeeContextKind
. - It is a single-kind context whose
key
is 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:
getError()
- It has a disallowed value for the
-
getError
public java.lang.String 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:
isValid()
-
getKind
public ContextKind getKind()
Returns the context'skind
attribute.Every valid context has a non-empty
ContextKind
. For multi-kind contexts, this value isContextKind.MULTI
and the kinds within the context can be inspected withgetIndividualContext(int)
orgetIndividualContext(String)
.- Returns:
- the context kind
- See Also:
ContextBuilder.kind(ContextKind)
-
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
public java.lang.String getKey()
Returns the context'skey
attribute.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:
ContextBuilder.key(String)
-
getName
public java.lang.String getName()
Returns the context'sname
attribute.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:
ContextBuilder.name(String)
-
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
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.
- Returns:
- true if the context should be excluded from the LaunchDarkly database
- See Also:
ContextBuilder.anonymous(boolean)
-
getValue
public LDValue getValue(java.lang.String attributeName)
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-purposeLDValue
type.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 anAttributeRef
for that purpose.If the value is found, the return value is the attribute value, using the type
LDValue
to 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(AttributeRef)
,ContextBuilder.set(String, String)
-
getValue
public LDValue getValue(AttributeRef attributeRef)
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
AttributeRef
can 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. SeeAttributeRef
for 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 anAttributeRef
for that purpose.If the value is found, the return value is the attribute value, using the type
LDValue
to 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
public java.lang.Iterable<java.lang.String> 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
public LDContext getIndividualContext(int index)
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
index
is 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
public LDContext getIndividualContext(ContextKind kind)
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
kind
is the same asgetKind()
, and the return value on success is the same LDContext. If the method is called on a multi-kind context, thenkind
should 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
public LDContext getIndividualContext(java.lang.String kind)
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
public AttributeRef getPrivateAttribute(int index)
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
public java.lang.String getFullyQualifiedKey()
Returns a string that describes the LDContext uniquely based onkind
andkey
values.This value is used whenever LaunchDarkly needs a string identifier based on all of the
kind
andkey
values in the context; the SDK may use this for caching previously seen contexts, for instance.- Returns:
- the fully-qualified key
-
toString
public java.lang.String 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.- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-