1

Given this simplification of my structure:

public class Order
{
    public String firstName;
    public Optional<PaymentInfo> paymentInfo;
}

public class PaymentInfo
{
    public String billingAddress;
    public int totalAmount;
}

I want to map to this class:

public class SimpleOrder
{
    public string firstName;
    public Optional<String> billingAddress;
}

I'm not sure how to only map the billingAddress if the Optional has value. I was thinking something like this:

private static Condition<?, ?> optionalHasValue = 
    (MappingContext<Optional<PaymentInfo>, String> context)
    -> context.getSource().isPresent();

private static class OrderMap extends PropertyMap<Order, SimpleOrder>
{
    @Override
    protected void configure()
    {
        map(source.firstName, destination.firstName);
        when(optionalHasValue)
            .map(source.paymentInfo.get().billingAddress, destination.billingAddress);
    }
}

but this fails with error "Cannot map final type java.util.Optional.". I did see this question about mapping Optional but it seemed be about a different issue.

Community
  • 1
  • 1
Zout
  • 821
  • 10
  • 18

0 Answers0