Class NativeMaterializer

java.lang.Object
com.apicatalog.tree.io.TreeTraversal
com.apicatalog.tree.io.java.NativeMaterializer
All Implemented Interfaces:
TreeGenerator

public class NativeMaterializer extends TreeTraversal implements TreeGenerator
A specialized class that builds a native object model from any tree-like source.

This class implements both TreeTraversal and TreeGenerator, allowing it to act as a self-contained transformation engine. It traverses a source structure using its NodeVisitor capabilities and consumes its own traversal events via its NodeGenerator implementation to construct a Object tree in memory.

The class is stateful and designed for a single transformation. It can be reused by calling the reset() method.

  • Field Details

    • structures

      protected final Deque<Object> structures
    • object

      protected Object object
  • Constructor Details

    • NativeMaterializer

      public NativeMaterializer()
  • Method Details

    • features

      public Features features()
      Specified by:
      features in interface TreeGenerator
    • node

      public static Object node(TreeIO node) throws TreeIOException
      Throws:
      TreeIOException
    • node

      public static Object node(Object node, TreeAdapter adapter) throws TreeIOException
      Throws:
      TreeIOException
    • scalar

      public static Object scalar(Object node, TreeAdapter adapter) throws TreeIOException
      Throws:
      TreeIOException
    • structure

      public Object structure(Object node, TreeAdapter adapter) throws TreeIOException
      The primary entry point for materialization. Traverses the given source node.
      Parameters:
      node - the source root node to traverse
      adapter - the adapter for interpreting the source node's structure
      Returns:
      the fully materialized object
      Throws:
      TreeIOException - if an error occurs during generation
    • object

      public Object object()
      Returns the fully materialized Object after a successful traversal.
      Returns:
      the resulting Object, or null if traversal has not completed
    • reset

      public NativeMaterializer reset()
      Resets the visitor's internal state, clearing the traversal stack and counters. The visitor can be reused after calling this method, but a new root node must be set using TreeTraversal.root(Object, TreeAdapter).

      Clears the partially built structure and the internal builder stack, allowing the instance to be reused for a new materialization.

      Overrides:
      reset in class TreeTraversal
      Returns:
      this instance, for chaining.
    • nullValue

      public void nullValue() throws TreeIOException
      Adds a null value to the current context.
      Specified by:
      nullValue in interface TreeGenerator
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • booleanValue

      public void booleanValue(boolean node) throws TreeIOException
      Adds a boolean value to the current context.
      Specified by:
      booleanValue in interface TreeGenerator
      Parameters:
      node - the boolean value to add.
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • stringValue

      public void stringValue(String node) throws TreeIOException
      Adds a string value. Depending on the context, this could represent a map key, a value associated with a key, or an element in a collection.

      If the current context is a PROPERTY_KEY, the string is pushed onto the builder stack to be used as a key. Otherwise, it creates a String value.

      Specified by:
      stringValue in interface TreeGenerator
      Parameters:
      node - the non-null string value to add.
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • numericValue

      public void numericValue(long node) throws TreeIOException
      Adds a long integer value. Depending on the context, this could represent a map key (in formats like CBOR), a value, or a collection element.
      Specified by:
      numericValue in interface TreeGenerator
      Parameters:
      node - the long value to add.
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • numericValue

      public void numericValue(BigInteger node) throws TreeIOException
      Adds an arbitrary-precision integer value. Depending on the context, this could represent a map key, a value, or a collection element.
      Specified by:
      numericValue in interface TreeGenerator
      Parameters:
      node - the non-null BigInteger value to add.
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • numericValue

      public void numericValue(double node) throws TreeIOException
      Adds a double-precision floating-point value to the current context.
      Specified by:
      numericValue in interface TreeGenerator
      Parameters:
      node - the double value to add.
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • numericValue

      public void numericValue(BigDecimal node) throws TreeIOException
      Adds an arbitrary-precision decimal value to the current context.
      Specified by:
      numericValue in interface TreeGenerator
      Parameters:
      node - the non-null BigDecimal value to add.
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • binaryValue

      public void binaryValue(byte[] node) throws TreeIOException
      Adds a binary data value to the current context. The specific encoding (e.g., Base64 for JSON, or direct bytes for CBOR) is determined by the underlying implementation.
      Specified by:
      binaryValue in interface TreeGenerator
      Parameters:
      node - the non-null byte array to add.
      Throws:
      UnsupportedOperationException - always
      TreeIOException - if an I/O error occurs during construction.
    • beginMap

      public void beginMap() throws TreeIOException
      Begins a new map (or object) structure. After this call, the generator expects a sequence of key-value pairs.

      The type of a key is determined by the target format. For formats like JSON, keys are strings added via TreeGenerator.stringValue(String). For others, like CBOR, keys can also be other types.

      Every call to this method must be matched by a corresponding call to TreeGenerator.end().
      Specified by:
      beginMap in interface TreeGenerator
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • beginList

      public void beginList() throws TreeIOException
      Begins a new list (or array) structure. An ordered collection that allows duplicates. After this call, subsequent calls are expected to be the elements of the list. Every call to this method must be matched by a corresponding call to TreeGenerator.end().
      Specified by:
      beginList in interface TreeGenerator
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • beginSet

      public void beginSet() throws TreeIOException
      Begins a new set structure. An unordered collection of unique elements. After this call, subsequent calls are expected to be the elements of the set. Every call to this method must be matched by a corresponding call to TreeGenerator.end().
      Specified by:
      beginSet in interface TreeGenerator
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • end

      public void end() throws TreeIOException
      Ends the current map or collection structure. This call must match a preceding TreeGenerator.beginMap() or TreeGenerator.beginList().

      Finalizes the current Map or List, pops it from the stack, builds the final Object, and attaches it to its parent structure if one exists.

      Specified by:
      end in interface TreeGenerator
      Throws:
      TreeIOException - if an I/O error occurs during construction.
    • value

      protected void value(Object value)
      Internal dispatcher that places a newly created Object into the correct position within the structure being built.
      Parameters:
      value - the Object to place