public abstract class LDValue
extends java.lang.Object
This is used with the client's LDClientInterface.jsonValueVariation(String, LDUser, LDValue)
method, and is also used internally to hold feature flag values.
While the LaunchDarkly SDK uses Gson for JSON parsing, some of the Gson value types (object
and array) are mutable. In contexts where it is important for data to remain immutable after
it is created, these values are represented with LDValue
instead. It is easily
convertible to primitive types and provides array element/object property accessors.
Modifier and Type | Class and Description |
---|---|
static class |
LDValue.Convert
Predefined instances of
LDValue.Converter for commonly used types. |
static class |
LDValue.Converter<T>
Defines a conversion between
LDValue and some other type. |
Constructor and Description |
---|
LDValue() |
Modifier and Type | Method and Description |
---|---|
com.google.gson.JsonElement |
asJsonElement()
Deprecated.
The Gson types may be removed from the public API at some point; it is preferable to
use getters like
booleanValue() and getType() . |
com.google.gson.JsonElement |
asUnsafeJsonElement()
Deprecated.
This method will be removed in a future version. Application code should always use
asJsonElement() . |
boolean |
booleanValue()
Returns this value as a boolean if it is explicitly a boolean.
|
static ArrayBuilder |
buildArray()
Starts building an array value.
|
static ObjectBuilder |
buildObject()
Starts building an object value.
|
double |
doubleValue()
Returns this value as a
double if it is numeric. |
boolean |
equals(java.lang.Object o) |
float |
floatValue()
Returns this value as a
float if it is numeric. |
static LDValue |
fromJsonElement(com.google.gson.JsonElement value)
Deprecated.
The Gson types may be removed from the public API at some point; it is preferable to
use factory methods like
of(boolean) . |
LDValue |
get(int index)
Returns an array element by index.
|
LDValue |
get(java.lang.String name)
Returns an object property by name.
|
abstract LDValueType |
getType()
Gets the JSON type for this value.
|
int |
hashCode() |
int |
intValue()
Returns this value as an
int if it is numeric. |
boolean |
isInt()
Tests whether this value is a number that is also an integer.
|
boolean |
isNull()
Tests whether this value is a null.
|
boolean |
isNumber()
Tests whether this value is a number (not a numeric string).
|
boolean |
isString()
Tests whether this value is a string.
|
java.lang.Iterable<java.lang.String> |
keys()
Enumerates the property names in an object.
|
long |
longValue()
Returns this value as a
long if it is numeric. |
static LDValue |
normalize(LDValue value)
Returns the same value if non-null, or
ofNull() if null. |
static LDValue |
of(boolean value)
Returns an instance for a boolean value.
|
static LDValue |
of(double value)
Returns an instance for a numeric value.
|
static LDValue |
of(float value)
Returns an instance for a numeric value.
|
static LDValue |
of(int value)
Returns an instance for a numeric value.
|
static LDValue |
of(long value)
Returns an instance for a numeric value.
|
static LDValue |
of(java.lang.String value)
Returns an instance for a string value (or a null).
|
static LDValue |
ofNull()
Returns an instance for a null value.
|
int |
size()
Returns the number of elements in an array or object.
|
java.lang.String |
stringValue()
Returns this value as a
String if it is a string. |
java.lang.String |
toJsonString()
Converts this value to its JSON serialization.
|
java.lang.String |
toString() |
static LDValue |
unsafeFromJsonElement(com.google.gson.JsonElement value)
Deprecated.
This method will be removed in a future version. Application code should use
fromJsonElement(JsonElement) or, preferably, factory methods like of(boolean) . |
java.lang.Iterable<LDValue> |
values()
Enumerates the values in an array or object.
|
<T> java.lang.Iterable<T> |
valuesAs(LDValue.Converter<T> converter)
Enumerates the values in an array or object, converting them to a specific type.
|
public static LDValue normalize(LDValue value)
ofNull()
if null.public static LDValue ofNull()
public static LDValue of(boolean value)
true
and false
are always used.value
- a boolean valuepublic static LDValue of(int value)
value
- an integer numeric valuepublic static LDValue of(long value)
Note that the LaunchDarkly service, and most of the SDKs, represent numeric values internally
in 64-bit floating-point, which has slightly less precision than a signed 64-bit long
;
therefore, the full range of long
values cannot be accurately represented. If you need
to set a user attribute to a numeric value with more significant digits than will fit in a
double
, it is best to encode it as a string.
value
- a long integer numeric valuepublic static LDValue of(float value)
value
- a floating-point numeric valuepublic static LDValue of(double value)
value
- a floating-point numeric valuepublic static LDValue of(java.lang.String value)
value
- a nullable String referenceofNull()
if the value was null.public static ArrayBuilder buildArray()
LDValue arrayOfInts = LDValue.buildArray().add(LDValue.int(1), LDValue.int(2)).build():
If the values are all of the same type, you may also use LDValue.Converter.arrayFrom(Iterable)
or LDValue.Converter.arrayOf(Object...)
.ArrayBuilder
public static ObjectBuilder buildObject()
LDValue objectVal = LDValue.buildObject().put("key", LDValue.int(1)).build():
If the values are all of the same type, you may also use LDValue.Converter.objectFrom(Map)
.ObjectBuilder
@Deprecated public static LDValue fromJsonElement(com.google.gson.JsonElement value)
of(boolean)
.JsonElement
value. If the value is a complex type, it is
deep-copied; primitive types are used as is.value
- a nullable JsonElement
referenceofNull()
if the value was null.@Deprecated public static LDValue unsafeFromJsonElement(com.google.gson.JsonElement value)
fromJsonElement(JsonElement)
or, preferably, factory methods like of(boolean)
.JsonElement
value without copying it. This
method exists only to support deprecated SDK methods where a JsonElement
is needed, to
avoid the inefficiency of a deep-copy; application code should not use it, since it can break
the immutability contract of LDValue
.value
- a nullable JsonElement
referenceofNull()
if the value was null.public abstract LDValueType getType()
LDValueType
public boolean isNull()
true
if this is a null valuepublic boolean booleanValue()
false
.public boolean isNumber()
true
if this is a numeric valuepublic boolean isInt()
JSON does not have separate types for integer and floating-point values; they are both just
numbers. This method returns true if and only if the actual numeric value has no fractional
component, so LDValue.of(2).isInt()
and LDValue.of(2.0f).isInt()
are both true.
true
if this is an integer valuepublic int intValue()
int
if it is numeric. Returns zero for all non-numeric values.
If the value is a number but not an integer, it will be rounded toward zero (truncated). This is consistent with Java casting behavior, and with most other LaunchDarkly SDKs.
int
valuepublic long longValue()
long
if it is numeric. Returns zero for all non-numeric values.
If the value is a number but not an integer, it will be rounded toward zero (truncated). This is consistent with Java casting behavior, and with most other LaunchDarkly SDKs.
long
valuepublic float floatValue()
float
if it is numeric. Returns zero for all non-numeric values.float
valuepublic double doubleValue()
double
if it is numeric. Returns zero for all non-numeric values.double
valuepublic boolean isString()
true
if this is a string valuepublic java.lang.String stringValue()
String
if it is a string. Returns null
for all non-string values.public int size()
public java.lang.Iterable<java.lang.String> keys()
public java.lang.Iterable<LDValue> values()
LDValue
valuespublic <T> java.lang.Iterable<T> valuesAs(LDValue.Converter<T> converter)
This is an efficient method because it does not copy values to a new list, but returns a view into the existing array.
Example:
LDValue anArrayOfInts = LDValue.Convert.Integer.arrayOf(1, 2, 3);
for (int i: anArrayOfInts.valuesAs(LDValue.Convert.Integer)) { println(i); }
T
- the desired typeconverter
- the LDValue.Converter
for the specified typepublic LDValue get(int index)
ofNull()
if this is not an array or if the
index is out of range (will never throw an exception).index
- the array indexofNull()
public LDValue get(java.lang.String name)
ofNull()
if this is not an object or if the
key is not found (will never throw an exception).name
- the property nameofNull()
public java.lang.String toJsonString()
@Deprecated public com.google.gson.JsonElement asJsonElement()
booleanValue()
and getType()
.JsonElement
. If the value is a complex type, it is deep-copied
deep-copied, so modifying the return value will not affect the LDValue
.JsonElement
, or null
if the value is a null@Deprecated public com.google.gson.JsonElement asUnsafeJsonElement()
asJsonElement()
.JsonElement
if the value was created from one, otherwise converts the
value to a JsonElement
. This method exists only to support deprecated SDK methods where a
JsonElement
is needed, to avoid the inefficiency of a deep-copy; application code should not
use it, since it can break the immutability contract of LDValue
.JsonElement
, or null
if the value is a nullpublic java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object