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 variously typed objects
- When it does not really matter whether a deeply nested value is
nullor just not present at all - To keep your code concise and clean when reading a deeply nested value.
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 reading values and
false when writing values. However, the PathWalker contains a constructor that enables
you to enable and disable exception suppression. Without exception suppression a PathWalker will
throw a DeadEndException when failing to read/write a value, which may be useful when debugging.
- Author:
- Ayco Holleman
-
Constructor Summary
ConstructorsConstructorDescriptionPathWalker(String... paths) Creates aPathWalkerfor the specified paths.PathWalker(List<Path> paths) Creates aPathWalkerfor the specified paths.PathWalker(List<Path> paths, boolean suppressExceptions) Creates aPathWalkerfor the specified paths.PathWalker(List<Path> paths, boolean suppressExceptions, PathSegmentDeserializer segmentDeserializer) Creates aPathWalkerfor the specified paths.PathWalker(Path... paths) Creates aPathWalkerfor the specified paths. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TReturns the value at the specified path.<T> Result<T> Reads the value of the first path specified through the constructor.static <T> Result<T> Returns aResultobject containing the value at the specified path orResult.notAvailable()if the value could not be retrieved.readValues(Object host) Returns the values of all paths specified through the constructor.booleanSets 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)
-
Constructor Details
-
PathWalker
Creates aPathWalkerfor the specified paths.- Parameters:
paths- One or more paths representing possibly deeply-nested properties
-
PathWalker
Creates aPathWalkerfor the specified paths.- Parameters:
paths- The paths to walk through the provided host objects
-
PathWalker
-
PathWalker
Creates aPathWalkerfor the specified paths.- Parameters:
paths- The action to take if a path could not be read or writtensuppressExceptions- Iftrue, thereadmethods will returnnullfor paths that could not be read. Thewritemethods will quietly return without having written the value. Iffalse, aDeadEndExceptionwill be thrown detailing the error.
-
PathWalker
public PathWalker(List<Path> paths, boolean suppressExceptions, PathSegmentDeserializer segmentDeserializer) Creates aPathWalkerfor the specified paths.- Parameters:
paths- The paths to walksuppressExceptions- Iftrue, thereadmethods will returnnullfor paths that could not be read. Thewritemethods will quietly return without having written the value. Iffalse, aDeadEndExceptionwill be thrown detailing the error.segmentDeserializer- A function that converts path segments to map keys. You need to provide this when reading from, or writing toMapobjects with a non-String key type.
-
-
Method Details
-
get
Returns the value at the specified path. If the value could not be read aNoSuchElementExceptionexception is thrown (seeResult.get()). This method is useful if you already know for sure that the specified path can be traced through the specified host object.- 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:
- the value at the specified path
-
read
Returns aResultobject 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
Resultobject containing the value at the specified path orResult.notAvailable()if the value could not be retrieved
-
readValues
Returns the values of all paths specified through the constructor.- Parameters:
host- the object to read the values from- Returns:
- the values of all paths specified through the constructor
- Throws:
DeadEndException- IfsuppressExceptionsis false and thePathWalkerfails 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- IfsuppressExceptionsis false and thePathWalkerfails to retrieve the value of the first path.
-
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
booleanarray indicating which paths could successfully be set, and which could not.
-
writeValues
-
write
-