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
Valuerepresents query responses from FaunaDB. Instances ofValueshould 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 classValue.ArrayVRepresents an array value in the FaunaDB query language.static classValue.BooleanVRepresents a boolean value in the FaunaDB query language.static classValue.BytesVRepresents a blob value in the FaunaDB query language.static classValue.DateVRepresents a date value in the FaunaDB query language.static classValue.DoubleVRepresents a double value in the FaunaDB query language.static classValue.LongVRepresents a long value in the FaunaDB query language.static classValue.NativeBuiltin reference types.static classValue.NullVRepresents a null value in the FaunaDB query language.static classValue.ObjectVRepresents an Object value in the FaunaDB query language.static classValue.QueryVRepresents a query value in the FaunaDB query language.static classValue.RefIDTheValue.RefVinternal ID representation.static classValue.RefVA FaunaDB reference type.static classValue.SetRefVA FaunaDB set literal.static classValue.StringVRepresents a string value in the FaunaDB query language.static classValue.TimeVRepresents 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.Valueat(int... indexes)Assuming the underlying value is a collection, it traverses to a desired path.Valueat(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 theFieldprovided 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> Tget(Field<T> field)Extract aFieldfrom the value.<T> Tget(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 aFieldfrom this value.ValueorNull()Returns this value, or returns null if no value is present.<T> Result<T>to(Codec<T> codec)Attempts to convert the value using theCodecpassed.<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 theCodecpassed.
-
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
Resultof 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
Resultcontaining 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 aFieldfrom the value.- Type Parameters:
T- the type of returned field- Parameters:
field- theFieldto extract- Returns:
- the resulting value of extracting the
Fieldfrom this value - Throws:
IllegalStateException- ifFielddoes 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 aFieldfrom this value.- Type Parameters:
T- the type of returned field- Parameters:
field-Fieldto extract- Returns:
- An
Optionalcontaining 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
Optionalcontaining 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 theFieldprovided 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
Valueunder 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
Valueunder the path provided
-
-