Class PathTreeWalker

java.lang.Object
org.klojang.path.PathTreeWalker

public final class PathTreeWalker extends Object

The PathTreeWalker is functionally the same as the PathWalker class. However, it will turn the paths you specify through one of the constructors into a tree of path segments. This guarantees that the PathTreeWalker makes a minimal amount of "movements" through the object from which to retrieve the values. For example, say you have specified the following paths:


 person.address.street
 person.address.zipCode
 parson.address.city
 
A PathWalker would perform 9 read operations as it retrieves the values for street, zipCode, and city. The PathTreeWalker on the other hand performs only 5 read operations, because it retrieves the values for person and address just once. Thus the PathTreeWalker is likely to be more performant when reading a relatively large number of paths that, together, constitute a somewhat flat hierarchy.
Author:
Ayco Holleman
  • Constructor Details

    • PathTreeWalker

      public PathTreeWalker(Path... paths)
      Creates a PathWalker for the specified paths.
      Parameters:
      paths - One or more paths representing possibly deeply-nested properties
    • PathTreeWalker

      public PathTreeWalker(String... paths)
      Creates a PathWalker for the specified paths.
      Parameters:
      paths - The paths to walk through the provided host objects
    • PathTreeWalker

      public PathTreeWalker(List<Path> paths)
      Creates a PathWalker for the specified paths.
      Parameters:
      paths - The paths to walk through the provided host objects
    • PathTreeWalker

      public PathTreeWalker(List<Path> paths, boolean suppressExceptions)
      Creates a PathWalker for the specified paths.
      Parameters:
      paths - The action to take if a path could not be read or written
      suppressExceptions - If true, the read methods will return null for paths that could not be read. The write methods will quietly return without having written the value. If false, a DeadEndException will be thrown detailing the error.
    • PathTreeWalker

      public PathTreeWalker(List<Path> paths, boolean suppressExceptions, PathSegmentDeserializer segmentDeserializer)
      Creates a PathWalker for the specified paths.
      Parameters:
      paths - The paths to walk
      suppressExceptions - If true, the read methods will return null for paths that could not be read. The write methods will quietly return without having written the value. If false, a DeadEndException will 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 to Map objects with a non-String key type.
  • Method Details

    • read

      public Map<Path, Result<Object>> read(Object host) throws DeadEndException
      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 - If suppressExceptions is false and the PathWalker fails to retrieve the values of one or more paths.