- All Superinterfaces:
MapAccessor
,MapAccessorWithDefaultValue
- All Known Subinterfaces:
InternalValue
- All Known Implementing Classes:
BooleanValue
,BytesValue
,DateTimeValue
,DateValue
,DurationValue
,EntityValueAdapter
,FloatValue
,IntegerValue
,ListValue
,LocalDateTimeValue
,LocalTimeValue
,MapValue
,NodeValue
,NullValue
,NumberValueAdapter
,ObjectValueAdapter
,PathValue
,PointValue
,RelationshipValue
,StringValue
,TimeValue
,UnsupportedDateTimeValue
,ValueAdapter
This interface describes a number of isType
methods along with
typeValue
methods. The first set of these correlate with types from
the Neo4j Type System and are used to determine which Neo4j type is represented.
The second set of methods perform coercions to Java types (wherever possible).
For example, a common String value should be tested for using isString
and extracted using stringValue
.
Navigating a tree structure
Because Neo4j often handles dynamic structures, this interface is designed to help
you handle such structures in Java. Specifically, Value
lets you navigate arbitrary tree
structures without having to resort to type casting.
Given a tree structure like:
{
users : [
{ name : "Anders" },
{ name : "John" }
]
}
You can retrieve the name of the second user, John, like so:
String username = value.get("users").get(1).get("name").asString();
You can also easily iterate over the users:
List<String> names = new LinkedList<>();
for(Value user : value.get("users").values() )
{
names.add(user.get("name").asString());
}
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> T
Maps this value to the given type providing that is it supported.boolean
boolean
asBoolean
(boolean defaultValue) byte[]
byte[]
asByteArray
(byte[] defaultValue) double
asDouble()
Returns a Java double if no precision is lost in the conversion.double
asDouble
(double defaultValue) Returns a Java double if no precision is lost in the conversion.asEntity()
float
asFloat()
Returns a Java float if no precision is lost in the conversion.float
asFloat
(float defaultValue) Returns a Java float if no precision is lost in the conversion.int
asInt()
Returns a Java int if no precision is lost in the conversion.int
asInt
(int defaultValue) Returns a Java int if no precision is lost in the conversion.asIsoDuration
(IsoDuration defaultValue) asList()
If the underlying type can be viewed as a list, returns a java list of values, where each value has been converted usingasObject()
.<T> List<T>
<T> List<T>
If the underlying type can be viewed as a list, returns a java list of values, where each value has been converted usingasObject()
.asLocalDate
(LocalDate defaultValue) asLocalDateTime
(LocalDateTime defaultValue) asLocalTime
(LocalTime defaultValue) long
asLong()
Returns a Java long if no precision is lost in the conversion.long
asLong
(long defaultValue) Returns a Java long if no precision is lost in the conversion.Return as a map of string keys and values converted usingasObject()
.asNode()
asNumber()
asObject()
This returns a java standard library representation of the underlying value, using a java type that is "sensible" given the underlying type.asOffsetDateTime
(OffsetDateTime defaultValue) asOffsetTime
(OffsetTime defaultValue) asPath()
asPoint()
asString()
asZonedDateTime
(ZonedDateTime defaultValue) <T> T
computeOrDefault
(Function<Value, T> mapper, T defaultValue) boolean
get
(int index) Retrieve the value at the given indexint
hashCode()
boolean
Test if this value is a value of the given typeboolean
isEmpty()
If this value represents a list or map, test if the collection is empty.boolean
isFalse()
boolean
isNull()
boolean
isTrue()
keys()
If the underlying value supportskey-based indexing
, return an iterable of the keys in the map, this applies tomap
,node
andTypeSystem.RELATIONSHIP()
relationship} values.int
size()
If the underlying value is a collection type, return the number of values in the collection.toString()
type()
Methods inherited from interface org.neo4j.driver.types.MapAccessor
asMap, asMap, containsKey, get, values, values
-
Method Details
-
size
int size()If the underlying value is a collection type, return the number of values in the collection.For
TypeSystem.LIST()
list} values, this will return the size of the list.For
map
values, this will return the number of entries in the map.For
node
andTypeSystem.RELATIONSHIP()
relationship} values, this will return the number of properties.For
path
values, this returns the length (number of relationships) in the path.- Specified by:
size
in interfaceMapAccessor
- Returns:
- the number of values in an underlying collection
-
isEmpty
boolean isEmpty()If this value represents a list or map, test if the collection is empty.- Returns:
true
if size() is 0, otherwisefalse
-
keys
If the underlying value supportskey-based indexing
, return an iterable of the keys in the map, this applies tomap
,node
andTypeSystem.RELATIONSHIP()
relationship} values.- Specified by:
keys
in interfaceMapAccessor
- Returns:
- the keys in the value
-
get
Retrieve the value at the given index- Parameters:
index
- the index of the value- Returns:
- the value or a
NullValue
if the index is out of bounds - Throws:
ClientException
- if record has not been initialized
-
type
Type type()- Returns:
- The type of this value as defined in the Neo4j type system
-
hasType
Test if this value is a value of the given type- Parameters:
type
- the given type- Returns:
- type.isTypeOf(this)
-
isTrue
boolean isTrue()- Returns:
true
if the value is a Boolean value and has the value True.
-
isFalse
boolean isFalse()- Returns:
true
if the value is a Boolean value and has the value False.
-
isNull
boolean isNull()- Returns:
true
if the value is a Null, otherwisefalse
-
asObject
Object asObject()This returns a java standard library representation of the underlying value, using a java type that is "sensible" given the underlying type. The mapping for common types is as follows:TypeSystem.NULL()
-null
TypeSystem.LIST()
-List
TypeSystem.MAP()
-Map
TypeSystem.BOOLEAN()
-Boolean
TypeSystem.INTEGER()
-Long
TypeSystem.FLOAT()
-Double
TypeSystem.STRING()
-String
TypeSystem.BYTES()
- byte[]TypeSystem.DATE()
-LocalDate
TypeSystem.TIME()
-OffsetTime
TypeSystem.LOCAL_TIME()
-LocalTime
TypeSystem.DATE_TIME()
-ZonedDateTime
TypeSystem.LOCAL_DATE_TIME()
-LocalDateTime
TypeSystem.DURATION()
-IsoDuration
TypeSystem.POINT()
-Point
TypeSystem.NODE()
-Node
TypeSystem.RELATIONSHIP()
-Relationship
TypeSystem.PATH()
-Path
Note that the types in
TypeSystem
refers to the Neo4j type system whereTypeSystem.INTEGER()
andTypeSystem.FLOAT()
are both 64-bit precision. This is why these types return javaLong
andDouble
, respectively.- Returns:
- the value as a Java Object.
- Throws:
DateTimeException
- if zone information supplied by server is not supported by driver runtime. Applicable to datetime values only.
-
computeOrDefault
-
asBoolean
boolean asBoolean()- Returns:
- the value as a Java boolean, if possible.
- Throws:
Uncoercible
- if value types are incompatible.
-
asBoolean
boolean asBoolean(boolean defaultValue) - Parameters:
defaultValue
- return this value if the value is aNullValue
.- Returns:
- the value as a Java boolean, if possible.
- Throws:
Uncoercible
- if value types are incompatible.
-
asByteArray
byte[] asByteArray()- Returns:
- the value as a Java byte array, if possible.
- Throws:
Uncoercible
- if value types are incompatible.
-
asByteArray
byte[] asByteArray(byte[] defaultValue) - Parameters:
defaultValue
- default to this value if the original value is aNullValue
- Returns:
- the value as a Java byte array, if possible.
- Throws:
Uncoercible
- if value types are incompatible.
-
asString
String asString()- Returns:
- the value as a Java String, if possible.
- Throws:
Uncoercible
- if value types are incompatible.
-
asString
- Parameters:
defaultValue
- return this value if the value is null.- Returns:
- the value as a Java String, if possible
- Throws:
Uncoercible
- if value types are incompatible.
-
asNumber
Number asNumber()- Returns:
- the value as a Java Number, if possible.
- Throws:
Uncoercible
- if value types are incompatible.
-
asLong
long asLong()Returns a Java long if no precision is lost in the conversion.- Returns:
- the value as a Java long.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asLong
long asLong(long defaultValue) Returns a Java long if no precision is lost in the conversion.- Parameters:
defaultValue
- return this default value if the value is aNullValue
.- Returns:
- the value as a Java long.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asInt
int asInt()Returns a Java int if no precision is lost in the conversion.- Returns:
- the value as a Java int.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asInt
int asInt(int defaultValue) Returns a Java int if no precision is lost in the conversion.- Parameters:
defaultValue
- return this default value if the value is aNullValue
.- Returns:
- the value as a Java int.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asDouble
double asDouble()Returns a Java double if no precision is lost in the conversion.- Returns:
- the value as a Java double.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asDouble
double asDouble(double defaultValue) Returns a Java double if no precision is lost in the conversion.- Parameters:
defaultValue
- default to this value if the value is aNullValue
.- Returns:
- the value as a Java double.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asFloat
float asFloat()Returns a Java float if no precision is lost in the conversion.- Returns:
- the value as a Java float.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asFloat
float asFloat(float defaultValue) Returns a Java float if no precision is lost in the conversion.- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a Java float.
- Throws:
LossyCoercion
- if it is not possible to convert the value without loosing precision.Uncoercible
- if value types are incompatible.
-
asList
If the underlying type can be viewed as a list, returns a java list of values, where each value has been converted usingasObject()
.- Returns:
- the value as a Java list of values, if possible
- See Also:
-
asList
If the underlying type can be viewed as a list, returns a java list of values, where each value has been converted usingasObject()
.- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a Java list of values, if possible
- See Also:
-
asList
- Type Parameters:
T
- the type of target list elements- Parameters:
mapFunction
- a function to map from Value to T. SeeValues
for some predefined functions, such asValues.ofBoolean()
,Values.ofList(Function)
.- Returns:
- the value as a list of T obtained by mapping from the list elements, if possible
- See Also:
-
asList
- Type Parameters:
T
- the type of target list elements- Parameters:
mapFunction
- a function to map from Value to T. SeeValues
for some predefined functions, such asValues.ofBoolean()
,Values.ofList(Function)
.defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a list of T obtained by mapping from the list elements, if possible
- See Also:
-
asEntity
Entity asEntity()- Returns:
- the value as a
Entity
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asNode
Node asNode()- Returns:
- the value as a
Node
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asRelationship
Relationship asRelationship()- Returns:
- the value as a
Relationship
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asPath
Path asPath()- Returns:
- the value as a
Path
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asLocalDate
LocalDate asLocalDate()- Returns:
- the value as a
LocalDate
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asOffsetTime
OffsetTime asOffsetTime()- Returns:
- the value as a
OffsetTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asLocalTime
LocalTime asLocalTime()- Returns:
- the value as a
LocalTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asLocalDateTime
LocalDateTime asLocalDateTime()- Returns:
- the value as a
LocalDateTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asOffsetDateTime
OffsetDateTime asOffsetDateTime()- Returns:
- the value as a
OffsetDateTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.DateTimeException
- if zone information supplied by server is not supported by driver runtime.
-
asZonedDateTime
ZonedDateTime asZonedDateTime()- Returns:
- the value as a
ZonedDateTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.DateTimeException
- if zone information supplied by server is not supported by driver runtime.
-
asIsoDuration
IsoDuration asIsoDuration()- Returns:
- the value as a
IsoDuration
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asPoint
Point asPoint()- Returns:
- the value as a
Point
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asLocalDate
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
LocalDate
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asOffsetTime
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
OffsetTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asLocalTime
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
LocalTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asLocalDateTime
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
LocalDateTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asOffsetDateTime
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
OffsetDateTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asZonedDateTime
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
ZonedDateTime
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asIsoDuration
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
IsoDuration
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asPoint
- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a
Point
, if possible. - Throws:
Uncoercible
- if value types are incompatible.
-
asMap
Return as a map of string keys and values converted usingasObject()
.This is equivalent to calling
asMap(Function, Map)
withValues.ofObject()
.- Parameters:
defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a Java map
-
asMap
- Type Parameters:
T
- the type of map values- Parameters:
mapFunction
- a function to map from Value to T. SeeValues
for some predefined functions, such asValues.ofBoolean()
,Values.ofList(Function)
.defaultValue
- default to this value if the value is aNullValue
- Returns:
- the value as a map from string keys to values of type T obtained from mapping he original map values, if possible
- See Also:
-
as
Maps this value to the given type providing that is it supported.Basic Mapping
Supported destination types depend on the value
Type
, please see the table below for more details.Object Mapping
Mapping of user-defined properties to user-defined types is supported for the following value types:
Example (using the Neo4j Movies Database):
// assuming the following Java record public record Movie(String title, String tagline, long released) {} // the nodes may be mapped to Movie instances var movies = driver.executableQuery("MATCH (movie:Movie) RETURN movie") .execute() .records() .stream() .map(record -> record.get("movie").as(Movie.class)) .toList();
Note that Object Mapping is an alternative to accessing the user-defined values in a
MapAccessor
. If Object Graph Mapping (OGM) is needed, please use a higher level solution built on top of the driver, like Spring Data Neo4j.The mapping is done by matching user-defined property names to target type constructor parameters. Therefore, the constructor parameters must either have
Property
annotation or have a matching name that is available at runtime (note that the constructor parameter names are typically changed by the compiler unless either the compiler-parameters
option is used or they belong to the cannonical constructor ofjava.lang.Record
). The name matching is case-sensitive.Additionally, the
Property
annotation may be used when mapping a property with a different name tojava.lang.Record
cannonical constructor parameter.The constructor selection criteria is the following (top priority first):
- Maximum matching properties.
- Minimum mismatching properties.
Class.getDeclaredConstructors()
and is finished either when a full match is found with no mismatches or once all constructors have been visited.At least 1 property match must be present for mapping to work.
A
null
value is used for arguments that don't have a matching property. If the argument does not acceptnull
value (this includes primitive types), an alternative constructor that excludes it must be available.The mapping only works for types with directly accessible constructors, not interfaces or abstract types.
Example with optional property (using the Neo4j Movies Database):
// assuming the following Java record public record Person(String name, long born) { // alternative constructor for values that don't have 'born' property available public Person(@Property("name") String name) { this(name, -1); } } // the nodes may be mapped to Person instances var persons = driver.executableQuery("MATCH (person:Person) RETURN person") .execute() .records() .stream() .map(record -> record.get("person").as(Person.class)) .toList();
Types with generic parameters defined at the class level are not supported. However, constructor arguments with specific types are permitted.
Example (using the Neo4j Movies Database):
// assuming the following Java record public record Acted(List<String> roles) {} // the relationships may be mapped to Acted instances var actedList = driver.executableQuery("MATCH ()-[acted:ACTED_IN]-() RETURN acted") .execute() .records() .stream() .map(record -> record.get("acted").as(Acted.class)) .toList();
public record Acted<T>(List<T> roles) {}
- Type Parameters:
T
- the target type to map to- Parameters:
targetClass
- the target class to map to- Returns:
- the mapped value
- Throws:
Uncoercible
- when mapping to the target type is not possibleLossyCoercion
- when mapping cannot be achieved without losing precision- Since:
- 5.28.5
- See Also:
-
equals
-
hashCode
int hashCode() -
toString
String toString()
-