Package net.obvj.confectory.mapper
Class PropertiesToObjectMapper<T>
- java.lang.Object
-
- net.obvj.confectory.mapper.PropertiesToObjectMapper<T>
-
- Type Parameters:
T- the target type to be produced by thisMapper
- All Implemented Interfaces:
Mapper<T>
public class PropertiesToObjectMapper<T> extends Object implements Mapper<T>
A specializedMapperthat loads the contents of aSource(e.g.: file, URL) in thePropertiesformat (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
@Propertyannotation, defining a custom key to be mapped
Notes:
- The fields in the target object can be
private(recommended) - Fields marked
transientare 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,
falseforboolean, andnullfor 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
ObjectFactoryin 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 Summary
Constructors Constructor Description PropertiesToObjectMapper(Class<T> targetType)Builds a new Properties Mapper with the specified target type.PropertiesToObjectMapper(Class<T> targetType, ObjectFactory objectFactory)Builds a new Properties Mapper with the specified target type and a custom object factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tapply(InputStream inputStream)Applies thisMapperinto the given input.ConfigurationHelper<T>configurationHelper(T type)Creates a newConfigurationHelperinstance recommended by thisMapper.
-
-
-
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 thisMapper
-
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 thisMapperobjectFactory- theObjectFactoryto produce objects; not null- Since:
- 2.5.0
-
-
Method Detail
-
apply
public T apply(InputStream inputStream) throws IOException
Description copied from interface:MapperApplies thisMapperinto the given input.Note: The input stream must be closed by the caller after the mapping operation.
- Specified by:
applyin interfaceMapper<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
-
configurationHelper
public ConfigurationHelper<T> configurationHelper(T type)
Description copied from interface:MapperCreates a newConfigurationHelperinstance recommended by thisMapper.- Specified by:
configurationHelperin interfaceMapper<T>- Parameters:
type- the configuration object to be used by the helper- Returns:
- a new
ConfigurationHelperinstance
-
-