Class PathWalker
A PathWalker
lets you read from, and write to objects using Path
objects. The value you
read or write can be deeply nested inside the object. The Path
object specifies where inside the
object the value is located. A PathWalker
can read almost any type of object it encounters as it
walks down the path towards the last path segment: JavaBeans, records, maps, collections, arrays and scalar
values. It can also write to most of them. The PathWalker
class has various use cases:
- When processing large batches of sparsely populated objects
- When processing large batches of differently structured objects
- When it does not really matter whether a deeply nested value is
null
or just not present at all - To keep your code concise and clean when reading a deeply nested value.
Exception Suppression
By default, a PathWalker
will not throw an exception if it cannot read or write a value
— that is, if it cannot walk a path all the way down to the last path segment. That would defy the
purposes listed above. Instead, it just returns Result.notAvailable()
when failing to read a
value, and false
when failing to write a value. However, the PathWalker
class contains
constructors that allow you to disable exception suppression.
With exception suppression disabled a PathWalker
will throw a DeadEndException
when
failing to read/write a value, which may be useful for debugging.
Path Segment Deserialization
If you want a PathWalker
to be able to read from, or write to maps with a non-String
key type, you must instruct the PathWalker
how to deserialize the path segment representing the
key. This is done using a PathSegmentDeserializer
. The PathWalker
class has a
constructor that enables you to specify a PathSegmentDeserializer
.
- Author:
- Ayco Holleman
-
Constructor Summary
ConstructorsConstructorDescriptionPathWalker
(String... paths) Creates aPathWalker
for the specified paths.PathWalker
(List<Path> paths) Creates aPathWalker
for the specified paths.PathWalker
(List<Path> paths, boolean suppressExceptions) Creates aPathWalker
for the specified paths.PathWalker
(List<Path> paths, boolean suppressExceptions, PathSegmentDeserializer segmentDeserializer) Creates aPathWalker
for the specified paths.PathWalker
(Path... paths) Creates aPathWalker
for the specified paths. -
Method Summary
Modifier and TypeMethodDescription<T> Result
<T> Reads the value of the first path specified through the constructor.static <T> Result
<T> Returns aResult
object containing the value at the specified path orResult.notAvailable()
if the value could not be retrieved.Returns the values of the paths specified through the constructor.readIntoList
(Object host) Returns the values of the paths specified through the constructor.readIntoMap
(Object host) Returns the values of the paths specified through the constructor.boolean
Sets the value of the first path specified through the constructor.boolean[]
writeValues
(Object host, Object... values) Sets the values of the paths specified through the constructor.boolean[]
writeValues
(Object host, List<Object> values) Sets the values of the paths specified through the constructor.
-
Constructor Details
-
PathWalker
Creates aPathWalker
for the specified paths.- Parameters:
paths
- the paths to read or write
-
PathWalker
Creates aPathWalker
for the specified paths.- Parameters:
paths
- the paths to read or write
-
PathWalker
-
PathWalker
-
PathWalker
public PathWalker(List<Path> paths, boolean suppressExceptions, PathSegmentDeserializer segmentDeserializer) Creates aPathWalker
for the specified paths.- Parameters:
paths
- the paths to read or writesuppressExceptions
- whether to enable exception suppressionsegmentDeserializer
- a function that deserializes path segments into non-String map keys
-
-
Method Details
-
read
Returns aResult
object containing the value at the specified path orResult.notAvailable()
if the value could not be retrieved.- Type Parameters:
T
- the type of the value- Parameters:
host
- the object from which to read the valuepath
- the path specifying where to find the value- Returns:
- a
Result
object containing the value at the specified path orResult.notAvailable()
if the value could not be retrieved
-
readAll
Returns the values of the paths specified through the constructor. The returned map maps the paths to their values.- Parameters:
host
- the object to read the values from- Returns:
- the values of the paths specified through the constructor
- Throws:
DeadEndException
- if exception suppression is disabled and thePathWalker
fails to retrieve the values of one or more paths.
-
readIntoMap
Returns the values of the paths specified through the constructor. The returned map maps the path strings to their values.- Parameters:
host
- the object to read the values from- Returns:
- the values of the paths specified through the constructor
- Throws:
DeadEndException
- if exception suppression is disabled and thePathWalker
fails to retrieve the values of one or more paths.
-
readIntoList
Returns the values of the paths specified through the constructor. The values are returned in the same order as the paths.- Parameters:
host
- the object to read the values from- Returns:
- the values of the paths specified through the constructor
- Throws:
DeadEndException
- if exception suppression is disabled and thePathWalker
fails to retrieve the values of one or more paths.
-
read
Reads the value of the first path specified through the constructor. Convenient if you specified just one path.- Type Parameters:
T
- The type of the value being returned- Parameters:
host
- the object from which to read the value- Returns:
- the value of the first path specified through the constructor
- Throws:
DeadEndException
- if exception suppression is disabled and thePathWalker
fails to retrieve the values of one or more paths.
-
writeValues
Sets the values of the paths specified through the constructor. The provided array of values must have the same length as the number of paths.- Parameters:
host
- the object to which to write the valuesvalues
- the values to write- Returns:
- a
boolean
array indicating which paths could successfully be set, and which could not.
-
writeValues
Sets the values of the paths specified through the constructor. The providedList
of values must have the same size as the number of paths.- Parameters:
host
- the object to which to write the valuesvalues
- the values to write- Returns:
- a
boolean
array indicating which paths could successfully be set, and which could not.
-
write
-