Package com.bazaarvoice.jolt.traversr
Class Traversr<DataType>
- java.lang.Object
-
- com.bazaarvoice.jolt.traversr.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.
-
-
Field Summary
Fields Modifier and Type Field Description private TraversalStep
root
private int
traversalLength
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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.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.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.private TraversalStep
makePathElement(java.lang.String path, TraversalStep child)
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.Optional<DataType>
set(java.lang.Object tree, java.util.List<java.lang.String> keys, DataType data)
-
-
-
Field Detail
-
root
private final TraversalStep root
-
traversalLength
private final int traversalLength
-
-
Method Detail
-
makePathElement
private TraversalStep makePathElement(java.lang.String path, TraversalStep child)
-
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 navigatedata
- 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?
-
-