Class PropertiesToObjectMapper<T>

  • Type Parameters:
    T - the target type to be produced by this Mapper
    All Implemented Interfaces:
    Mapper<T>

    public class PropertiesToObjectMapper<T>
    extends Object
    implements Mapper<T>
    A specialized Mapper that loads the contents of a Source (e.g.: file, URL) in the Properties format (a sequence of key-value pairs) and converts it into a POJO (Plain-Old Java Object).

    Every property will be assigned to a field in the target object in either of the following cases:

    • the field name is equal to the property key (with no need to use an annotation in these cases); or
    • the field is marked with the @Property annotation, defining a custom key to be mapped

    Notes:

    • The fields in the target object can be private (recommended)
    • Fields marked transient are ignored
    • If the associated property is missing in the source, the corresponding field assumes a default value as specified by The JavaTM Tutorials > Language Basics > Variables > Primitive Data Types, i.e., zero for numeric types, false for boolean, and null for String (or any object type).

    By default, new objects will be created by allocating a new instance directly in the heap, without calling a constructor, then assigning each field via reflection. To change this behavior, you may specify a different ObjectFactory in the constructor:

     new PropertiesToObjectMapper(MyType.class, ObjectFactory.FAST); // Default
     new PropertiesToObjectMapper(MyType.class, ObjectFactory.CONSTRUCTOR_BASED);
     
    Since:
    1.2.0
    Author:
    oswaldo.bapvic.jr (Oswaldo Junior)
    See Also:
    Property, ObjectFactory
    • Constructor Detail

      • PropertiesToObjectMapper

        public PropertiesToObjectMapper​(Class<T> targetType)
        Builds a new Properties Mapper with the specified target type.
        Parameters:
        targetType - the target type to be produced by this Mapper
      • PropertiesToObjectMapper

        public PropertiesToObjectMapper​(Class<T> targetType,
                                        ObjectFactory objectFactory)
        Builds a new Properties Mapper with the specified target type and a custom object factory.
        Parameters:
        targetType - the target type to be produced by this Mapper
        objectFactory - the ObjectFactory to produce objects; not null
        Since:
        2.5.0
    • Method Detail

      • apply

        public T apply​(InputStream inputStream)
                throws IOException
        Description copied from interface: Mapper
        Applies this Mapper into the given input.

        Note: The input stream must be closed by the caller after the mapping operation.

        Specified by:
        apply in interface Mapper<T>
        Parameters:
        inputStream - the input stream to be mapped
        Returns:
        the mapped object
        Throws:
        IOException - if a low-level I/O problem (such and unexpected end-of-input, or network error) occurs