java.lang.Object
org.klojang.path.PathWalker
A
PathWalker lets you read and write deeply nested values using Path objects. It
can read almost any type of object it encounters as it walks down the path towards the last path
segment: JavaBeans, maps, collections, arrays, scalars, and opaque types like String. It
can also write to most of them. A PathWalker can be useful when processing large batches
of sparsely populated maps or objects and/or it doesn't really matter whether a deeply nested
value is null or just not present at all. By default, the PathWalker will return
null in either case (although you can change this).- 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, KeyDeserializer keyDeserializer) Creates aPathWalkerfor the specified paths.PathWalker(Path... paths) Creates aPathWalkerfor the specified paths. -
Method Summary
Modifier and TypeMethodDescription<T> TReads the value of the first path specified through the constructor.Object[]readValues(Object host) Returns the values of all paths specified through the constructor.voidreadValues(Object host, Object[] output) Reads the values of all paths specified through the constructor.voidreadValues(Object host, Map<Path, Object> output) Reads the values of all paths and inserts them into the provided path-to-value map.booleanSets the value of the first path specified through the constructor.intwriteValues(Object host, Object... values) Sets the values of the paths specified through the constructor.
-
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
Creates aPathWalkerfor the specified paths.- Parameters:
paths- The paths to walk through the provided host objects
-
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, aPathWalkerExceptionwill be thrown detailing the error.
-
PathWalker
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, aPathWalkerExceptionwill be thrown detailing the error.keyDeserializer- A function that converts path segments to map keys. You need to provide this if the host objects are, or contain,Mapinstances with non-string keys.
-
-
Method Details
-
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:
PathWalkerException- IfsuppressExceptionsis false and thePathWalkerfails to retrieve the values of one or more paths.
-
readValues
Reads the values of all paths specified through the constructor.- Parameters:
host- the object to read the path values fromoutput- an array into which to place the values. The length of the output array must be equal to, or greater than the number of paths specified through the constructor.- Throws:
PathWalkerException- IfsuppressExceptionsis false and thePathWalkerfails to retrieve the values of one or more paths.
-
readValues
Reads the values of all paths and inserts them into the provided path-to-value map.- Parameters:
host- the object from which to read the valuesoutput- TheMapinto which to put the values- Throws:
PathWalkerException- 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:
PathWalkerException- 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:
- the number of successfully written values
-
write
Sets the value of the first path specified through the constructor. Convenient if you specified just one path.- Parameters:
host- the object to write the value tovalue- The value to write- Returns:
trueif the value was successfully written
-