Constants in meter offsets

Does anyone know how to put a constant value in an attribute with dozer? I did not see anything about this in the bulldozer documentation

+7
java dozer
source share
6 answers

Not quite sure what you mean - if you want Dozer to always fill BeanB.someField constant whenever you type BeanA into BeanB?

You may want to register a custom converter for this mapping.

+3
source share

How to use the event mechanism?

So, you can register a listener that will configure the value in the mappingFinished() your listener. See the dozer doc for events for more details. Of course, you have to protect the setup code with some kind of if ... instanceof condition.

+2
source share

Recent bulldozer builds make this easier. You can specify both custom transducers, and you can specify the parameters for this transformer for a given field mapping. It should be trivial to create one "ConstantConverter" that will take an input parameter and place it in the output field 100% of the time.

+1
source share

One possible implementation:

 public class ConstantsCustomConvertor implements ConfigurableCustomConverter{ private String pararamter; @Override public Object convert(Object existingDestinationFieldValue, Object sourceFieldValue, Class<?> destinationClass, Class<?> sourceClass) { return pararamter; } @Override public void setParameter(String parameter) { this.pararamter = parameter; } } 

Example:

 <field custom-converter-param="CONTANT_VALUE" custom-converter="org.yourcompany.ConstantsCustomConvertor"> <a>a-class-dummyfieldname</a> <b>b-class-fieldname</b> </field> 

This custom converter assumes that the class name of the b-class is of type String.

+1
source share

This is pretty simple with ModelMapper :

 ModelMapper modelMapper = new ModelMapper(); modelMapper.addMappings(new PropertyMap<SourceClass, DestClass>() { protected void configure() { map().setSomeProperty(someConstant); } }); 

This example maps someConstant to DestClass.someProperty .

You can view more examples and documents at: http://modelmapper.org

0
source share

Assuming you only want to do this in a one-way display, the following will work for the String constant:

 /** * Custom one-way Dozer converter mapping to constant string value specified by a parameter. */ public class OneWayStringConstantConverter extends DozerConverter<Class, String> { public OneWayBooleanConstantConverter() { super(Class.class, String.class); } @Override public String convertTo(Class aClass, String aString) { // Return constant value specified by the parameter (source is ignored) return getParameter(); } @Override public Class convertFrom(String aString, Class aClass) { throw new UnsupportedOperationException( "OneWayStringConstantConverter should only be used in one-way mappings"); } } 

... is called:

  <field custom-converter="full.path.to.OneWayStringConstantConverter" custom-converter-param="My constant string value"> <a>class</a> <!-- Source not used: Converter only sets target of a one-way mapping --> <b>targetField</b> </field> 

I would recommend using the class value as a fictitious "source field", since it is not yet used, the getClass() method is guaranteed to exist in any object.

I needed to do this for a boolean, so I used this:

 /** * Custom one-way Dozer converter which maps to the constant boolean value (true or false) * specified by a parameter. */ public class OneWayBooleanConstantConverter extends DozerConverter<Class, Boolean> { public OneWayBooleanConstantConverter() { super(Class.class, Boolean.class); } @Override public Boolean convertTo(Class aClass, Boolean aBoolean) { // Return constant boolean value specified by the parameter (source is ignored) return Boolean.parseBoolean(getParameter()); } @Override public Class convertFrom(Boolean aBoolean, Class aClass) { throw new UnsupportedOperationException( "OneWayBooleanConstantConverter should only be used in one-way mappings"); } } 
0
source share

All Articles