Class Traversr<DataType>

  • Direct Known Subclasses:
    SimpleTraversr

    public abstract class Traversr<DataType>
    extends java.lang.Object
    Traversr allows you to walk JSON tree structures of data, and to GET and SET operations. Corner cases that arise during tree walk, are handled by subclasses. Ex: If no data exists mid tree walk quit or insert a new container? Or if there is data but it is the wrong type : overwrite or skip? Traversr analyzes the path path to be traversed and creates a "linked list" of Traversal objects. Then that list of Traversals can be used many times to write data into different JSON tree structures with different key values. For example given a Shiftr output path of : "tuna[&1].bob.&3[]" some of the keys are known, "tuna" and "bob", but other keys will only be known later. However, the structure of the output path will not change, which means we can do some work before the keys are known. First the output path is turned into its canonical form : "tuna.[4].[&1].bob.&3.[]". Then, a series of Traversals is created. tuna -> MapTraversal [&1] -> ArrayTraversal bob -> MapTraversal &3 -> MapTraversal [] -> AutoExpandArrayTraversal Later, a list of keys can then be provided, such as [ "tuna", "2", "bob", "smith", "[]" ], and they can be quickly used without having to build or parse any more objects. The list of keys are all Strings, which ArrayTraversals will convert to Integers as needed.
    • Constructor Summary

      Constructors 
      Constructor Description
      Traversr​(java.lang.String humanPath)  
      Traversr​(java.util.List<java.lang.String> paths)
      Constructor where we provide a known good set of pathElement Strings in a list.
    • Field Detail

      • traversalLength

        private final int traversalLength
    • Constructor Detail

      • Traversr

        public Traversr​(java.lang.String humanPath)
      • Traversr

        public Traversr​(java.util.List<java.lang.String> paths)
        Constructor where we provide a known good set of pathElement Strings in a list. Aka, no need to extract it from a "Human Readable" form.
    • Method Detail

      • get

        public Optional<DataType> get​(java.lang.Object tree,
                                      java.util.List<java.lang.String> keys)
        Note : Calling this method MAY modify the tree object by adding new Maps and Lists as needed for the traversal. This is determined by the behavior of the implementations of the abstract methods of this class.
      • set

        public Optional<DataType> set​(java.lang.Object tree,
                                      java.util.List<java.lang.String> keys,
                                      DataType data)
        Parameters:
        tree - tree of Map and List JSON structure to navigate
        data - JSON style data object you want to set
        Returns:
        returns the data object if successfully set, otherwise null if there was a problem walking the path
      • remove

        public Optional<DataType> remove​(java.lang.Object tree,
                                         java.util.List<java.lang.String> keys)
        Note : Calling this method MAY modify the tree object by adding new Maps and Lists as needed for the traversal. This is determined by the behavior of the implementations of the abstract methods of this class.
      • handleFinalSet

        public abstract Optional<DataType> handleFinalSet​(TraversalStep traversalStep,
                                                          java.lang.Object tree,
                                                          java.lang.String key,
                                                          DataType data)
        Allow subclasses to control how "sets" are done, if/once the traversal has made it to the the last element. Overwrite existing data? List-ize existing data with new data?
        Returns:
        the data object if the set was successful, or null if not
      • handleIntermediateGet

        public abstract Optional<DataType> handleIntermediateGet​(TraversalStep traversalStep,
                                                                 java.lang.Object tree,
                                                                 java.lang.String key,
                                                                 TraversalStep.Operation op)
        Allow subclasses to control how gets are handled for intermediate traversals. Example: we are a MapTraversal and out key is "foo". We simply do a 'tree.get( "foo" )'. However, if we get a null back, or we get back a data type incompatible with our child Traversal, what do we do? Overwrite or just return?