Class DataMapper


  • public class DataMapper
    extends java.lang.Object
    Maps data from a stream to input instances for a service. This class is intended as a basis for testing (here avoiding the test scope for generated code). The idea is that all input types are represented as attributes of a generated class (given in terms of a JSON file/stream). The generated service test calls this class providing a consumer to take over the data.
    Author:
    Holger Eichelberger, SSE
    • Constructor Summary

      Constructors 
      Constructor Description
      DataMapper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void mapJsonData​(java.io.InputStream stream, java.lang.Class<T> cls, java.util.function.Consumer<T> cons)
      Maps the data in stream to instances of cls, one instance per line.
      static <T> void mapJsonData​(java.io.InputStream stream, java.lang.Class<T> cls, java.util.function.Consumer<T> cons, boolean failOnUnknownProperties)
      Maps the data in stream to instances of cls, one instance per line.
      static <T> void mapJsonData​(java.io.InputStream stream, java.lang.Class<T> cls, java.util.function.Consumer<T> cons, boolean failOnUnknownProperties, java.util.function.Supplier<java.lang.Boolean> continueFunction)
      Maps the data in stream to instances of cls, one instance per line.
      static <T> DataMapper.IOIterator<T> mapJsonDataToIterator​(java.io.InputStream stream, java.lang.Class<T> cls)
      Maps the data in stream to instances of cls, one instance per line, returned in terms of an iterator.
      static <T> DataMapper.IOIterator<T> mapJsonDataToIterator​(java.io.InputStream stream, java.lang.Class<T> cls, boolean failOnUnknownProperties)
      Maps the data in stream to instances of cls, one instance per line, returned in terms of an iterator.
      • Methods inherited from class java.lang.Object

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

      • DataMapper

        public DataMapper()
    • Method Detail

      • mapJsonData

        public static <T> void mapJsonData​(java.io.InputStream stream,
                                           java.lang.Class<T> cls,
                                           java.util.function.Consumer<T> cons)
                                    throws java.io.IOException
        Maps the data in stream to instances of cls, one instance per line. Calls cons per instance/line. Closes stream. Ignores unknown attributes in cls.
        Type Parameters:
        T - the type of data to read
        Parameters:
        stream - the stream to read (may be null for none)
        cls - the type of data to read
        cons - the consumer to be called per instance
        Throws:
        java.io.IOException - if I/O or JSON parsing errors occur
      • mapJsonData

        public static <T> void mapJsonData​(java.io.InputStream stream,
                                           java.lang.Class<T> cls,
                                           java.util.function.Consumer<T> cons,
                                           boolean failOnUnknownProperties)
                                    throws java.io.IOException
        Maps the data in stream to instances of cls, one instance per line. Calls cons per instance/line. Closes stream.
        Type Parameters:
        T - the type of data to read
        Parameters:
        stream - the stream to read (may be null for none)
        cls - the type of data to read
        cons - the consumer to be called per instance
        failOnUnknownProperties - whether parsing shall be tolerant or not, the latter may be helpful for debugging
        Throws:
        java.io.IOException - if I/O or JSON parsing errors occur
      • mapJsonData

        public static <T> void mapJsonData​(java.io.InputStream stream,
                                           java.lang.Class<T> cls,
                                           java.util.function.Consumer<T> cons,
                                           boolean failOnUnknownProperties,
                                           java.util.function.Supplier<java.lang.Boolean> continueFunction)
                                    throws java.io.IOException
        Maps the data in stream to instances of cls, one instance per line. Calls cons per instance/line. Closes stream.
        Type Parameters:
        T - the type of data to read
        Parameters:
        stream - the stream to read (may be null for none)
        cls - the type of data to read
        cons - the consumer to be called per instance
        failOnUnknownProperties - whether parsing shall be tolerant or not, the latter may be helpful for debugging
        continueFunction - optional function that tells the data mapper to go on reading the input, may be null for none
        Throws:
        java.io.IOException - if I/O or JSON parsing errors occur
      • mapJsonDataToIterator

        public static <T> DataMapper.IOIterator<T> mapJsonDataToIterator​(java.io.InputStream stream,
                                                                         java.lang.Class<T> cls)
                                                                  throws java.io.IOException
        Maps the data in stream to instances of cls, one instance per line, returned in terms of an iterator. Ignores unknown attributes in cls.
        Type Parameters:
        T - the type of data to read
        Parameters:
        stream - the stream to read (may be null for none)
        cls - the type of data to read
        Returns:
        the data iterator
        Throws:
        java.io.IOException - if I/O or JSON parsing errors occur
      • mapJsonDataToIterator

        public static <T> DataMapper.IOIterator<T> mapJsonDataToIterator​(java.io.InputStream stream,
                                                                         java.lang.Class<T> cls,
                                                                         boolean failOnUnknownProperties)
                                                                  throws java.io.IOException
        Maps the data in stream to instances of cls, one instance per line, returned in terms of an iterator. Ignores unknown attributes in cls.
        Type Parameters:
        T - the type of data to read
        Parameters:
        stream - the stream to read (may be null for none)
        cls - the type of data to read
        failOnUnknownProperties - whether parsing shall be tolerant or not, the latter may be helpful for debugging
        Returns:
        the data iterator
        Throws:
        java.io.IOException - if I/O or JSON parsing errors occur