Class Chainr

  • All Implemented Interfaces:
    ContextualTransform, JoltTransform, Transform

    public class Chainr
    extends java.lang.Object
    implements Transform, ContextualTransform
    Chainr is the JOLT mechanism for chaining JoltTransforms together. Any of the built-in JOLT transform types can be called directly from Chainr. Any custom-written Java transforms can be adapted in by implementing the Transform or SpecDriven interfaces. A Chainr spec should be an array of objects in order that look like this: [ { "operation": "[operation-name]", // stuff that the specific transform needs go here }, ... ] Each operation is called in the order that it is specified within the array. The original input to Chainr is passed into the first operation, with its output passed into the next, and so on. The output of the final operation is returned from Chainr. Currently, [operation-name] can be any of the following: - shift: (Shiftr) a tool for moving parts of an input JSON document to a new output document - default: (Defaultr) a tool for applying default values to the provided JSON document - remove: (Removr) a tool for removing specific values from the provided JSON document - sort: (Sortr) sort the JSON document - java: passes control to whatever Java class you specify as long as it implements the Transform interface Shift, default, and remove operation all require a "spec", while sort does not. [ { "operation": "shift", "spec" : { // shiftr spec } }, { "operation": "sort" // sort does not need a spec }, ... ] Custom Java classes that implement Transform and/or SpecDriven can be loaded by specifying the full className to load. Additionally, if upon reflection of the class we see that it is an instance of a SpecDriven, then we will construct it with a the supplied "spec" object. [ { "operation": "com.bazaarvoice.tuna.CustomTransform", "spec" : { ... } // optional spec to use to construct a custom Transform if it has the SpecDriven marker interface. }, ... ]
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  Chainr.ContextualTransformAdapter
      Adapt "normal" Transforms to look like ContextualTransforms, so that Chainr can just maintain a single list of "JoltTransforms" to run.
    • Constructor Summary

      Constructors 
      Constructor Description
      Chainr​(java.util.List<JoltTransform> joltTransforms)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Object doTransform​(java.util.List<ContextualTransform> transforms, java.lang.Object input, java.util.Map<java.lang.String,​java.lang.Object> context)  
      static Chainr fromSpec​(java.lang.Object input)  
      static Chainr fromSpec​(java.lang.Object input, ChainrInstantiator instantiator)  
      java.util.List<ContextualTransform> getContextualTransforms()
      This method allows Chainr clients to examine the ContextualTransforms in this Chainr instance.
      boolean hasContextualTransforms()  
      java.lang.Object transform​(int from, int to, java.lang.Object input)
      Useful for testing and debugging.
      java.lang.Object transform​(int from, int to, java.lang.Object input, java.util.Map<java.lang.String,​java.lang.Object> context)
      Have Chainr run a subset of the transforms in it's spec.
      java.lang.Object transform​(int to, java.lang.Object input)
      Have Chainr run a subset of the transforms in it's spec.
      java.lang.Object transform​(int to, java.lang.Object input, java.util.Map<java.lang.String,​java.lang.Object> context)
      Useful for testing and debugging.
      java.lang.Object transform​(java.lang.Object input)
      Execute a transform on some input JSON with optionally provided "context" and return the result.
      java.lang.Object transform​(java.lang.Object input, java.util.Map<java.lang.String,​java.lang.Object> context)
      Runs a series of Transforms on the input, piping the inputs and outputs of the Transforms together.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Chainr

        public Chainr​(java.util.List<JoltTransform> joltTransforms)
    • Method Detail

      • fromSpec

        public static Chainr fromSpec​(java.lang.Object input)
      • transform

        public java.lang.Object transform​(java.lang.Object input,
                                          java.util.Map<java.lang.String,​java.lang.Object> context)
        Runs a series of Transforms on the input, piping the inputs and outputs of the Transforms together. Chainr instances are meant to be immutable once they are created so that they can be used many times. The notion of passing "context" to the transforms allows chainr instances to be reused, even in situations were you need to slightly vary.
        Specified by:
        transform in interface ContextualTransform
        Parameters:
        input - a JSON (Jackson-parsed) maps-of-maps object to transform
        context - optional tweaks that the consumer of the transform would like
        Returns:
        an object representing the JSON resulting from the transform
        Throws:
        TransformException - if the specification is malformed, an operation is not found, or if one of the specified transforms throws an exception.
      • transform

        public java.lang.Object transform​(java.lang.Object input)
        Description copied from interface: Transform
        Execute a transform on some input JSON with optionally provided "context" and return the result.
        Specified by:
        transform in interface Transform
        Parameters:
        input - the JSON object to transform in plain vanilla Jackson Map style
        Returns:
        the results of the transformation
      • transform

        public java.lang.Object transform​(int to,
                                          java.lang.Object input)
        Have Chainr run a subset of the transforms in it's spec. Useful for testing and debugging.
        Parameters:
        input - the input data to transform
        to - transform from the chainrSpec to end with: 0 based index exclusive
      • transform

        public java.lang.Object transform​(int to,
                                          java.lang.Object input,
                                          java.util.Map<java.lang.String,​java.lang.Object> context)
        Useful for testing and debugging.
        Parameters:
        input - the input data to transform
        to - transform from the chainrSpec to end with: 0 based index exclusive
        context - optional tweaks that the consumer of the transform would like
      • transform

        public java.lang.Object transform​(int from,
                                          int to,
                                          java.lang.Object input)
        Useful for testing and debugging.
        Parameters:
        input - the input data to transform
        from - transform from the chainrSpec to start with: 0 based index
        to - transform from the chainrSpec to end with: 0 based index exclusive
      • transform

        public java.lang.Object transform​(int from,
                                          int to,
                                          java.lang.Object input,
                                          java.util.Map<java.lang.String,​java.lang.Object> context)
        Have Chainr run a subset of the transforms in it's spec. Useful for testing and debugging.
        Parameters:
        input - the input data to transform
        from - transform from the chainrSpec to start with: 0 based index
        to - transform from the chainrSpec to end with: 0 based index exclusive
        context - optional tweaks that the consumer of the transform would like
      • doTransform

        private static java.lang.Object doTransform​(java.util.List<ContextualTransform> transforms,
                                                    java.lang.Object input,
                                                    java.util.Map<java.lang.String,​java.lang.Object> context)
      • hasContextualTransforms

        public boolean hasContextualTransforms()
        Returns:
        true if this Chainr instance has any ContextualTransforms
      • getContextualTransforms

        public java.util.List<ContextualTransform> getContextualTransforms()
        This method allows Chainr clients to examine the ContextualTransforms in this Chainr instance. This may be helpful when building the "context".
        Returns:
        List of ContextualTransforms used by this Chainr instance