Custom Convertor for a particular field

389 views
Skip to first unread message

jat...@gmail.com

unread,
Aug 3, 2015, 4:53:05 PM8/3/15
to Selma
I was wondering if the @Field or any other annotation supports defining which function (custom convertor) to use to create this fields value from the source object (let's say a field is based on multiple source fields for whatever reason).

Consider I have the following two classes (this is a simple example, but I did face this issue on an existing project where I was trying to plug in Selma instead of handcoded mappings)

public class Measurement {
private BigDecimal temp;
private BigDecimal humidity;

//setters and getters

}

public class MeasurementDto {
    private double temperature;
    private double celsiusTemp;
    private double fahrenTemp;
    private double humidity;

//setters and getters
    }

With a mapping class like this:
@Mapper(withCustomFields = {
        @Field({ "com.jatinst.selma.demo.model.MeasurementDto.temp",
                "com.jatinst.selma.demo.model.Measurement.temperature" }),
        @Field({ "com.jatinst.selma.demo.model.MeasurementDto.celsiustemp",
                "com.jatinst.selma.demo.model.Measurement.temperature" }),
        @Field({ "com.jatinst.selma.demo.model.MeasurementDto.fahrentemp",
                "com.jatinst.selma.demo.model.Measurement.temperature" }) }, withCustom = { BigDecimalToDoubleMapper.class })
public interface MeasurementMapper {
    public MeasurementDto getMeasurement(Measurement measure);

}

Now, there is no way I can specify that fahrenTemp = (some constant) * Measurement.temperature . I can obviously make it a different type and use a Custom Mapper, but what if the destination field was a combination of two source fields? Dozer or Orika will support this by just specifying a custom convertor at the individual field level. Is there any way to do this in Selma?

The source code for this example can be found at: https://github.com/jatinst/code-samples/tree/master/selma
Reply all
Reply to author
Forward
0 new messages