Package com.faunadb.client.types
Class Value
- java.lang.Object
-
- com.faunadb.client.query.Expr
-
- com.faunadb.client.types.Value
-
- Direct Known Subclasses:
Value.ArrayV
,Value.BooleanV
,Value.BytesV
,Value.DateV
,Value.DoubleV
,Value.LongV
,Value.NullV
,Value.ObjectV
,Value.QueryV
,Value.RefV
,Value.SetRefV
,Value.StringV
,Value.TimeV
public abstract class Value extends Expr
Value
represents query responses from FaunaDB. Instances ofValue
should be treated as opaque vales until converted to a native type. Available conversion methods are:to(Class)
to(Codec)
get(Field)
getOptional(Field)
collect(Field)
asCollectionOf(Class)
asMapOf(Class)
Examples:
Using the traversal API:
Using a field extractor:Value result = client.query(getUserQuery).get(); String name = result .at("data", "name") .to(String.class) .get();
Field<String> name = Field.at("data", "name").to(String.class); Value result = client.query(getUserQuery).get(); String name = result.get(name);
- See Also:
- Fauna Data Types,
Field
,Codec
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Value.ArrayV
Represents an array value in the FaunaDB query language.static class
Value.BooleanV
Represents a boolean value in the FaunaDB query language.static class
Value.BytesV
Represents a blob value in the FaunaDB query language.static class
Value.DateV
Represents a date value in the FaunaDB query language.static class
Value.DoubleV
Represents a double value in the FaunaDB query language.static class
Value.LongV
Represents a long value in the FaunaDB query language.static class
Value.Native
Builtin reference types.static class
Value.NullV
Represents a null value in the FaunaDB query language.static class
Value.ObjectV
Represents an Object value in the FaunaDB query language.static class
Value.QueryV
Represents a query value in the FaunaDB query language.static class
Value.RefID
TheValue.RefV
internal ID representation.static class
Value.RefV
A FaunaDB reference type.static class
Value.SetRefV
A FaunaDB set literal.static class
Value.StringV
Represents a string value in the FaunaDB query language.static class
Value.TimeV
Represents a timestamp value in the FaunaDB query language.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Result<Collection<T>>
asCollectionOf(Class<T> elementType)
Attempts to convert the value to aCollection
.<T> Result<Map<String,T>>
asMapOf(Class<T> valueType)
Attempts to convert the value to aMap
.Value
at(int... indexes)
Assuming the underlying value is a collection, it traverses to a desired path.Value
at(String... keys)
Assuming the underlying value is a key/value map, it traverses to a desired path.<T> List<T>
collect(Field<T> field)
Assuming the underlying value is a collection, it collects theField
provided for all elements in the collection.<T> Collection<T>
collect(Class<T> elementType)
Attempts to convert the value to aCollection
.static <T> Result<Value>
from(T obj)
<T> T
get(Field<T> field)
Extract aField
from the value.<T> T
get(Class<T> clazz)
Attempts to decode the value using the reflectionDecoder
.Optional<Value>
getOptional()
Converts this into anOptional
.<T> Optional<T>
getOptional(Field<T> field)
Safely attempts to extract aField
from this value.Value
orNull()
Returns this value, or returns null if no value is present.<T> Result<T>
to(Codec<T> codec)
Attempts to convert the value using theCodec
passed.<T> Result<T>
to(Class<T> clazz)
Attempts to decode the value using the reflectionDecoder
.<T> Map<String,T>
toMap(Class<T> valueType)
Attempts to convert the value to aMap
.
-
-
-
Method Detail
-
to
public final <T> Result<T> to(Codec<T> codec)
Attempts to convert the value using theCodec
passed.
-
to
public final <T> Result<T> to(Class<T> clazz)
Attempts to decode the value using the reflectionDecoder
.
-
from
public static <T> Result<Value> from(T obj)
- Type Parameters:
T
- the type to convert from- Parameters:
obj
- the object instance to encode- Returns:
- the
Result
of the conversion
-
asMapOf
public final <T> Result<Map<String,T>> asMapOf(Class<T> valueType)
Attempts to convert the value to aMap
.
-
toMap
public final <T> Map<String,T> toMap(Class<T> valueType)
Attempts to convert the value to aMap
.
-
asCollectionOf
public final <T> Result<Collection<T>> asCollectionOf(Class<T> elementType)
Attempts to convert the value to aCollection
.- Type Parameters:
T
- the type of the elements on theCollection
- Parameters:
elementType
- the type of the elements in theCollection
.- Returns:
- a
Result
containing the resultingCollection
. - See Also:
Decoder
,Types
-
collect
public final <T> Collection<T> collect(Class<T> elementType)
Attempts to convert the value to aCollection
.- Type Parameters:
T
- the type of the elements in theCollection
- Parameters:
elementType
- the type of the elements in theCollection
- Returns:
- a new collection
- See Also:
Decoder
,Types
-
get
public final <T> T get(Field<T> field)
Extract aField
from the value.- Type Parameters:
T
- the type of returned field- Parameters:
field
- theField
to extract- Returns:
- the resulting value of extracting the
Field
from this value - Throws:
IllegalStateException
- ifField
does not exists on this value- See Also:
Field
-
get
public final <T> T get(Class<T> clazz)
Attempts to decode the value using the reflectionDecoder
.- Type Parameters:
T
- the type to convert to- Parameters:
clazz
- a class type to convert- Returns:
- the converted result
- See Also:
Decoder
-
getOptional
public final <T> Optional<T> getOptional(Field<T> field)
Safely attempts to extract aField
from this value.- Type Parameters:
T
- the type of returned field- Parameters:
field
-Field
to extract- Returns:
- An
Optional
containing the resulting value, if the field extraction was successful. It returnsOptional.empty()
, otherwise. - See Also:
Field
-
getOptional
public Optional<Value> getOptional()
Converts this into anOptional
.- Returns:
- An
Optional
containing this value, if present.
-
orNull
public final Value orNull()
Returns this value, or returns null if no value is present.- Returns:
- the value or null
-
collect
public final <T> List<T> collect(Field<T> field)
Assuming the underlying value is a collection, it collects theField
provided for all elements in the collection.For example:
Field<String> userName = Field.at("name").to(String.class); Value result = client.query(getAllUsersQuery).get(); List<String> userNames = result.at("data").collect(userName);
-
at
public final Value at(String... keys)
Assuming the underlying value is a key/value map, it traverses to a desired path.For example:
Value result = client.query(getUser).get(); Value zipCode = result.at("data", "address", "zipCode");
- Parameters:
keys
- the object keys to traverse- Returns:
- the
Value
under the path provided
-
at
public final Value at(int... indexes)
Assuming the underlying value is a collection, it traverses to a desired path.For example:
Value result = client.query(getAllUsers).get(); Value firstUser = result.at("data").at(0);
- Parameters:
indexes
- the collection indexes to traverse- Returns:
- the
Value
under the path provided
-
-